Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Why would SQL Server 2005 find it more efficient to perform a table scan instead of using the available clustered index on the primary key (and only the primary key)?

DISCLAIMER: There is also a non-clustered, non-unique index on the primary key with no included columns. This is baffling to me and we've had a good office chuckle already. If this index ends up being the problem, then we know who to shoot. Unfortunately, it's a production site and I can't just rip it out but will make plans to do so if necessary.

Maybe the problem is not the mentally deficient contrary index, however...

According to Foglight PASS the following statement has been causing a scan on a table with ~10 million rows about 600 times an hour when we delete a row by the primary key:

DELETE FROM SomeBigTable WHERE ID = @ID

The table DDL:

CREATE TABLE [SomeBigTable]
(
    [ID] [int] NOT NULL,
    [FullTextIndexTime] [timestamp] NOT NULL,
    [FullTextSearchText] [varchar] (max) NOT NULL,
    CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED
    (
        [ID] ASC
    )
) -- ...
ON PRIMARY

The clustered index constraint in detail:

ADD CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED
(
    [ID] ASC 
) WITH  PAD_INDEX = OFF
       ,STATISTICS_NORECOMPUTE = OFF
       ,SORT_IN_TEMPDB = OFF
       ,IGNORE_DUP_KEY = OFF
       ,ONLINE = OFF
       ,ALLOW_ROW_LOCKS = ON
       ,ALLOW_PAGE_LOCKS = ON
       ,FILLFACTOR = 75
ON PRIMARY

The non-unique, non-clustered index on the same table:

CREATE NONCLUSTERED INDEX [IX_SomeBigTable_ID] ON [SomeBigTable]
(
    [ID] ASC
) WITH  PAD_INDEX = OFF
       ,STATISTICS_NORECOMPUTE = OFF
       ,SORT_IN_TEMPDB = OFF
       ,IGNORE_DUP_KEY = OFF
       ,ONLINE = OFF
       ,ALLOW_ROW_LOCKS = ON
       ,ALLOW_PAGE_LOCKS = ON
       ,FILLFACTOR = 98
ON PRIMARY

There is also a foreign key constraint on the [ID] column pointing to an equally large table.

The 600 table scans are about ~4% of the total delete operations per hour on this table using the same statement. So, not all executions of this statement cause a table scan.

It goes without saying, but saying it anyway...this is a lot of nasty I/O that I'd like to send packing.

share|improve this question
1  
How have you established this delete statement is causing that amount of scans? –  Martin Smith Nov 16 '12 at 20:13
    
FogLight Performance Analysis for SQL Server, I have edited the question to include. Thanks. –  Travis Royal Nov 16 '12 at 20:19
    
Can you post query plan output for the execution of the delete statement? Not that I think it matters, but why is there both a primary key constraint and index on he same column? –  Emmanuel Idi Nov 16 '12 at 22:04
    
The extra index: this is a glorious mistake. As far as the execution plan goes this will take me a while to reproduce as the customer is not actively running these statements and FogLight is unable to produce an execution plan. –  Travis Royal Nov 16 '12 at 23:45
    
you should be able to get the plan from the plan cache. –  Max Vernon Nov 17 '12 at 4:58

1 Answer 1

The optimizer may find a scan more appropriate based on statistics on the duplicate index instead of statistics on the PK. You didn't define the duplicate index as UNIQUE, so getting the "good" or "bad" plan could be just a matter of which index metadata is used by the optimizer to produce the plan. Very hard to tell without the actual execution plan, though.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.