The following query uses an index seek on an index on the LastModifiedTime column.
SELECT
CONVERT(varchar, a.ReadTime, 101) as ReadDate,
a.SubID,
a.PlantID,
a.Unit as UnitID,
a.SubAssembly
FROM dbo.Accepts a WITH (NOLOCK)
WHERE a.LastModifiedTime BETWEEN '3/3/2010' And '3/4/2010'
AND a.SubAssembly = '400'
The query below, which is almost identical to the above query, uses a clustered index scan, instead of the index on LastModifiedTime. Can anyone tell me why? And, more importantly, what I can do to get SQL Server to use the index on the LastModifiedTime column, without using an index hint.
Declare @LastModifiedTimeEnd dateTime
Declare @LastModifiedTimeStart dateTime
SELECT
CONVERT(varchar, a.ReadTime, 101) as ReadDate,
a.SubID,
a.PlantID,
a.Unit as UnitID,
a.SubAssembly
FROM dbo.Accepts a WITH (NOLOCK)
WHERE a.LastModifiedTime BETWEEN @LastModifiedTimeStart And @LastModifiedTimeEnd
AND a.SubAssembly = '400'
3) index scan is faster than a table scan because they look at sorted data and query optimizers know when to stop and look for another range. 4) index seek is the fastest way to retrieve data and it comes into the picture when your search criterion is very specific.
Table Scan vs. For several reasons, the clustered index usually performs better than the nonclustered index. When the system scans a clustered index, it doesn't need to leave the b-tree structure to scan data pages because the pages already exist at the leaf level.
you've requested rows directly in the query that's why you got a clustered index SEEK . Clustered index scan: When Sql server reads through for the Row(s) from top to bottom in the clustered index. for example searching data in non key column.
If the table is very large, an index seek will probably be chosen. However, if the table is small, then the optimizer might decide that a table scan is still faster, since some overhead is required to access an index.
The query below, which is almost identical to the above query, uses a clustered index scan, instead of the index on
LastModifiedTime
. Can anyone tell me why?
The query below does not know the values of the parameters when building the plan and assumes that in general, the clustered index scan is better.
And, more importantly, what I can do to get SQL Server to use the index on the
LastModifiedTime
column, without using an index hint.
SELECT
CONVERT(varchar, a.ReadTime, 101) as ReadDate,
a.SubID,
a.PlantID,
a.Unit as UnitID,
a.SubAssembly
FROM dbo.Accepts a WITH (NOLOCK)
WHERE a.LastModifiedTime BETWEEN @LastModifiedTimeStart And @LastModifiedTimeEnd
AND a.SubAssembly = '400'
OPTION (OPTIMIZE FOR (@LastModifiedTimeStart = '3/3/2010', @LastModifiedTimeEnd = '3/4/2010'))
Alternatively, you can add OPTION (RECOMPILE)
, which will create the different execution plan each time the query is run, taking the parameter values into the account (parameter sniffing).
This, however, does not guarantee that the index will be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With