{"id":209,"date":"2009-02-09T18:50:48","date_gmt":"2009-02-09T16:50:48","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=209"},"modified":"2011-01-07T11:41:15","modified_gmt":"2011-01-07T09:41:15","slug":"all-indexes-are-unique","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2009\/02\/09\/all-indexes-are-unique\/","title":{"rendered":"All indexes are unique"},"content":{"rendered":"<p>Well, that&#8217;s a rather contentious title. There are probably several people shaking their heads at this point. Let me explain.<\/p>\n<p>I was listening to a <a href=\"http:\/\/runasradio.com\/default.aspx?showNum=76\">podcast with Kimberly Tripp<\/a> this morning, and she mentioned this briefly. I thought it would be a good discussion to end a short series on indexes and selectivity.<\/p>\n<p><strong>The Clustered Index<\/strong><\/p>\n<p>A clustered index has to be unique, because the clustering key acts as the row&#8217;s location in the table. If the index is not defined as unique, SQL will make it unique by adding a uniquifier, a 4-byte integer that&#8217;s hidden behind the scenes and is added when necessary to make the clustered index unique.<\/p>\n<p>It&#8217;s not documented anywhere clearly, but it is mentioned in a couple of places. From <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms177484.aspx\">msdn<\/a>:<\/p>\n<blockquote><p>If the clustered index is not a unique index, SQL Server makes any duplicate keys unique by adding an internally generated value called a <strong>uniqueifier<\/strong>. This four-byte value is not visible to users. It is only added when required to make the clustered key unique for use in nonclustered indexes. SQL Server retrieves the data row by searching the clustered index using the clustered index key stored in the leaf row of the nonclustered index.<\/p><\/blockquote>\n<p>So all clustered indexes are unique.<\/p>\n<p><!--more--><strong>The Nonclustered Index<\/strong><\/p>\n<p>A nonclustered index contains, in addition to the index key and any include columns, a pointer to the actual row. This is so that the row can be retrieved when other columns are needed for a query (A <a href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2009\/01\/27\/a-bookmark-lookup-by-any-other-name\/\">bookmark lookup<\/a>)<\/p>\n<p>When the table has a clustered index, this pointer is the clustered index key. When the table does not have a clustered index, the pointer is the RID, a combination of file ID, page ID and slot index (which gives the row&#8217;s logical position on the page). These pointers are not just stored at the leaf level of the index, they&#8217;re stored at the higher levels as well, something that a bit of poking with DBCC Page can easily verify. (Unless the nonclustered index is defined unique, in which case it&#8217;s just at the lead level)<\/p>\n<p>As was proven above, the clustering key is unique. The RID, since it points to the row&#8217;s actual position, is also unique. There&#8217;s no way that two rows can be in the same place on a page.<\/p>\n<p>Hence, since part of the nonclustered index is unique, the entire index has to be unique.<\/p>\n<p>So all nonclustered indexes are also unique.<\/p>\n<p>Q.E.D.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, that&#8217;s a rather contentious title. There are probably several people shaking their heads at this point. Let me explain. I was listening to a podcast with Kimberly Tripp this morning, and she mentioned this briefly. I thought it would&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2009\/02\/09\/all-indexes-are-unique\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[24,15,16],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-indexes","category-sql-server","category-syndication"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-3n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}