Search code examples
sql-serverindexingsql-server-2008-r2sql-execution-plannon-clustered-index

execution plan suggesting to add an index on columns which are not part of where clause


I am running following query in SSMS and execution plan suggesting to add index on columns which are not part of where clause. I was planning to add index on two columns which are being used in where clause (OID and TransactionDate).

SELECT  
        [OID] , //this is not a PK. Primary key column is not a part of sql script
        [CustomerNum] ,
        [Amount] ,
        [TransactionDate] ,
        [CreatedDate] 

FROM    [dbo].[Transaction]
WHERE   OID = 489
        AND TransactionDate > '01/01/2018 06:13:06.46';

Index suggestion

CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[Transaction] ([OID],[TransactionDate])
INCLUDE ([CustomerNum],[Amount],[CreatedDate])

Updated

Do i need to include other columns? Data is being imported to that table through a back end process using SQLBulkCopy class in .net. I am wondering if having non cluster index on all columns would reduce the performance. (In my table is Pk column called TransactionID which is not needed but i have this in the table in case its needed in the future otherwise SQLBulkCopy works better with heap. Other option is to drop and recreate indexes before and after SQLBulkCopy operation)

enter image description here


Solution

  • the INCLUDE keyword specifies the non-key columns to be added to the leaf level of the nonclustered index.

    This means that if you will add this index and run the query again, SQL Server can get all the information needed from the index, thus eliminating the need to perform a lookup in the table as well.

    As a general rule of thumb - when SSMS suggest an index, create it. You can always drop it later if it doesn't help.