Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Do I need to INCLUDE the primary key in an index or not?

$
0
0

Dear collegues,

The INCLUDE syntax in indexes is new to me. If I understand the online help correctly, every index will automatically include the clustered index. So if I have a table with a foreign key constraint and a datetime2 field, and if I know that including the (clustered) primary key and the datetime2 field in the index will help performance, do I need to include the primary key field in my code, or will it be done behind the scenes anyway?

To be a bit more precise, given the table definition below, do I need to create my index as in 1 or as in 2?

  1. CREATE INDEX IX_NU_DossierAnalysis$DossierCode
    ON dbo.DossierAnalysis (DossierCode) INCLUDE (Id, CreDateTime);
  2. CREATE INDEX IX_NU_DossierAnalysis$DossierCode
    ON dbo.DossierAnalysis (DossierCode) INCLUDE (CreDateTime);

CREATE TABLE dbo.DossierAnalysis
  ( Id				bigint		IDENTITY(1,1) NOT NULL
  , DossierCode	int			NOT NULL
  , CreDateTime	datetime2(3)	NOT NULL
  , TotalCost		numeric(12,2)	NOT NULL
  );
ALTER TABLE dbo.DossierAnalysis
  ADD CONSTRAINT PK_DossierAnalysis PRIMARY KEY CLUSTERED (Id);
GO


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>