I am using SQL Server 2008 and refuse to search on my index that spans the computed column.
My table looks like this:
CREATE TABLE Person { Id uniqueidentifier NOT NULL, InsertDate datetime NOT NULL, PhoneNumber NULL, PhoneNumberComparable AS (MakePhoneNumberComparable(PhoneNumber)) PERSISTED, ... etc... }
The ID column contains the index of the cluster primary key, as well as the index in the InsertDate column.
The calculated column PhoneNumberComparable has an index:
CREATE NONCLUSTERED INDEX IX_Person_PhoneNumberComparable ON Person ( PhoneNumberComparable ASC )
All indexes have updated statistics.
My query looks like this:
SELECT TOP 20 * FROM Person WHERE PhoneNumberComparable = @PhoneNumber ORDER BY InsertDate DESC
By default, SQL Server decides to use the index in InsertDate instead of the index on PhoneNumberComparable, which results in very poor performance.
If I try to get the phone number index to be used by adding WITH WITH (INDEX = IX_Person_PhoneNumberComparable) to the query, SQL will try to check, not search.
If I try to use the FORCESEEK query hint, then SQL Server gives me the following error:
The query processor was unable to create a query plan due to the prompts defined in this query. Repeat the query without prompting and without using SET FORCEPLAN.
So, for some reason, SQL Server refuses to search my index! Why?
EDIT
As suggested in the comments, I simplified the request, but the problem still exists (scanning the primary key is performed instead of searching by phone number index):
SELECT TOP 20 PhoneNumberComparable FROM Person WHERE PhoneNumberComparable = @PhoneNumber