我有一个非常脏的查询,确保可以优化,因为它有很多CASE语句!
SELECT
(CASE pa.KplusTable_Id WHEN 1 THEN sp.sp_id
WHEN 2 THEN fw.fw_id
WHEN 3 THEN s.sw_Id
WHEN 4 THEN id.ia_id END) as Deal_Id,
max(CASE pa.KplusTable_Id WHEN 1 THEN sp.Trans_Id
WHEN 2 THEN fw.Trans_Id
WHEN 3 THEN s.Trans_Id
WHEN 4 THEN id.Trans_Id END) as TransId_CurrentMax
INTO #MaxRazlicitOdNull
FROM #PotencijalniAktuelni pa LEFT JOIN kplus_sp sp (nolock) on sp.sp_id=pa.Deal_Id AND pa.KplusTable_Id=1
LEFT JOIN kplus_fw fw (nolock) on fw.fw_id=pa.Deal_Id AND pa.KplusTable_Id=2
LEFT JOIN dev_sw s (nolock) on s.sw_Id=pa.Deal_Id AND pa.KplusTable_Id=3
LEFT JOIN kplus_ia id (nolock) on id.ia_id=pa.Deal_Id AND pa.KplusTable_Id=4
WHERE isnull(CASE pa.KplusTable_Id WHEN 1 THEN sp.BROJ_TIKETA
WHEN 2 THEN fw.BROJ_TIKETA
WHEN 3 THEN s.tiket
WHEN 4 THEN id.BROJ_TIKETA END, '')<>''
GROUP BY CASE pa.KplusTable_Id WHEN 1 THEN sp.sp_id
WHEN 2 THEN fw.fw_id
WHEN 3 THEN s.sw_Id
WHEN 4 THEN id.ia_id END
因为我有几次相同的条件,你是否知道如何优化查询,使其更简单和更好。欢迎所有建议!
提前TnX!维迪奇
答案 0 :(得分:4)
对我而言,这看起来像是一种拙劣的子类型尝试。这就是我认为你现在所拥有的。
根据模型,以下内容应该有效:
;
with
q_00 as (
select
pa.Deal_Id as Deal_Id
, coalesce(sp.BROJ_TIKETA, fw.BROJ_TIKETA, sw.tiket, ia.BROJ_TIKETA, '') as Ticket_No
, coalesce(sp.Trans_Id, fw.Trans_Id, sw.Trans_Id, ia.Trans_Id) as Trans_Id
from #PotencijalniAktuelni as pa
left join kplus_sp as sp on sp.sp_Id = pa.Deal_Id and pa.KplusTable_Id = 1
left join kplus_fw as fw on fw.fw_Id = pa.Deal_Id and pa.KplusTable_Id = 2
left join dev_sw as sw on sw.sw_Id = pa.Deal_Id and pa.KplusTable_Id = 3
left join kplus_ia as ia on ia.ia_Id = pa.Deal_Id and pa.KplusTable_Id = 4
)
select
Deal_Id
, max(Trans_Id) as TransId_CurrentMax
into #MaxRazlicitOdNull
from q_00
where Ticket_No <> ''
group by Deal_Id ;
SQL Server 2005 +
答案 1 :(得分:2)
最快的查询可能是将4个子句中的每个子句合并在一起并将它们组合在一起。代码结束的时间越长,但每个块的作用要清楚得多 - 特别是如果你一起评论它们。
-- When KplusTable_Id = 1
Select
sp.sp_id as as Deal_Id,
max(sp.Trans_Id) as TransId_CurrentMax
FROM #PotencijalniAktuelni pa LEFT JOIN kplus_sp sp (nolock) on sp.sp_id=pa.Deal_Id AND pa.KplusTable_Id=1
LEFT JOIN kplus_fw fw (nolock) on fw.fw_id=pa.Deal_Id AND pa.KplusTable_Id=2
LEFT JOIN dev_sw s (nolock) on s.sw_Id=pa.Deal_Id AND pa.KplusTable_Id=3
LEFT JOIN kplus_ia id (nolock) on id.ia_id=pa.Deal_Id AND pa.KplusTable_Id=4
WHERE sp.BROJ_TIKETA <>''
and pa.KplusTable_Id = 1
GROUP BY sp.sp_id
Union ...
-- When 2
将整个查询包装在一个select中,以插入#MaxRazlicitOdNull
答案 2 :(得分:1)
案例对我来说没问题。通常比联盟更快。 放置查询的多个变体并在Plan中比较批处理。 只有一个(无关紧要的)细节 改变
WHERE isnull(CASE pa.KplusTable_Id WHEN 1 THEN sp.BROJ_TIKETA
WHEN 2 THEN fw.BROJ_TIKETA
WHEN 3 THEN s.tiket
WHEN 4 THEN id.BROJ_TIKETA END, '')<>''
这个
WHERE CASE pa.KplusTable_Id WHEN 1 THEN sp.BROJ_TIKETA
WHEN 2 THEN fw.BROJ_TIKETA
WHEN 3 THEN s.tiket
WHEN 4 THEN id.BROJ_TIKETA END is not null
另一个sol(带UNION):
SELECT pa.Deal_Id, MAX(Q.Trans_Id) AS TransId_CurrentMax
INTO #MaxRazlicitOdNull
FROM
(SELECT 1 A KplusTable_Id, Trans_Id, sp_id AS Deal_Id FROM kplus_sp
UNION
SELECT 2 AS KplusTable_Id, Trans_Id, fw_id AS Deal_Id FROM kplus_fw
UNION
SELECT 3 AS KplusTable_Id, Trans_Id, sw_Id AS Deal_Id FROM dev_sw
UNION
SELECT 4 AS KplusTable_Id, Trans_Id, ia_id AS Deal_Id FROM kplus_ia) AS Q
INNER JOIN #PotencijalniAktuelni pa ON pa.KplusTable_Id=Q.KplusTable_Id AND pa.Deal_Id=Q.Deal_Id
GROUP BY pa.Deal_Id
在计划中测试查询的每个变体并选择更快