我不是那么高级的sql用户。 您能否回顾一下我的以下查询是否最佳?或者我可以做一些更优化和更具可读性的事情?
select Distinct DT.Station , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
from (
select Station, Slot, SubSlot, CompID
from DeTrace
where DeviceID = '1151579773'
) as DT
Left outer CList as CL
on DT.CompID = CL.CompID
where CL.CompName = '9234220'
order by CompName
感谢您的帮助。
答案 0 :(得分:2)
这样更容易阅读:
select Distinct DT.Station , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
from DeTrace DT
Left outer join CList as CL
on DT.CompID = CL.CompID
where CL.CompName = '9234220'
and DT.DeviceID = '1151579773'
order by CompName
优化器应该能够像您一样高效地执行此查询,但您应该检查查询执行计划以确保。
答案 1 :(得分:0)
为什么不呢:
SELECT DISTINCT DT.Station , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
FROM DeTrace DT
LEFT OUTER JOIN CList CL ON DT.CompID = CL.CompID
AND DT.DeviceID = '1151579773'
AND CL.CompName = '9234220'
ORDER BY CL.CompName
答案 2 :(得分:0)
我认为你不需要子查询。检查以下内容:
select Distinct DT.Station , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
from DeTrace as DT
Left outer CList as CL
on DT.CompID = CL.CompID
where CL.CompName = '9234220' and DeviceID = '1151579773'
order by CompName
答案 3 :(得分:0)
我最喜欢这种方式:
SELECT DISTINCT
DeTrace.Station,
DeTrace.Slot,
DeTrace.SubSlot,
DeTrace.CompID,
CL.CompName
FROM
DeTrace
LEFT OUTER JOIN
CList AS CL ON
DeTrace.CompID = CL.CompID
WHERE
DeviceID = '1151579773' AND
CL.CompName = '9234220'
ORDER BY
CL.CompName
答案 4 :(得分:0)
select Distinct DT.Station , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
from DeTrace DT
Left outer join CList as CL
on DT.CompID = CL.CompID
where DT.DeviceID = '1151579773'
and CL.CompName = '9234220'
order by CL.CompName
基于Sql Server Cost的优化程序应该能够确定应用条件的最有效顺序。