使用 SSMS,我如何确定哪个查询更有效?我更喜欢 A,但我被告知子查询对传输中的每一行执行一次,因此首选 B。
一个
Update t set t.transmission_status_id =
(select transmission_status_id from transmission_status where code = 'TRANSLATED')
from transmission t
where t.transmission_status_id =
(select transmission_status_id from transmission_status where code = 'RECEIVED')
乙
declare @transmission_status_TRANSLATED INT = (select transmission_status_id from transmission_status where code = 'TRANSLATED')
declare @transmision_status_RECEIVED INT = (select transmission_status_id from transmission_status where code = 'RECEIVED')
Update t set t.transmission_status_id = @transmission_status_TRANSLATED
from transmission t
where t.transmission_status_id = @transmision_status_RECEIVED
编辑:这是使用 SET STATISTICS ON 的统计信息:
一个。
Table 'transmission_status'. Scan count 1, logical reads 2, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'transmission_status'. Scan count 1, logical reads 2, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'transmission'. Scan count 1, logical reads 778, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
乙
Table 'transmission'. Scan count 1, logical reads 778, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'transmission_status'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
因此,据我所知,效率没有区别。
编辑2:所以我现在明白了:由于子查询不是correlated
子查询,它只执行一次。https://en.wikipedia.org/wiki/Correlated_subquery(感谢@destination_data 提供链接。)