我有一个大约需要 3 分钟才能运行的查询。查询用于报告,我希望它更有效。我认为瓶颈是内部连接,我可能是错的只是想知道你们是否有任何解决方案。
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)