I have a database with a lot of unscary indexes. I don’t want to remove them all with one big query for time reasons. I’m not very familiar with query languages so is there a way to loop through the index list and remove all indexes with exception of PKs or is doing it the long way better/safer.
4
Answers
Thanks everyone for the helpful advice. I ended up taking Tadman's advice and used PHP to run a loop. here is the code in case anyone in the future has the same question/problem.
There are no looping constructs for DDL (DROP INDEX etc). You could dynamically build DDL statements and execute them, but depending on how much you have to drop, it’s probably not worth the bother.
You say "I don’t want to remove them all with one big query for time reasons." but "remove them all with one big query" is what you would be doing if you looped through a list of indexes and executed the DROP for each.
If you have the grants to access to information_schema, you can generate a procedure that gets index names from specific table (INNODB_SYS_INDEXES), create the drop statement (with PREPARE command) and run it.
In the INNODB_SYS_INDEXES the Indexes have a specific flag (TYPE field) [0 (secondary index), 1 (cluster index), 2 (unique index), 3 (primary key), 32 (full-text index).)
bye
You have to create a procedure (my_procedure) with the following declarations:
(To do this you must have the right privileges on information_schema).
Now you can OPEN the cursor and FETCH it into three variables:
v_TABLE_SCHEMA, v_TABLE_NAME, v_INDEX_NAME that you have previously declared;
for each entry of the cursor you have to create a proper statement:
Then you have to run the statement contained into the string with the following sequence of statements:
then close the loop.
now you can run
to drop in few seconds all indexes.
***** be careful before running the procedure!
run the select that is used in the creation of the cursor to make you sure that the index that you are going to select are really to delete.
Once runned the procedure there is no way to roll back.
good luck!
bye