﻿{"id":86,"date":"2014-10-13T09:52:01","date_gmt":"2014-10-13T09:52:01","guid":{"rendered":"http:\/\/www.codereview.co\/?p=86"},"modified":"2014-10-13T10:56:00","modified_gmt":"2014-10-13T10:56:00","slug":"how-to-disable-indexes-in-sql-server-2005-and-2008","status":"publish","type":"post","link":"http:\/\/www.codereview.co\/index.php\/sql-server-tutorials\/how-to-disable-indexes-in-sql-server-2005-and-2008\/","title":{"rendered":"How to disable indexes in SQL Server 2005 and 2008?"},"content":{"rendered":"<p><span style=\"color: #000000;\"><strong>Scenario proposal<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">While looking through the new features in SQL Server 2005 and SQL Server 2008 we found a potentially interesting one called <strong>Disabling Indexes,<\/strong> which can be used to disable indexes on a table or a view. Can you give us a detailed explanation of how we go about using this new feature along with examples?<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Solution<\/strong><\/span><br \/>\n<!--more--><br \/>\n<span style=\"color: #000000;\">Database Administrators can use the Disabling Indexes feature which is available in SQL Server 2005 and later versions to prevent the index usage by user queries. This feature is very useful for DBA&#8217;s when they need to figure out whether the indexes which are available on a table are really useful or not. When you are disabling an index the index definition remains in metadata and index statistics are also kept on non-clustered indexes. However, disabling a clustered index or a non-clustered index on a view physically deletes the index data. If you are disabling a clustered index on a table then the table won&#8217;t be available for user access, however the data will still remain in the table, but it will be unavailable for any DML operation until the index is rebuilt or dropped. You can use <strong>ALTER INDEX REBUILD <\/strong>to rebuild an index and <strong>CREATE INDEX WITH DROP_EXISTING<\/strong> statement to enable a disabled index. In this tip I will be using the <strong>Person.Address <\/strong>table which is available in the <strong>AdventureWorks<\/strong> database.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Disabling Indexes <\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">There are different ways by which you can disable an index. Lets us go by each option one by one.<\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\"><strong> Disabling Index Using T-SQL<\/strong> Execute the below T-SQL to disable IX_Address_StateProvinceID index which is available on Person.Address table of AdventureWorks database.<\/span><\/li>\n<\/ol>\n<table width=\"90%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">USE\u00a0AdventureWorks GO ALTER\u00a0INDEX\u00a0IX_Address_StateProvinceID\u00a0ON\u00a0Person.Address\u00a0DISABLE GO &#8212;\u00a0Query\u00a0to\u00a0check\u00a0Index\u00a0Usage SELECT\u00a0NAME\u00a0AS\u00a0[IndexName],\u00a0TYPE_DESC\u00a0AS\u00a0[IndexType], CASE\u00a0IS_DISABLED\u00a0 WHEN\u00a00\u00a0THEN\u00a0&#8216;Enabled&#8217; ELSE\u00a0&#8216;Disabled&#8217;\u00a0 END\u00a0AS\u00a0[IndexUsage],\u00a0 FILL_FACTOR\u00a0AS\u00a0[FillFactor]\u00a0FROM\u00a0SYS.INDEXES WHERE\u00a0OBJECT_ID\u00a0=\u00a0OBJECT_ID(&#8216;Person.Address&#8217;)\u00a0ORDER\u00a0BY\u00a0IndexName,\u00a0IndexUsage GO<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\"><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-89\" src=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/13.jpg?resize=296%2C300\" alt=\"1\" srcset=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/13.jpg?resize=296%2C300 296w, http:\/\/www.codereview.co\/wp-content\/uploads\/2014\/10\/13.jpg 612w\" sizes=\"(max-width: 296px) 100vw, 296px\" data-recalc-dims=\"1\" \/><\/strong><\/span><\/p>\n<p>&nbsp;<\/p>\n<ol start=\"2\">\n<li><span style=\"color: #000000;\"><strong>Disabling Index Using SQL Server Management Studio (SSMS)<\/strong> Expand Object Explorer for the AdventureWorks Tables until you get to Person.Address and then expand Indexes and right click <strong>IX_Address_StateProvinceID (Non-Unique, Non-Clustered) <\/strong>and select <strong>Disable<\/strong> from the drop down list. This will open up <strong>Disable Indexes<\/strong> dialog box, click OK to disable the index.<\/span><\/li>\n<\/ol>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-90\" src=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/22.jpg?resize=300%2C253\" alt=\"2\" srcset=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/22.jpg?resize=300%2C253 300w, http:\/\/www.codereview.co\/wp-content\/uploads\/2014\/10\/22.jpg 569w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\">You can also <strong>disable<\/strong> an index by unchecking <strong>Use Index <\/strong>option as shown in the below snippet which is available in the <strong>Options<\/strong> page for the <strong>Index Properties<\/strong> dialog box.<\/span><\/p>\n<p><span style=\"color: #000000;\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-91\" src=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/31.jpg?resize=300%2C192\" alt=\"3\" srcset=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/31.jpg?resize=300%2C192 300w, http:\/\/www.codereview.co\/wp-content\/uploads\/2014\/10\/31.jpg 575w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Enabling Indexes <\/strong> <\/span><\/p>\n<p><span style=\"color: #000000;\">There are different ways by which you can enable indexes. Let&#8217;s go through each option. <\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>1. Enable Index Using ALTER INDEX REBUILD Statement<\/strong><\/span><\/p>\n<table width=\"90%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">USE\u00a0AdventureWorks GO ALTER\u00a0INDEX\u00a0IX_Address_StateProvinceID\u00a0ON\u00a0Person.Address\u00a0REBUILD GO<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\"><strong>2. Enable Index Using CREATE INDEX WITH DROP_EXISTING Statement<\/strong><\/span><\/p>\n<table width=\"90%\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\">USE\u00a0AdventureWorks GO CREATE\u00a0NONCLUSTERED\u00a0INDEX\u00a0[IX_Address_StateProvinceID]\u00a0ON\u00a0[Person].[Address]\u00a0 ( [StateProvinceID]\u00a0ASC )WITH\u00a0(DROP_EXISTING\u00a0=\u00a0ON,\u00a0FILLFACTOR\u00a0=\u00a080)\u00a0ON\u00a0[PRIMARY] GO<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\"><strong>3. Enable Index Using SQL Server Management Studio (SSMS)<\/strong> Expand Object Explorer for the AdventureWorks Tables until you get to Person.Address and then expand Indexes and right click <strong>IX_Address_StateProvinceID (Non-Unique, Non-Clustered) <\/strong>and select <strong>Rebuild<\/strong> from the drop down list. This will open up <strong>Rebuild Indexes<\/strong> dialog box, click OK to enable the index.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-92\" src=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/41.jpg?resize=300%2C250\" alt=\"4\" srcset=\"http:\/\/i0.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/41.jpg?resize=300%2C250 300w, http:\/\/www.codereview.co\/wp-content\/uploads\/2014\/10\/41.jpg 565w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">You can also enable an index by selecting <strong>Use Index <\/strong>option as shown in the below snippet which is available in the <strong>Options<\/strong> page for the <strong>Index Properties<\/strong> dialog box.<\/span><\/p>\n<p><span style=\"color: #000000;\">\u00a0<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-93\" src=\"http:\/\/i1.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/51.jpg?resize=300%2C193\" alt=\"5\" srcset=\"http:\/\/i1.wp.com\/www.codereview.co\/wp-content\/uploads\/2014\/10\/51.jpg?resize=300%2C193 300w, http:\/\/www.codereview.co\/wp-content\/uploads\/2014\/10\/51.jpg 571w\" sizes=\"(max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Thanks for reading this article,<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Next steps :<\/strong><\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\"><strong> Add this article to your database toolkit<\/strong><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong> Share this with your colleagues because Sharing is Learning<\/strong><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong> Comment below if you need any assistance<\/strong><\/span><\/li>\n<\/ol>\n<p><span style=\"color: #000000;\">\u00a0<\/span><\/p>\n<p><span style=\"color: #000000;\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scenario proposal While looking through the new features in SQL Server 2005 and SQL Server 2008 we found a potentially interesting one called Disabling Indexes, which can be used to disable indexes on a table or a view. Can you give us a detailed explanation of how we go about using this new feature along [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[173,65,4,3],"tags":[18,17,19,16],"_links":{"self":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/86"}],"collection":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":7,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":330,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/86\/revisions\/330"}],"wp:attachment":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}