Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a normal SQL Server Index include Primary Key value? [duplicate]

I'm creating a lookup table to be used for data processing. The structure is rather simple.

The first field will be the primary key and clustered index. There will be a second field that will track the items type.

We would like an index on that second field since some (but not all queries) will be searching on that field. Those queries would also need to pull the value of the primary key.

I would like the queries that utilize the non-clustered index to not have to go to the table and pull the primary key value.

So my question is do normal SQL Server indexes have the primary key value on them or do they just have a pointer to the clustered index (for my purposes here I'm just assuming the primary key and the clustered index are coextensive)? If it only contains a pointer to disk and not the primary key value, I was thinking about making the first column (the primary key) an include column on the normal index based on the second column.

like image 310
geoffrobinson Avatar asked Dec 01 '25 02:12

geoffrobinson


2 Answers

To quote the docs for CREATE INDEX :

Nonclustered indexes always contain the clustered index columns if a clustered index is defined on the table.

like image 172
AakashM Avatar answered Dec 02 '25 18:12

AakashM


They don't necessarily have the PK columns in them, but the key columns of the clustered index. They are indeed used as a pointer to the CI row, but also usable in other types of queries.

For example an index on column A (and not explicitly on ID) can satisfy the query select A, ID from T.

like image 30
usr Avatar answered Dec 02 '25 19:12

usr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!