【发布时间】:2011-10-26 01:38:53
【问题描述】:
我根据建议修改了代码,但现在性能下降了。欢迎任何建议
select *
from (
SELECT
d.ID,
d.HeaderId,
CASE WHEN h.MyType = 'C' THEN
RANK() OVER (PARTITION BY l.Work ORDER BY l.Address1 DESC)
ELSE
RANK() OVER (PARTITION BY l.Home ORDER BY l.Address1 DESC)
END
AS 'RANK',
CASE WHEN h.MyType = 'C' THEN
COUNT(l.Work) OVER (PARTITION BY l.Work)
ELSE
COUNT(l.Home) OVER (PARTITION BY l.Home)
END
AS 'MAXCOUNT'
FROM schema1.Details AS d
JOIN schema1.BatchHeader AS h
ON d.HeaderId = h.ID
JOIN schema2.Details AS l
ON d.LeadBatchDetailId = l.Id
LEFT JOIN LDCs AS ldcElec
ON l.LDC_Elec = schema3.Code
LEFT JOIN LDCs AS ldcGas
ON l.ldc_gas = ldcGas.Code
LEFT JOIN schema2.Accounts ag
ON (l.Work = ag.Phone AND 'G' = ag.Business AND h.MyType = 'C')
OR (l.Home = ag.Phone AND 'G' = ag.Business AND h.MyType = 'R')
LEFT JOIN schema2.Accounts ae
ON (l.Work = ae.Phone AND 'E' = ae.Business AND h.MyType = 'C')
OR (l.Home = ae.Phone AND 'E' = ae.Business AND h.MyType = 'R')
WHERE d.HeaderId = @Id)
) a
WHERE [RANK] = [MAXCOUNT]
ORDER BY LdcGasName, LdcElecName
【问题讨论】:
-
只是一个想法,应该是CASE语句吗? CASE WHEN h.MyType = 'C' THEN RANK() OVER ... ELSE .. another RANK() END AS 'RANK'
-
案例工作,但现在很慢
-
很难从一个查询中知道要优化哪个部分。你有没有解释清楚查询中最昂贵的部分在哪里?
标签: sql sql-server sql-server-2005 tsql