I have a query that takes about 3 minutes to run. The Query is being used for reporting and i would like it to be more efficient. I think the bottle neck is the inner joins, I could be wrong just want to know if you guys had any solutions.
SELECT DISTINCT
SUBSTRING(T1.DateTime, 1, 4) AS Year,
SUBSTRING(T1.DateTime, 5, 2) AS Month,
SUBSTRING(T1.DateTime, 7, 2) AS Day,
T1.PipeNr, T1.SalesOrder, T1.JobNr, SIST.DefectCode AS DefectRef,
DEFCODES.DefectCode, DEFCODES.DefectDesc, SIST.ODYes, SIST.LocationWrap,
T1.OWS0601, T1.OWS0602, T1.OWS0603, T1.CrossWeld
FROM PIPEDB.dbo.SIST INNER JOIN PIPEDB.dbo.MPIPEID T1 ON PIPEDB.dbo.SIST.PipeNr = T1.PipeNr INNER JOIN
PIPEDB.dbo.DEFCODES ON PIPEDB.dbo.SIST.DefectCode = PIPEDB.dbo.DEFCODES.DefectRef
WHERE PIPEDB.dbo.SIST.DefectCode
IN (
SELECT Top (10) PIPEDB.dbo.SIST.DefectCode
FROM PIPEDB.dbo.SIST INNER JOIN PIPEDB.dbo.MPIPEID T2 ON PIPEDB.dbo.SIST.PipeNr = T2.PipeNr INNER JOIN
PIPEDB.dbo.DEFCODES ON PIPEDB.dbo.SIST.DefectCode = PIPEDB.dbo.DEFCODES.DefectRef
WHERE SUBSTRING(T2.DateTime, 1, 4) = SUBSTRING(T1.DateTime, 1, 4) AND SUBSTRING(T2.DateTime, 5, 2) = SUBSTRING(T1.DateTime, 5, 2) AND
SUBSTRING(T2.DateTime, 7, 2) = SUBSTRING(T1.DateTime, 7, 2)
GROUP BY PIPEDB.dbo.SIST.DefectCode
ORDER BY COUNT(PIPEDB.dbo.SIST.PipeNr) DESC)
AND (PIPEDB.dbo.DEFCODES.DefectDesc IN ("Cut To Remove Defect")) AND
((CASE WHEN T1.CrossWeld = 1 THEN 1 WHEN T1.CrossWeld = 0 THEN 2 END) = @Crossweld OR @Crossweld = 0)
The issue is that you are making IN subquery as a correlated subquery which in turn is causing the performance issue.For each rows from the main clause (or outer from) the IN subquery is being executed and thus it is taking time.Each execution of Inner subquery could be expensive based on size of tables and most probably it will use a nested loop join for in subquery. Also if number of rows from outer query are quite high then the expensive IN subquery is being executed lots of time and thus degrading the performance of query.I would suggest that have a look at the logic once again and make sure that it is giving correct data.
See below.I have tried to reproduce your issue.The cost for this query is 2045 which is very huge and off course it is going to take time. It clocks around 10 million io’s 176 sec of CPU and 48 seconds elapsed time.
That’s not it.I expected to have just 10 productid(I am sure you alos expect to have just 10 defectcodes) but i could see lots of other productids just because of the correlated subquery.
)