可恶SQL SERVER的索引Disable后不能直接ENABLE,只有rebuild后才OK!SQL SERVER – Disable Clustered Index and Data Insert
2014-02-25 18:55
477 查看
Earlier today, I received following email.
“Dear Pinal,
We looked at your script and found out that in your script of disabling indexes, you have only included selected non-clustered index during the bulk insert and missed to disabled all the clustered index. Our DBA [name removed] has changed your script a
bit and included all the clustered indexes. Since then our application is not working.
When DBA [name removed] tried to enable clustered indexes again he is facing error Incorrect syntax error.
We are in deep problem [word replaced]
[Removed Identity of organization and few unrelated stuff ]“
I have replied to my client and helped them fixed the problem. However, what really caught my attention was the concept of disabling clustered index. Let us try to learn a lesson from this experience.
In this case, there was no need to disable clustered index at all. I had done all the crucial work when I was called in to work on the tuning project. I removed unused indexes, created a few optimal indexes and wrote a script to disable selected high cost indexes
when bulk insert operations (and the like) are performed. There was another script which rebuilds all the indexes as well. The solution worked until they included a clustered index in order to disable the script.
Clustered indexes are in fact original tables (or heap) which are physically ordered (any more things – not scope of this article) according to one or more keys (columns). When a clustered index is disabled, its data rows cannot be accessed. This means
that there will be no insertion process possible. On the other hand, when non-clustered indexes are disabled, all the data related to it are physically deleted, but the definition of the index is kept in the system.
Due to the same reason, even reorganization of the index is not possible until the clustered index (which was disabled) is rebuilt. Now, let us come to the second part of the question which is in regards to the receiving of the error when a clustered index
is ‘enabled’. This is a very common question that I receive on the blog. (The following statement is written keeping the syntax of T-SQL in mind) Clustered indexes can be disabled but cannot be enabled again; they have to be rebuilt to become enabled. It is
indeed a common thinking that something which we have ‘disabled’ can be ‘enabled’ but the syntax for this is ‘rebuild’. This issue has been explained here: SQL
SERVER – How to Enable Index – How to Disable Index – Incorrect syntax near ‘ENABLE’.
Let us go over this example where inserting the data is not possible when a clustered index is disabled.
I hope this example is clear enough. There were a few additional posts I had written years ago, and they are as follows:
SQL
SERVER – Enable and Disable Index Non Clustered Indexes Using T-SQL
SQL
SERVER – Enabling Clustered and Non-Clustered Indexes – Interesting Fact
“Dear Pinal,
We looked at your script and found out that in your script of disabling indexes, you have only included selected non-clustered index during the bulk insert and missed to disabled all the clustered index. Our DBA [name removed] has changed your script a
bit and included all the clustered indexes. Since then our application is not working.
When DBA [name removed] tried to enable clustered indexes again he is facing error Incorrect syntax error.
We are in deep problem [word replaced]
[Removed Identity of organization and few unrelated stuff ]“
I have replied to my client and helped them fixed the problem. However, what really caught my attention was the concept of disabling clustered index. Let us try to learn a lesson from this experience.
In this case, there was no need to disable clustered index at all. I had done all the crucial work when I was called in to work on the tuning project. I removed unused indexes, created a few optimal indexes and wrote a script to disable selected high cost indexes
when bulk insert operations (and the like) are performed. There was another script which rebuilds all the indexes as well. The solution worked until they included a clustered index in order to disable the script.
Clustered indexes are in fact original tables (or heap) which are physically ordered (any more things – not scope of this article) according to one or more keys (columns). When a clustered index is disabled, its data rows cannot be accessed. This means
that there will be no insertion process possible. On the other hand, when non-clustered indexes are disabled, all the data related to it are physically deleted, but the definition of the index is kept in the system.
Due to the same reason, even reorganization of the index is not possible until the clustered index (which was disabled) is rebuilt. Now, let us come to the second part of the question which is in regards to the receiving of the error when a clustered index
is ‘enabled’. This is a very common question that I receive on the blog. (The following statement is written keeping the syntax of T-SQL in mind) Clustered indexes can be disabled but cannot be enabled again; they have to be rebuilt to become enabled. It is
indeed a common thinking that something which we have ‘disabled’ can be ‘enabled’ but the syntax for this is ‘rebuild’. This issue has been explained here: SQL
SERVER – How to Enable Index – How to Disable Index – Incorrect syntax near ‘ENABLE’.
Let us go over this example where inserting the data is not possible when a clustered index is disabled.
USE AdventureWorks GO -- Create Table CREATE TABLE [dbo].[TableName]( [ID] [int] NOT NULL, [FirstCol] [varchar](50) NULL, CONSTRAINT [PK_TableName] PRIMARY KEY CLUSTERED ([ID] ASC) ) GO -- Create Nonclustered Index CREATE UNIQUE NONCLUSTERED INDEX [IX_NonClustered_TableName] ON [dbo].[TableName] ([FirstCol] ASC) GO -- Populate Table INSERT INTO [dbo].[TableName] SELECT 1, 'First' UNION ALL SELECT 2, 'Second' UNION ALL SELECT 3, 'Third' GO -- Disable Nonclustered Index ALTER INDEX [IX_NonClustered_TableName] ON [dbo].[TableName] DISABLE GO -- Insert Data should work fine INSERT INTO [dbo].[TableName] SELECT 4, 'Fourth' UNION ALL SELECT 5, 'Fifth' GO -- Disable Clustered Index ALTER INDEX [PK_TableName] ON [dbo].[TableName] DISABLE GO -- Insert Data will fail INSERT INTO [dbo].[TableName] SELECT 6, 'Sixth' UNION ALL SELECT 7, 'Seventh' GO -- Reorganizing Index will also throw an error ALTER INDEX [PK_TableName] ON [dbo].[TableName] REORGANIZE GO -- Rebuliding should work fine ALTER INDEX [PK_TableName] ON [dbo].[TableName] REBUILD GO -- Insert Data should work fine INSERT INTO [dbo].[TableName] SELECT 6, 'Sixth' UNION ALL SELECT 7, 'Seventh' GO -- Clean Up DROP TABLE [dbo].[TableName] GO
I hope this example is clear enough. There were a few additional posts I had written years ago, and they are as follows:
SQL
SERVER – Enable and Disable Index Non Clustered Indexes Using T-SQL
SQL
SERVER – Enabling Clustered and Non-Clustered Indexes – Interesting Fact
相关文章推荐
- ios UIButton得 几种触发方式
- 基本的SQl语句:update table1 set field1=value1 where 范围
- 【问题】各种背景色&UIScrollView注意问题
- RDIFramework.NET 答客户问(2014-02-23)
- 谜题8:Dos Equis——条件表达式。
- 【IOS】KVC(Key - value coding 机制) & KVO(键值观察)
- Ubiquitous Religions
- Build类
- Extjs4.x (MVC)Controller中refs以及Ext.ComponentQuery解析
- poj 2299 Ultra-QuickSort(树状数组+离散化的题目)据说是简单题,不过还是觉得好难。。。
- poj2524 Ubiquitous Religions(并查集求组数)
- EditText自动弹出键盘问题处理
- View Controller Programming Guid for iOS 笔记
- Fatal error: Unknown: Failed opening required 'c:/wamp/www/view.php' (include_path='.
- iOS:自定义UIBarButtonItem
- ArrayBlockingQueue 实现线程同步效果例子
- How to Build ffmpeg with NDK r9
- Android Building System 分析
- 深入理解ReferenceQueue GC finalize Reference
- Request对象获取数据