I am trying to speed up my stored procedure, so I test my stored procedure in two formats using statistics io like below
Method 1: using join
set statistics io on
select top 2000
p.Vehicleno,
dbo.GetVehicleStatusIcon1(p.Direction, StatusCode, 0) as 'Status',
location,
Convert(varchar(13), p.TrackTime, 102) + ' ' + Convert(varchar(13), p.TrackTime, 108) AS 'TrackTime',
p.Speed, p.Ignition
from
pollingdata p
inner join
assignvehicletouser asn on asn.vehicleno = p.vehicleno
where
asn.empid = 1
I get statistics result as
Table 'Worktable'. Scan count 943, logical reads 7671, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'AssignVehicleToUser'. Scan count 1, logical reads 41, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'PollingData'. Scan count 1, logical reads 50, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Method 2: using where clause sub-query
set statistics io on
select top 2000
Vehicleno,
dbo.GetVehicleStatusIcon1(Direction,StatusCode, 0) as 'Status',
location,
Convert(varchar(13), TrackTime, 102) + ' ' + Convert(varchar(13), TrackTime, 108) AS 'TrackTime',
Speed, Ignition
from
pollingdata
where
vehicleno in (select vehicleno
from assignvehicletouser
where empid = 1)
I get statistics result as
Table 'PollingData'. Scan count 1, logical reads 50, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'AssignVehicleToUser'. Scan count 1, logical reads 41, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Need to know which one is best to use?
Need explanation about how logical reads works here?
If you don't need anything from
assignvehicletouser
table, I'd preferEXISTS
(It probably works same way asIN
)