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 |
cosaco
Starting Member
1 Post |
Posted - 2004-08-26 : 11:33:40
|
The next script, gets redundant indexes, in a given database.I run it in the query Analyzer, one statement at a time.PLEASE: review the output, before drop any index.USE ....-- step 1-- gets an tab,idx,col,order viewcreate view listaidxcols as select SO.name as tabname, SI.name as idxname, IK.keyno as keyno, SC.name as colnamefrom sysindexkeys IK, syscolumns SC, sysindexes SI, sysobjects SOwhere -- Link syscolumns IK.id=SC.id and IK.colid=SC.colid-- Link sysindexesand IK.id=SI.id and IK.indid=SI.indid-- Link sysObjects (tables)and IK.id=SO.idand SO.xtype='U'-- no internal indexesand SI.name not like '_WA_Sys_%'and SI.name not like 'hind_%'--step 2: view to get # of columns per indexcreate view cantcolsidxas select tabname, idxname, count(*) as numllavesfrom listaidxcolsgroup by tabname,idxname-- step 3-- the redundant index listselect A.tabname as tabla,A.idxname as Aidx, B.idxname as Bidxfrom cantcolsidx A, cantcolsidx Bwhere A.tabname = B.tabnameand A.numllaves < B.numllavesand A.idxname <> B.idxnameand A.numllaves in ( select count(*) from listaidxcols C, listaidxcols D where C.tabname=A.tabname and C.idxname=A.idxname and D.tabname=B.tabname and D.idxname=B.idxname and C.idxname<>D.idxname and C.colname=D.colname and C.keyno =D.keyno)--clean updrop view listaidxcols;drop view cantcolsidx; |
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-08-26 : 18:50:51
|
Thanks, that's a useful script. Found 2 redundant indexes in one of my DB's.--KenI want to die in my sleep like my grandfather, not screaming in terror like his passengers. |
|
|
coolerbob
Aged Yak Warrior
841 Posts |
Posted - 2005-04-07 : 07:32:38
|
What is this actually doing? How does it decide what is "redundant"? |
|
|
|
|
|
|
|