{"id":1032,"date":"2019-08-08T06:00:31","date_gmt":"2019-08-08T13:00:31","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/erin\/?p=1032"},"modified":"2019-08-08T06:26:07","modified_gmt":"2019-08-08T13:26:07","slug":"what-columns-are-in-that-index","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/","title":{"rendered":"What columns are in that index?"},"content":{"rendered":"<p>I\u2019ve been helping several customers with tuning lately, which typically means I\u2019m also looking at indexes and asking questions. A pattern I\u2019ve noticed is columns from the clustering key being added to the key definition for the nonclustered index.\u00a0 This brings up some interesting considerations when it comes to creating your nonclustered indexes.<\/p>\n<h1>Review<\/h1>\n<p>We\u2019re going to start with a copy of WideWorldImporters, and create a table and indexes from it.<br \/>\nNotice that I\u2019m adding a column to this table named OrderID_and_OrderLineID, which is the OrderID and the OrderLineID values concatenated. In Sales.OrderLines, OrderLine is unique, but OrderID is not. The two of them concatenated are unique, and I made this my primary key, as well as my clustering key (remember, you can have a primary key that\u2019s different than your clustering key, which is a separate topic for another day).<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUSE &#x5B;WideWorldImporters]\r\nGO\r\n\r\nDROP TABLE IF EXISTS &#x5B;Sales].&#x5B;SQLskills_OrderLines];\r\nGO\r\n\r\nCREATE TABLE &#x5B;Sales].&#x5B;SQLskills_OrderLines](\r\n     &#x5B;OrderLineID] &#x5B;int] NOT NULL,\r\n     &#x5B;OrderID] &#x5B;int] NOT NULL,\r\n     &#x5B;OrderID_and_OrderLineID] &#x5B;bigint] NOT NULL,\r\n     &#x5B;StockItemID] &#x5B;int] NOT NULL,\r\n     &#x5B;Description] &#x5B;nvarchar](100) NOT NULL,\r\n     &#x5B;PackageTypeID] &#x5B;int] NOT NULL,\r\n     &#x5B;Quantity] &#x5B;int] NOT NULL,\r\n     &#x5B;UnitPrice] &#x5B;decimal](18, 2) NULL,\r\n     &#x5B;TaxRate] &#x5B;decimal](18, 3) NOT NULL,\r\n     &#x5B;PickedQuantity] &#x5B;int] NOT NULL,\r\n     &#x5B;PickingCompletedWhen] &#x5B;datetime2](7) NULL,\r\n     &#x5B;LastEditedBy] &#x5B;int] NOT NULL,\r\n     &#x5B;LastEditedWhen] &#x5B;datetime2](7) NOT NULL,\r\n     CONSTRAINT &#x5B;PK_Sales_SQLskills_OrderLines] PRIMARY KEY CLUSTERED\r\n     (\r\n          &#x5B;OrderID_and_OrderLineID] ASC\r\n     )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, \r\n     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON &#x5B;USERDATA]\r\n     ) ON &#x5B;USERDATA];\r\nGO\r\n\r\nINSERT INTO &#x5B;Sales].&#x5B;SQLskills_OrderLines](\r\n     &#x5B;OrderLineID]\r\n     ,&#x5B;OrderID]\r\n     ,&#x5B;OrderID_and_OrderLineID]\r\n     ,&#x5B;StockItemID]\r\n     ,&#x5B;Description]\r\n     ,&#x5B;PackageTypeID]\r\n     ,&#x5B;Quantity]\r\n     ,&#x5B;UnitPrice]\r\n     ,&#x5B;TaxRate]\r\n     ,&#x5B;PickedQuantity]\r\n     ,&#x5B;PickingCompletedWhen]\r\n     ,&#x5B;LastEditedBy]\r\n     ,&#x5B;LastEditedWhen]\r\n     )\r\nSELECT\r\n     &#x5B;OrderLineID]\r\n     ,&#x5B;OrderID]\r\n     ,CAST(CAST(&#x5B;OrderID] AS VARCHAR(20)) + CAST(&#x5B;OrderLineID] AS VARCHAR(20)) AS BIGINT)\r\n     ,&#x5B;StockItemID]\r\n     ,&#x5B;Description]\r\n     ,&#x5B;PackageTypeID]\r\n     ,&#x5B;Quantity]\r\n     ,&#x5B;UnitPrice]\r\n     ,&#x5B;TaxRate]\r\n     ,&#x5B;PickedQuantity]\r\n     ,&#x5B;PickingCompletedWhen]\r\n     ,&#x5B;LastEditedBy]\r\n     ,&#x5B;LastEditedWhen]\r\nFROM &#x5B;Sales].&#x5B;OrderLines];\r\nGO\r\n<\/pre>\n<p>We\u2019ll use <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/sp_helpindex-v20170228\/\">Kimberly\u2019s version<\/a> of sp_helpindex, sp_SQLskills_helpindex, to see what\u2019s truly <em>in<\/em> the index, and where. I much prefer this version to the built-in sp_helpindex, as it includes information about included columns, filters, and tells you what columns are in the tree and leaf levels.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nEXEC sp_SQLskills_helpindex 'Sales.SQLskills_OrderLines';\r\nGO\r\n<\/pre>\n<figure id=\"attachment_1038\" aria-describedby=\"caption-attachment-1038\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-large wp-image-1038\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/1_CI-1024x36.jpg\" alt=\"Clustered index for SQLskills_OrderLines\" width=\"1024\" height=\"36\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/1_CI-1024x36.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/1_CI-300x11.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/1_CI-768x27.jpg 768w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/1_CI.jpg 1733w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-1038\" class=\"wp-caption-text\">Clustered index for SQLskills_OrderLines<\/figcaption><\/figure>\n<p>This output is wide, which doesn\u2019t lend itself well to a post, so you may need to zoom in on the image. I also trimmed out the included and filtered columns as neither are relevant in this discussion.<\/p>\n<p>Notice that the only column in columns_in_tree is the concatenated OrderID and OrderLineID, and then all columns are in columns_in_leaf (because the leaf level of the clustered index is all the data).\u00a0 Now we\u2019ll add a nonclustered index on OrderID:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE NONCLUSTERED INDEX &#x5B;NCI_OrderID]\r\n     ON &#x5B;Sales].&#x5B;SQLskills_OrderLines] (&#x5B;OrderID])\r\n     ON &#x5B;USERDATA];\r\nGO\r\n\r\nEXEC sp_SQLskills_helpindex 'Sales.SQLskills_OrderLines';\r\nGO\r\n<\/pre>\n<figure id=\"attachment_1039\" aria-describedby=\"caption-attachment-1039\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"size-large wp-image-1039\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/2_NCI-1024x51.jpg\" alt=\"Nonclustered index for SQLskills_OrderLines\" width=\"1024\" height=\"51\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/2_NCI-1024x51.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/2_NCI-300x15.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/2_NCI-768x38.jpg 768w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/2_NCI.jpg 1822w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-1039\" class=\"wp-caption-text\">Nonclustered index for SQLskills_OrderLines<\/figcaption><\/figure>\n<p>In columns_in_tree we have our index key, OrderID, but we also have our clustering key, OrderID_and_OrderLineID, and both are in columns_in_leaf. What happens if we create a nonclustered index on OrderLineID, and make it unique?<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE UNIQUE NONCLUSTERED INDEX &#x5B;NCI_OrderLineID]\r\n     ON &#x5B;Sales].&#x5B;SQLskills_OrderLines] (&#x5B;OrderLineID])\r\n     ON &#x5B;USERDATA];\r\nGO\r\n\r\nEXEC sp_SQLskills_helpindex 'Sales.SQLskills_OrderLines';\r\nGO\r\n<\/pre>\n<figure id=\"attachment_1033\" aria-describedby=\"caption-attachment-1033\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-large wp-image-1033\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/3_NCI_unique-1024x68.jpg\" alt=\"Unique nonclustered index for SQLskills_OrderLines\" width=\"1024\" height=\"68\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/3_NCI_unique-1024x68.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/3_NCI_unique-300x20.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/3_NCI_unique-768x51.jpg 768w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/3_NCI_unique.jpg 1822w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-1033\" class=\"wp-caption-text\">Unique nonclustered index for SQLskills_OrderLines<\/figcaption><\/figure>\n<p>In the sp_SQLskills_helpindex output you\u2019ll notice that OrderLineID is the only column in columns_in_tree, and both OrderLineID and OrderID_and_OrderLineID are in columns_in_leaf.<\/p>\n<p>Remember that when you identify a nonclustered index as unique, it will not add the clustering key to the tree level, it just adds it to the leaf level. But if the nonclustered index is not declared as unique, then SQL Server adds the clustering key to the tree level to uniquely identify the row.<\/p>\n<p>If we had created a clustered index that we didn\u2019t also identify as unique, then SQL Server adds a UNIQUIFIER to the clustered index key, which then also gets added to the nonclustered index.<\/p>\n<h1>Defining the clustering key as part of the non-clustered key<\/h1>\n<p>What I\u2019ve seen lately when looking at indexes is this:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE NONCLUSTERED INDEX &#x5B;NCI_OrderID_2]     \r\n     ON &#x5B;Sales].&#x5B;SQLskills_OrderLines] (&#x5B;OrderID], &#x5B;OrderID_and_OrderLineID])\r\n     ON &#x5B;USERDATA];\r\nGO\r\n<\/pre>\n<p>This is the same as the NCI_OrderID index I created, but someone has specifically placed the clustering key in the nonclustered index key. I always ask why they added the clustering key. Typically the answer is: I know it\u2019s going to be needed in the query. That\u2019s fine, but understand that it doesn\u2019t matter whether you add the clustering key to the key for a non-unique nonclustered index or not; if <em>you<\/em> don\u2019t, SQL Server will add it.<\/p>\n<p>We\u2019ve kept it simple so far with a single column for our clustering key. What if we have multiple columns? We\u2019ll change up our table a bit:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nUSE &#x5B;WideWorldImporters]\r\nGO\r\n\r\nDROP TABLE IF EXISTS &#x5B;Sales].&#x5B;SQLskills_OrderLines];\r\nGO\r\n\r\nCREATE TABLE &#x5B;Sales].&#x5B;SQLskills_OrderLines](\r\n     &#x5B;OrderLineID] &#x5B;int] NOT NULL,\r\n     &#x5B;OrderID] &#x5B;int] NOT NULL,\r\n     &#x5B;OrderID_and_OrderLineID] &#x5B;bigint] NOT NULL,\r\n     &#x5B;StockItemID] &#x5B;int] NOT NULL,\r\n     &#x5B;Description] &#x5B;nvarchar](100) NOT NULL,\r\n     &#x5B;PackageTypeID] &#x5B;int] NOT NULL,\r\n     &#x5B;Quantity] &#x5B;int] NOT NULL,\r\n     &#x5B;UnitPrice] &#x5B;decimal](18, 2) NULL,\r\n     &#x5B;TaxRate] &#x5B;decimal](18, 3) NOT NULL,\r\n     &#x5B;PickedQuantity] &#x5B;int] NOT NULL,\r\n     &#x5B;PickingCompletedWhen] &#x5B;datetime2](7) NULL,\r\n     &#x5B;LastEditedBy] &#x5B;int] NOT NULL,\r\n     &#x5B;LastEditedWhen] &#x5B;datetime2](7) NOT NULL,\r\n     CONSTRAINT &#x5B;PK_Sales_SQLskills_OrderLines] PRIMARY KEY CLUSTERED\r\n     (\r\n     &#x5B;OrderID] ASC, &#x5B;OrderLineID] ASC, &#x5B;StockItemID] ASC, &#x5B;PackageTypeID] ASC\r\n     )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, \r\n     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON &#x5B;USERDATA]\r\n     ) ON &#x5B;USERDATA];\r\nGO\r\n\r\nINSERT INTO &#x5B;Sales].&#x5B;SQLskills_OrderLines](\r\n     &#x5B;OrderLineID]\r\n     ,&#x5B;OrderID]\r\n     ,&#x5B;OrderID_and_OrderLineID]\r\n     ,&#x5B;StockItemID]\r\n     ,&#x5B;Description]\r\n     ,&#x5B;PackageTypeID]\r\n     ,&#x5B;Quantity]\r\n     ,&#x5B;UnitPrice]\r\n     ,&#x5B;TaxRate]\r\n     ,&#x5B;PickedQuantity]\r\n     ,&#x5B;PickingCompletedWhen]\r\n     ,&#x5B;LastEditedBy]\r\n     ,&#x5B;LastEditedWhen])\r\nSELECT\r\n     &#x5B;OrderLineID]\r\n     ,&#x5B;OrderID]\r\n     ,CAST(CAST(&#x5B;OrderID] AS VARCHAR(20)) + CAST(&#x5B;OrderLineID] AS VARCHAR(20)) AS BIGINT)     \r\n     ,&#x5B;StockItemID]\r\n     ,&#x5B;Description]\r\n     ,&#x5B;PackageTypeID]\r\n     ,&#x5B;Quantity]\r\n     ,&#x5B;UnitPrice]\r\n     ,&#x5B;TaxRate]\r\n     ,&#x5B;PickedQuantity]\r\n     ,&#x5B;PickingCompletedWhen]\r\n     ,&#x5B;LastEditedBy]\r\n     ,&#x5B;LastEditedWhen]\r\nFROM &#x5B;Sales].&#x5B;OrderLines];\r\nGO\r\n\r\nEXEC sp_SQLskills_helpindex 'Sales.SQLskills_OrderLines';\r\nGO\r\n<\/pre>\n<figure id=\"attachment_1035\" aria-describedby=\"caption-attachment-1035\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1035\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/5_CI_4cols-1024x34.jpg\" alt=\"Four column clustered index for SQLskills_OrderLines\" width=\"1024\" height=\"34\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/5_CI_4cols-1024x34.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/5_CI_4cols-300x10.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/5_CI_4cols-768x26.jpg 768w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/5_CI_4cols.jpg 1822w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-1035\" class=\"wp-caption-text\">Four column clustered index for SQLskills_OrderLines<\/figcaption><\/figure>\n<p>I don\u2019t generally advocate a wide clustered index, but there are certain scenarios where it\u2019s needed\/appropriate. If we take this table and add a nonclustered index, not unique, on one column, then you\u2019ll see all four columns from the clustering key in the tree level:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE NONCLUSTERED INDEX &#x5B;NCI_Quantity]\r\n     ON &#x5B;Sales].&#x5B;SQLskills_OrderLines] (&#x5B;Quantity])\r\n     ON &#x5B;USERDATA];\r\nGO\r\n\r\nEXEC sp_SQLskills_helpindex 'Sales.SQLskills_OrderLines';\r\nGO\r\n<\/pre>\n<figure id=\"attachment_1036\" aria-describedby=\"caption-attachment-1036\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1036\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/6_NCI-1024x51.jpg\" alt=\"Nonclustered index (with wide CI) for SQLskills_OrderLines\" width=\"1024\" height=\"51\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/6_NCI-1024x51.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/6_NCI-300x15.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/6_NCI-768x39.jpg 768w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/6_NCI.jpg 1811w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-1036\" class=\"wp-caption-text\">Nonclustered index (with wide CI) for SQLskills_OrderLines<\/figcaption><\/figure>\n<p>For a large table, the size of this nonclustered index has the potential to be much, much larger than a table where the clustering key is only a single column.<\/p>\n<p>If we add a unique nonclustered index, then that clustering key is only in the leaf level:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE UNIQUE NONCLUSTERED INDEX &#x5B;NCI_OrderID_and_OrderLineID]\r\n     ON &#x5B;Sales].&#x5B;SQLskills_OrderLines] (&#x5B;OrderID_and_OrderLineID])\r\n     ON &#x5B;USERDATA];\r\nGO\r\n\r\nEXEC sp_SQLskills_helpindex 'Sales.SQLskills_OrderLines';\r\nGO\r\n<\/pre>\n<figure id=\"attachment_1040\" aria-describedby=\"caption-attachment-1040\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1040\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/8-1024x60.jpg\" alt=\"Unique nonclustered index (with wide CI) for SQLskills_OrderLines\" width=\"1024\" height=\"60\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/8-1024x60.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/8-300x18.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/8-768x45.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-1040\" class=\"wp-caption-text\">Unique nonclustered index (with wide CI) for SQLskills_OrderLines<\/figcaption><\/figure>\n<p>But then\u2026what happens if we add one or two columns from the clustering key into the nonclustered index key?<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE NONCLUSTERED INDEX &#x5B;NCI_PickingCompletedWhen]\r\n     ON &#x5B;Sales].&#x5B;SQLskills_OrderLines] (&#x5B;PickingCompletedWhen], &#x5B;OrderID], &#x5B;OrderLineID])\r\n     ON &#x5B;USERDATA];\r\nGO\r\n\r\nEXEC sp_SQLskills_helpindex 'Sales.SQLskills_OrderLines';\r\nGO\r\n<\/pre>\n<figure id=\"attachment_1041\" aria-describedby=\"caption-attachment-1041\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1041\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/9-1024x77.jpg\" alt=\"Nonclustered index with column from clustering key for SQLskills_OrderLines\" width=\"1024\" height=\"77\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/9-1024x77.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/9-300x22.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/9-768x57.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-1041\" class=\"wp-caption-text\">Nonclustered index with column from clustering key for SQLskills_OrderLines<\/figcaption><\/figure>\n<p>It doesn\u2019t matter! All the columns from the clustering key are added to the tree level because I did not identify it as unique! If I had defined the nonclustered index as unique, then only those two columns that I specified in the key would be in the tree, and the other two columns from the clustering key would be part of the leaf level.<\/p>\n<h1>Conclusion<\/h1>\n<p>There are a couple points to remember when designing your nonclustered indexes. First, whenever you know that the column, or combination or columns, makes a unique key for a nonclustered index, I always recommend that you specify UNIQUE when you create the index. That information is great for the optimizer to have when generating query plans. Second, think carefully about the impact of your clustering key on your nonclustered indexes. Any non-unique nonclustered index will be larger (more pages, eventually more levels) because the entire clustering key will be in the tree level. Further, if you happen to have a clustering key with a column (or multiple columns) that are frequently modified, then those modifications have to be propagated to the tree levels of the nonclustered indexes as well.<\/p>\n<p>Happy indexing!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been helping several customers with tuning lately, which typically means I\u2019m also looking at indexes and asking questions. A pattern I\u2019ve noticed is columns from the clustering key being added to the key definition for the nonclustered index.\u00a0 This brings up some interesting considerations when it comes to creating your nonclustered indexes. Review We\u2019re [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,15],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What columns are in that index? - Erin Stellato<\/title>\n<meta name=\"description\" content=\"Understanding what columns are in an index is important for mulitple reasons, including understanding how it affects index size and use.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What columns are in that index? - Erin Stellato\" \/>\n<meta property=\"og:description\" content=\"Understanding what columns are in an index is important for mulitple reasons, including understanding how it affects index size and use.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/\" \/>\n<meta property=\"og:site_name\" content=\"Erin Stellato\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-08T13:00:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-08-08T13:26:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/1_CI-1024x36.jpg\" \/>\n<meta name=\"author\" content=\"Erin Stellato\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Erin Stellato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/\",\"name\":\"What columns are in that index? - Erin Stellato\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\"},\"datePublished\":\"2019-08-08T13:00:31+00:00\",\"dateModified\":\"2019-08-08T13:26:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\"},\"description\":\"Understanding what columns are in an index is important for mulitple reasons, including understanding how it affects index size and use.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What columns are in that index?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\",\"name\":\"Erin Stellato\",\"description\":\"The SQL Sequel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\",\"name\":\"Erin Stellato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"caption\":\"Erin Stellato\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/erin\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What columns are in that index? - Erin Stellato","description":"Understanding what columns are in an index is important for mulitple reasons, including understanding how it affects index size and use.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/","og_locale":"en_US","og_type":"article","og_title":"What columns are in that index? - Erin Stellato","og_description":"Understanding what columns are in an index is important for mulitple reasons, including understanding how it affects index size and use.","og_url":"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/","og_site_name":"Erin Stellato","article_published_time":"2019-08-08T13:00:31+00:00","article_modified_time":"2019-08-08T13:26:07+00:00","og_image":[{"url":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2019\/08\/1_CI-1024x36.jpg"}],"author":"Erin Stellato","twitter_misc":{"Written by":"Erin Stellato","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/","name":"What columns are in that index? - Erin Stellato","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website"},"datePublished":"2019-08-08T13:00:31+00:00","dateModified":"2019-08-08T13:26:07+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158"},"description":"Understanding what columns are in an index is important for mulitple reasons, including understanding how it affects index size and use.","breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/what-columns-are-in-that-index\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/erin\/"},{"@type":"ListItem","position":2,"name":"What columns are in that index?"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/","name":"Erin Stellato","description":"The SQL Sequel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158","name":"Erin Stellato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","caption":"Erin Stellato"},"sameAs":["http:\/\/3.209.169.194\/blogs\/erin"],"url":"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/1032"}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/comments?post=1032"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/1032\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}