patternsqlMinor
Why don't I get an index seek?
Viewed 0 times
whyseekgetindexdon
Problem
Working on SQL Server 2014 and trying to optimize a query. One portion of it is doing a table scan. I distilled the issue down to the simplest form.
There is a field in a table (3rd party product's DB) that has no index and no PK. We frequently search on that field (
The exec plan recommends this:
I create the index and the warning goes away, BUT I'm still getting a table scan. There are 95K records in the table and this query finds 860 rows.
The predicate says:
I even tried casting the string to a uniqueidentifier:
Can anyone explain why I'm still getting a table scan?
My understanding is that it's using the index in the
QueryPlan XML
`
There is a field in a table (3rd party product's DB) that has no index and no PK. We frequently search on that field (
Form_FKey)SELECT load_ID FROM [dbo].[tblLoads]
WHERE [Form_FKey] = '87F13E42-B11D-413B-AEBC-A58E4CDB9D3E'The exec plan recommends this:
CREATE NONCLUSTERED INDEX []
ON [dbo].[tblLoads] ([Form_FKey])I create the index and the warning goes away, BUT I'm still getting a table scan. There are 95K records in the table and this query finds 860 rows.
The predicate says:
[Scale_WH].[dbo].[tblLoads].[Form_FKey]={guid'87F13E42-B11D-413B-AEBC-
A58E4CDB9D3E'}I even tried casting the string to a uniqueidentifier:
SELECT load_ID FROM [dbo].[tblLoads]
WHERE [Form_FKey] = CAST('87F13E42-B11D-413B-AEBC-A58E4CDB9D3E' AS
UNIQUEIDENTIFIER)Can anyone explain why I'm still getting a table scan?
loadid is unique. and is in fact the primary key on the table (BUT no primary key is defined. it's a 3rd party vendor's database so we have to be very careful about modifying the schema). I'm trying to learn more about tuning and this is an example. Assume for this case that it's just a regular column.My understanding is that it's using the index in the
WHERE clause to identify the row, it will then go directly to the row to get other columns not in the index, so the columns in select don't affect whether it does a seek or not. I could be wrong.QueryPlan XML
`
Solution
Your new index isn't covering the query as it's missing the
My understanding is that it's using the index in the
It can do that and execute a row lookup after a Seek operation is performed, but that's never guaranteed. Aaron's alluded to the tipping point, and as explained by Kimberly Tripp in the
load_ID column from the INCLUDE statement (which incidentally is also missing from the index definition). Recreate the index including the additional column and see if you get the expected behavior.My understanding is that it's using the index in the
WHERE clause to identify the row, it will then go directly to the row to get other columns not in the index, so the columns in select don't affect whether it does a seek or not. I could be wrong.It can do that and execute a row lookup after a Seek operation is performed, but that's never guaranteed. Aaron's alluded to the tipping point, and as explained by Kimberly Tripp in the
Why is the tipping point interesting? section, "Narrow (non-covering) nonclustered indexes have fewer uses than often expected (just because a query has a column in the WHERE clause doesn't mean that SQL Server's going to use that index)"Context
StackExchange Database Administrators Q#176763, answer score: 4
Revisions (0)
No revisions yet.