views:

228

answers:

4

When I run SQL Server 2005 Database Tuning Advisor, it gives a recommendation to create an index, but it will recommends to index a column which already has an index on it. Why does it give a recommendation to create the same index again?

Here is my SQL:

SELECT t.name AS 'affected_table'
  , 'Create NonClustered Index IX_' + t.name + '_' 
   + CAST(ddmid.index_handle AS VARCHAR(10))
   + ' On ' + ddmid.STATEMENT 
   + ' (' + IsNull(ddmid.equality_columns,'') 
   + CASE 
     WHEN ddmid.equality_columns IS NOT NULL 
          AND ddmid.inequality_columns IS NOT NULL
     THEN ',' 
     ELSE '' 
     END 
   + ISNULL(ddmid.inequality_columns, '')
   + ')' 
   + ISNULL(' Include (' + ddmid.included_columns + ');', ';') 
  AS sql_statement
  , ddmigs.user_seeks
  , ddmigs.user_scans
  , CAST((ddmigs.user_seeks + ddmigs.user_scans) 
    * ddmigs.avg_user_impact AS INT) AS 'est_impact'
  , ddmigs.last_user_seek
FROM 
  sys.dm_db_missing_index_groups AS ddmig
  INNER JOIN sys.dm_db_missing_index_group_stats AS ddmigs
    ON ddmigs.group_handle = ddmig.index_group_handle
  INNER JOIN sys.dm_db_missing_index_details AS ddmid 
    ON ddmig.index_handle = ddmid.index_handle
  INNER Join sys.tables AS t
    ON ddmid.OBJECT_ID = t.OBJECT_ID
WHERE 
  ddmid.database_id = DB_ID()
  AND CAST((ddmigs.user_seeks + ddmigs.user_scans) 
  * ddmigs.avg_user_impact AS INT) > 100
ORDER BY 
  CAST((ddmigs.user_seeks + ddmigs.user_scans) 
  * ddmigs.avg_user_impact AS INT) DESC;
A: 

You may need to run your queries and suggest the index that is already there.

SELECT * FROM table WITH INDEX(IX_INDEX_SHOULD_BE_USED) WHERE x = y

The index that is there might not be thought of as useful from SQL Server. Run the query that is suggesting the need for the index and see the execution path in SQL Server and then build other indexes that are needed.

AutomatedTester
I don't think the poster is saying his query doesn't use the index. He's saying that sql server is suggesting that he generates an index against a particular table. Problem is, there's already an index on that table, against those fields (he believes).
Pure.Krome
If he isn't specifically specifying the index SQL server will try use the best index it feels for the item. It might be there but SQL server is ignoring it so then SQL server thinks it needs the same index.
AutomatedTester
A: 

Can u please list the full index missing warning message? generally, it's asking to create an index on the table BUT only to return certain fields, instead of an index on the table, which will return all fields by default.

Pure.Krome
A: 

Perhaps try "DESC" to order a different way?

This worked in another similar SO question... Why does SQL Server 2005 Dynamic Management View report a missing index when it is not?

gbn
Why has this been marked down? An index with the same columns but a different sort order is considered a "different" index by SQL Server. This is a possible solution, the original poster needs to provide both the current index definition and those suggested by the DTA.
John Sansom
@John: I have 5 serial down votes for my last 5 answers... I seem to upset someone :-)
gbn
+1 @gbn: There's just no pleasing some people.
John Sansom
A: 

Go ahead and script out the details of both your current index strucutre and then compare this to reccomendations made by the DTA.

I suspect that you will find there are structural differences in the results.

John Sansom