oracle sql optimization - sql

I have this query:
SELECT sd.sdt_service_type,
sd.sdt_status,
count(*) col_count
FROM mci_service_data sd
WHERE
sd.sdt_version = 1
AND sd.sdt_type = 'MMSP'
AND sd.sdt_status in (?)
AND(sd.STD_OPERATION_FLAG is null OR sd.STD_OPERATION_FLAG not like 'mark%')
AND sd.sdt_office_id in
(SELECT op.fld_ofs_id
FROM mci_ofs_per op
WHERE op.fld_per_id = ?)
group by sd.sdt_service_type,sd.sdt_status
in mci_service_data table there are indexes on
mci_service_data(sdt_type, sdt_version, sdt_status, sdt_office_id)
and mci_ofs_per(fld_per_id, fld_ofs_id).but this query takes time more than 10 seconds!
So,how this query will be optimize and faster?

For this query, I would recommend the following indexes:
mci_service_data(sdt_type, sdt_version, sdt_status, sdt_office_id)
mci_ofs_per(fld_per_id, fld_ofs_id)

Related

Determining what index to create given a query?

Given a SQL query:
SELECT *
FROM Database..Pizza pizza
JOIN Database..Toppings toppings ON pizza.ToppingId = toppings.Id
WHERE toppings.Name LIKE '%Mushroom%' AND
toppings.GlutenFree = 0 AND
toppings.ExtraFee = 1.25 AND
pizza.Location = 'Minneapolis, MN'
How do you determine what index to write to improve the performance of the query? (Assuming every value to the right of the equal is calculated at runtime)
Is there a built in command SQL command to suggest the proper index?
To me, it gets confusing when there's multiple JOINS that use fields from both tables.
For this query:
SELECT *
FROM Database..Pizza p JOIN
Database..Toppings t
ON p.ToppingId = t.Id
WHERE t.Name LIKE '%Mushroom%' AND
t.GlutenFree = 0 AND
t.ExtraFee = 1.25 AND
p.Location = 'Minneapolis, MN';
You basically have two options for indexes:
Pizza(location, ToppingId) and Toppings(id)
or:
Toppings(GlutenFree, ExtraFee, Name, id) and Pizza(ToppingId, location)
Which works better depends on how selective the different conditions are in the WHERE clause.

Too long execution time for update query

I'm trying to update the table with a query, that executing in ~5 sec on Postgresql and Oracle but takes too long on Firebird 2.5.
UPDATE GoodsCatUnit SET isDisplay=1
WHERE Id In (SELECT Min(gcu.Id) FROM GoodsCatUnit gcu GROUP BY gcu.GoodsCat_Id);
In the GoodsCatUnit ~34k rows and updating first 200 takes 15 seconds.
Try writing this using a correlated subquery and defining an index.
The query is:
UPDATE GoodsCatUnit gcu
SET isDisplay = 1
WHERE gcu.id = (SELECT MIN(gcu2.id)
FROM GoodsCatUnit gcu2
WHERE gcu2.GoodsCat_Id = gcu.GoodsCat_Id
) AND
gcu.isDisplay <> 1;
The index is on GoodsCatUnit(GoodsCat_Id, id).

SQL query optimization Oracle

I have this SQL query that first selects from a relative short table a number that is used after for another select, this time from a very large table, certain info using the code from the first one. This takes more than 30 minutes for only one select and i need to optimize as i have to run 300 selects like this one but with different SWNAMEs. I would appreciate any hints and tips that you can give me. Thank you !
SELECT SWOBJECTID
FROM MBR_INST_PRODUCTS
WHERE SWPRODRELEASEID IN
(SELECT SWPRODRELEASEID
FROM ORO_PPY_OPTIONS
WHERE SWNAME LIKE 'Nov Flexibil Offer:Net Unlimited for 1MON')
AND rownum <2;
How about a simple join?
SELECT m.swobjectid
FROM mbr_inst_products m
JOIN oro_ppy_options o ON o.swprodreleaseid = m.swprodreleaseid
WHERE o.swname = 'Nov Flexibil Offer:Net Unlimited for 1MON'
AND ROWNUM < 2;
Make sure swprodreleaseid columns are indexed.
I would write this query as:
SELECT ip.SWOBJECTID
FROM MBR_INST_PRODUCTS ip JOIN
ORO_PPY_OPTIONS po
ON po.SWPRODRELEASEID = ip.SWPRODRELEASEID
WHERE po.SWNAME = 'Nov Flexibil Offer:Net Unlimited for 1MON') AND
rownum = 1;
For this query, you want indexes on ORO_PPY_OPTIONS(SWNAME, SWPRODRELEASEID) and MBR_INST_PRODUCTS(MBR_INST_PRODUCTS).

SQL Server query issues

We have a SQL Server 2012 database with our live project including multiple tables and records, Basically We are facing a problem with SQL queries on a table where two same SQL queries. The first SQL query is taking less execution time and second SQL query is getting tremendously slow to execute.
I don't know why it is happening someone could help me to solve this problem?
Our two queries are given below....
First query (taking so much time to execute):
SELECT * FROM (SELECT TOP 10 TrackerResponse.EventName,TrackerResponse.ReceiveTime,ISNull(TrackerResponse.InputStatus,0) AS InputStatus,
TrackerResponse.Latitude,TrackerResponse.Longitude,TrackerResponse.Speed,
TrackerResponse.TrackerID,TrackerResponse.OdoMeter,TrackerResponse.Direction,
UserCar.CarNo FROM TrackerResponse
INNER JOIN UserCar ON (UserCar.TrackerID = TrackerResponse.TrackerID)
WHERE (TrackerResponse.EventName IS NOT NULL AND TrackerResponse.EventName<>'')
AND TrackerResponse.TrackerID = 112 Order By ID DESC) AS Events)
Second query (taking less execution time):
SELECT * FROM (SELECT TOP 10 TrackerResponse.EventName,TrackerResponse.ReceiveTime,ISNull(TrackerResponse.InputStatus,0) AS InputStatus,
TrackerResponse.Latitude,TrackerResponse.Longitude,TrackerResponse.Speed,
TrackerResponse.TrackerID,TrackerResponse.OdoMeter,TrackerResponse.Direction,
UserCar.CarNo FROM TrackerResponse
INNER JOIN UserCar ON (UserCar.TrackerID = TrackerResponse.TrackerID)
WHERE (TrackerResponse.EventName IS NOT NULL AND TrackerResponse.EventName<>'')
AND TrackerResponse.TrackerID = 56 Order By ID DESC) AS Events
I see Both queries are similar with an exception of tracker id..so my best guess would be ,this query might be a victim of Parameter sniffing..
Try using Option(Recompile) like below to see if this is the cause and follow article referred in above link for resolution
SELECT * FROM (SELECT TOP 10 TrackerResponse.EventName,TrackerResponse.ReceiveTime,ISNull(TrackerResponse.InputStatus,0) AS InputStatus,
TrackerResponse.Latitude,TrackerResponse.Longitude,TrackerResponse.Speed,
TrackerResponse.TrackerID,TrackerResponse.OdoMeter,TrackerResponse.Direction,
UserCar.CarNo FROM TrackerResponse
INNER JOIN UserCar ON (UserCar.TrackerID = TrackerResponse.TrackerID)
WHERE (TrackerResponse.EventName IS NOT NULL AND TrackerResponse.EventName<>'')
AND TrackerResponse.TrackerID = 56 Order By ID DESC
option(recompile)
AS Events)

Different execution time for same query - SQL Server 2008 R2

I run this query in SqlServer 2008 R2, it take about 6 seconds and return around 8000 records. OrderItemView is a view and DocumentStationHistory is a table.
SELECT o.Number, dsh.DateSend AS Expr1
FROM OrderItemView AS o INNER JOIN
DocumentStationHistory AS dsh ON dsh.DocumentStationHistoryId =
(SELECT TOP (1) DocumentStationHistoryId
FROM DocumentStationHistory AS dsh2
WHERE (o.DocumentStationId = ToStationId) AND
(DocumentId = o.id)
ORDER BY DateSend DESC)
WHERE (o.DocumentStationId = 10)
But when I run the same query with o.DocumentStationId = 8 where clause, it return around 200 records but take about 90 seconds!
Is there any idea that where is the problem?
I suppose the index is the issue, But not for o.DocumentStationId but all the fields that are joined using the field o.DocumentStationId.
try to see how your inner query is working by checking the execution plan.
that would need some performance tuning.
Also, try using index for ToStationId and DateSend. also see if you can modify inner query.
Other than these i dont see any suggestions.
Also post you execution plan
I rebuilt the index on o.DocumentStationId and the problem solved.
Try the following query. Also check if there is any index on DocumentStationId, ToStationId and DocumentId. if not create them
SELECT o.Number, dsh.DateSend AS Expr1
FROM OrderItemView AS o
OUTER APPLY
(SELECT TOP (1) DateSend
FROM DocumentStationHistory
WHERE (o.DocumentStationId = ToStationId) AND (DocumentId = o.id)
ORDER BY DateSend DESC) AS dsh
WHERE (o.DocumentStationId = 10)