两个SQL结果相同性能差别10000倍
SQL1是两个SQL语句,SQL2是一个SQL语句,系统某些限制只能用第二种模式,SQL2是否有改进的空间?
rt_TestData_SWG 170GB(数据和索引总计)
rt_TestRlt 17GB(数据和索引总计)
rt_prdSNTrace 12GB(数据和索引总计)
SQL1(执行时间1秒):
declare @table table(PrdID varchar(50)) insert into @table select PrdID from rt_prdsntrace with(nolock) where sbr='SBRE11s-4T'
SELECT PrdSN as SERIAL_NUM,paraSpecial1 AS hdPos,inDTime AS testTime,
ISNULL(MAX(CASE WHEN ParamName='EVENT_STATUS' THEN ParamValue ELSE NULL END),'NULL') AS EVENT_STATUS
FROM (SELECT rt_prdSNTrace.PrdSN,
rt_TestRlt.inDTime,rt_TestData.paraSpecial1,
rt_TestData.ParamName,rt_TestData.ParamValue
FROM rt_prdSNTrace WITH(NOLOCK)
join @table xx on xx.PrdID=rt_prdSNTrace.PrdID
JOIN rt_TestRlt WITH(NOLOCK) ON rt_prdSNTrace.PrdID = rt_TestRlt.PrdID
JOIN rt_TestData_SWG AS rt_TestData WITH(NOLOCK) ON rt_TestRlt.TestTraceID = rt_TestData.TestID
WHERE rt_TestRlt.ProcessSiteNo='SWG'
) AS TABLE_TMP
GROUP BY PrdSN,paraSpecial1,inDTime
SQL2(执行时间13分钟):
SELECT PrdSN as SERIAL_NUM,paraSpecial1 AS hdPos,inDTime AS testTime,
ISNULL(MAX(CASE WHEN ParamName='EVENT_STATUS' THEN ParamValue ELSE NULL END),'NULL') AS EVENT_STATUS
FROM (SELECT rt_prdSNTrace.PrdSN,rt_TestRlt.inDTime,rt_TestData.paraSpecial1,rt_TestData.ParamName,rt_TestData.ParamValue
FROM rt_prdSNTrace WITH(NOLOCK)
JOIN rt_TestRlt WITH(NOLOCK) ON rt_prdSNTrace.PrdID = rt_TestRlt.PrdID
JOIN rt_TestData_SWG AS rt_TestData WITH(NOLOCK) ON rt_TestRlt.TestTraceID = rt_TestData.TestID
WHERE rt_TestRlt.ProcessSiteNo='SWG' and sbr='SBRE11s-4T'
) AS TABLE_TMP
GROUP BY PrdSN,paraSpecial1,inDTime
------解决方案--------------------把需要返回的字段列出来,再不行的话LZ可以私信找我,远程帮你看看..