Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mgspross
Starting Member
2 Posts |
Posted - 2008-07-18 : 23:45:56
|
Hi all,I have a situation where I need to delete all the non-primary-key indexes in a database. After I delete these indexes, I will recreate new indexes from a predefined list. This database is part of a commercial application -- the reason I am doing this is to ensure all of our users have the same indexes in their databases (there apparently was a mix-up with a previous application update and some customers have different, missing, or additional indexes in their databases, thus I don't know ahead of time what indexes I need to delete).I came up with the following TSQL script to delete all the user-defined non-PK indexes. I'm by means a SQL guru, so I am wondering if there is room for improvement or if I am doing anything that is flat-out wrong or potentially dangerous:-- drop all non-PK indexes in the database my_databaseuse [my_database]declare @index_name sysnamedeclare @index_id intdeclare @sql_cmd nvarchar(512)-- get the name of each index in 'table_name.index_name' format.---- * ignore primary keys (indid=1) and system-defined indexes-- ('_WA_foo') and only get indexes on user-defined tables-- ('IsUserTable')---- * the 'indid' column is not used but needs to be here-- because of the ORDER BY---- * the ORDER BY is probably not necessary but forces-- the indexes to be dropped in a semi-logical orderdeclare c_index_info cursor for select object_name(id)+'.'+[name],indid from sysindexes where objectproperty(id,'IsUserTable') = 1 and not [name] like '_WA_%' and indid > 1 order by object_name(id),indid desc-- create a 'DROP INDEX' command for each index-- and execute itopen c_index_infofetch next from c_index_info into @index_name,@index_idwhile @@FETCH_STATUS = 0begin set @sql_cmd = 'DROP INDEX '+@index_name exec sp_executesql @sql_cmd fetch next from c_index_info into @index_name,@index_idend-- clean upclose c_index_infodeallocate c_index_info |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-07-20 : 12:44:04
|
Your assumption that indexes with an id of 1 are primary keys is incorrect. Indexes with an ID of 1 are the clustered index. By default the pk is enforced by a clustered index but it is not required. It's perfectly possible to have the PK enforced by a nonclustered index.Best way is probably to find the name of the primary key from sysobject where xtype='pk' and excluding indexes with those names.Also, index ID of 255 is the LOB data (large object - text, ntext, image) in a table and also is not a droppable indexFor your info, the _WA_sys entries aren't system-defined indexes. They are column statistics. SQL will never automatically create an index.--Gail ShawSQL Server MVP |
 |
|
mgspross
Starting Member
2 Posts |
Posted - 2008-07-20 : 13:45:18
|
quote: Originally posted by GilaMonster Your assumption that indexes with an id of 1 are primary keys is incorrect. Indexes with an ID of 1 are the clustered index....Best way is probably to find the name of the primary key from sysobject where xtype='pk' and excluding indexes with those names.
Thanks, I will correct my code. I did some more experimenting and it seems like the sysindexes.status column also indicates what type of index you have. From my tests, primary key indexes always have bit 11 of the status column set, and a few other websites I've found since my original post seem to confirm this. I guess there is always more than one way to do things with the SQL Server system tables ;-)quote: Also, index ID of 255 is the LOB data (large object - text, ntext, image) in a table and also is not a droppable index
Good to know. I will add a check for this in my script.quote: For your info, the _WA_sys entries aren't system-defined indexes. They are column statistics. SQL will never automatically create an index.
Thanks. I really didn't know what these were, I just assumed they were for internal use. |
 |
|
|
|
|
|
|