I am running a DTS to preform tasks in my database, in which at first I need to disable all indexes in the database and re-enable them when the DTS finish his work.
Is there a way that I can disable all the indexes in the whole database and afterwards to re-enable them all?
I know how to disable/enable one by one, can someone help me with the way to disable/enable all at once as a step in the DTS.
8条答案
按热度按时间ymzxtsji1#
We can use below scrip to disable indexes
Do your bulk insert to table and than run below script.
okxuctiv2#
Here is a script that will output ALTER statements for all non clustered indexes in your database. You can modify this easily to output REBUILD scripts and scripts for clustered indexes
m4pnthwp3#
This works for SQL Server 2008 and newer versions. It allows different schemas and also names that have spaces, dashes and other special characters that have to be quoted. What is the use of the square brackets [] in sql statements?
These scripts will output code into the results tab. You must copy/paste into the query tab and execute them.
Disable script
Enable script (Rebuild)
This is based on another answer here.
f4t66c6m4#
In order to enable an index, you have to rebuild it. This script will rebuild all disabled indexes.
jw5wzhpr5#
Disabling indexes is a good idea when it comes to loading large quantities of data, but... the big problem is clustered indexes. If you disable a clustered index, you’ve disabled the entire table.
Several options suggest themselves, and none of them are simple.
Loop through the system views (sys.indexes), extract the table and index name, generate and execute dynamic SQL to disable the index. Have an “undo” routine to re-enable them. (Be wary--was it a unique index or a unique constraint?) This, alas, only works if you do not use clustered indexes. Good luck with that.
As for 1, but skip any clustered indexes. When you load data, make sure it gets loaded in (clustered index) sequential order, otherwise you'll have poor load times and fragmented tables. (If you data providers are like mine, good luck with that one, too.)
Create tables in your database containing definitions of the indexes on your “loading” tables. Build a routine that loops through them and drops all the indexes (clustered indexes last). This will be fast if you truncate the tables first. Load your data, then loop through and recreate the indexes from scratch (clustered first). Use table partitioning to make less horrible on the rest of the system (e.g. do all the above on the “loading” tables, then use partition switching to move the loaded data into your “live” tables). It took me no little time to build such a system, but it can and will work.
anauzrmj6#
Use this script to disable all indices
Use this script to rebuild(enable) all indices
imzjd6km7#
You will have to run a script that selects the metadate for the table and index. Then you can do an:
Later you can run a similar script to rebuild:
You can do these one at a time, or collect them into a NVARCHAR(MAX) variable and execute them as a single batch. You can see sample code at this earlier question:
Disable all non-clustered indexes
gywdnpxw8#
All the index will be enabled by charm!