sql server - Does an index already cover a clustered primary key? -
let's have table this:
create table t( [guid] [uniqueidentifier] not null, [category] [nvarchar](400) {,...other columns} )
where guid
primary key, , has clustered index.
now, want index covers both category
, guid
, because i'm rolling other stuff related t
category, , want avoid including t
table itself.
is sufficient create index covering category
, or need include guid
well?
i expect sql server indexes point directly page offsets in t
rather referring guid
primary key value, means would need explicitly include pk column avoid hitting t
. case?
actually assumption wrong - sql server non-clustered indices include clustering key (single or multiple columns) , not point directly @ physical page.
this prevents sql server having reorganize , update lots of index entries when page needs split in 2 or relocated. if seeking in non-clustered index , find value, have clustering key , sql server need "bookmark lookup" (or key lookup) retrieve actual data page (the leaf page in clustering index) whole set of data belonging single row.
that said - if ever have situation depends on ordering of key columns, still might need create index on (guid, category)
- of course, in case, sql server smart enough figure out clustering key column in index , won't adding 1 more time.
the fact clustering key column(s) inlcuded in every single non-clustered index strong reason why clustering keys should narrow, static , unique. making them wide (anything beyond 8 byte) sure recipe bloat , slow-down.
Comments
Post a Comment