babybird
asked on
dropping and recreating indexes dynamically
We have an insert that is running poorly and we believe it is because of the number of indexes on the table. It is a very large table (approx 30 million rows). The insert is run in a batch process utilizing a stored proc. We would like to dynamically drop and recreate any indexes on it. I'd rather not hardcode them in the even we add some in the future and forget to address the hardcoding. I also think we could get by with only dropping the non-clustereds so the recreation doesn't take so long so it would be nice if we could identify these specifically.
Is there anyway to dynamically identify the indexes, drop them before the insert and recreate them after the insert?
Is there anyway to dynamically identify the indexes, drop them before the insert and recreate them after the insert?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Take out the status <> 0 and you should get all but your first. My recollection of the 0x800 and 0x2 flags are that they are the primary key and unique flags for an index. The first one you list has both set, and is therefore (if my recollection is right) the primary key of the table. Are you sure you want to drop this as well? If not, then hopefully removing the status <> 0 will sort you out - I can't remember why I added it in the first place.
Do you need any further help with this problem? Thanks.
ASKER
I might...I was able to get started but haven't gotten to complete it yet because some other things have taken priority but I hope to be hitting on it next week. I do have one question offhand....security....we don't have dbo rights in production so I would assume we would have issues with dropping and recreating indexes within our proc unless we set something up special. Do you have any thoughts on approaches to this?
Thanks for being so patient.
Thanks for being so patient.
No problem.
You need to be in the db_owner or db_ddladmin roles to drop and recreate the indexes. Since this is a batch process not an ad hoc thing, I would negotiate with the DBAs for this to run under a special login in the db_ddladmin role.
You need to be in the db_owner or db_ddladmin roles to drop and recreate the indexes. Since this is a batch process not an ad hoc thing, I would negotiate with the DBAs for this to run under a special login in the db_ddladmin role.
Of course, the above proc that goes against the sysindexes table might not work in SQL2005....
ASKER
arbert - why do you say that? What's changing?
No, true.
The system tables are being replaced in Yukon with different ones. They are implementing many of the old names as views for backwards compatibility, but there's no guarantee.
The system tables are being replaced in Yukon with different ones. They are implementing many of the old names as views for backwards compatibility, but there's no guarantee.
"The system tables are being replaced in Yukon with different ones."
I wouldn't say replaced--there are a lot of changes. There are never any guarantees when you use the system tables directly--that's why you use the Information_schema views or built in (documented) stored procedures. Not only can an upgrade of SQL Server break things, but even a service pack/hotfix can cause problems.
I wouldn't say replaced--there are a lot of changes. There are never any guarantees when you use the system tables directly--that's why you use the Information_schema views or built in (documented) stored procedures. Not only can an upgrade of SQL Server break things, but even a service pack/hotfix can cause problems.
ASKER
I ended up using Muzzy2003's logic tweaked to used sp_helpindex results and metadata functions where possible. This hopefully will limit the impact if they make changes to sysindexes. I greatly appreciate both of your help. You saved me a great deal of time and the process works beautifully!!
ASKER
FOR SELECT indid, name, OrigFillFactor
FROM sysindexes
WHERE id = @intObjectID
AND status & 0x800 = 0
AND status & 0x2 = 0
AND status <> 0
AND first <> 0
I did get it further along but am not clear on the how the status criteria and first criteria work as far as identifying the rows I need. I get several rows for the system indexes starting with _WA_Sys when I remove the 'status' and 'first' criteria. If I use the criteria as you have it entered, it eliminates all my rows. I hope this isn't a stupid question but I don't understand what 'status & 0x800' does.
This is the status column and the first column for the actual rows I want:
18450 0x28B609000100
0 0x6A0100000100
0 0x067501000100
The rows I don't want look like this for that particular table
10485856 0x000000000000
8388704 0x000000000000