2

Looking over on of our older databases I noticed that several large tables had duplicate indexes on the Primary Key.

Both indexes are Non-clustered, Unique and only includes the PK-column. The clustered index is on a different column

Below is the SQL to create the indexes

ALTER TABLE [dbo].[TableName] ADD  CONSTRAINT [PK_TableName_1] PRIMARY KEY NONCLUSTERED 
([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) 
ON [PRIMARY] 

and

CREATE UNIQUE NONCLUSTERED INDEX [PK_TableName] ON [dbo].[TableName] 
( [id] ASC )
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [FGIndex]

Both these statements will create a index that to my eyes looks like duplicates. The PK-index will be indicated with the "yellow key" in the indexlist in SSMS.

Is it safe to drop the the index? According to statistics the 2nd index is often the one that the Query Plan Engine chooses. The second index also uses less space.

Thanks for any input.

3
  • 1
    No index hints are used. Commented Feb 13, 2015 at 8:47
  • 1
    @KennyEliasson, verify foreign keys that reference the table are bound to the primary key index rather than the unique non-clustered index. See dbdelta.com/secrets-of-foreign-key-index-binding for a script.
    – Dan Guzman
    Commented Feb 14, 2015 at 14:15
  • 1
    @DanGuzman This particular database doesnt have any foreign keys ( yes it's true :( ) Commented Feb 16, 2015 at 8:16

1 Answer 1

3

I think it's safe to remove the non-primary key as it's an exact duplicate of the primary key.

If one of them is used more and takes less space, did you check the fragmentation and last statistics date on both? Maybe one of them is more fragmented or has better stats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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