I am making a procedure for optimizing indices of my databases and it will take me long. So I want to confirm if it is feasible to do? And not already done otherwise want to get it.
- I want to read all indices from all tables.
- I want to drop all other indices on fields which are primary key
- After doing above. The fields which are still indexed as unique. I want to drop all other indices on them
- The fields which are neither primary nor unique. I want to keep only one index on each and drop all other indices
I am curious Why MYSQL allows unique index on a primary key field? Can it make any useful difference?
Question :
I need guidance/query/procedure to remove all non useful indices with the hierarchy mentioned in my above 4 points
Update 1: I will keep my work updating Here on SQLFiddle. At the moment it is just beginning. However you can see on this link that there are four fields and 8 indices. I want just 3 of them and drop all others. I need only 1st, 3rd and 4th. According to my above 4 points.
Update 2 : I got excellent answer by eggayal. First link he gave is the pure sql solution. I tried it Here at Link2. It gave me undesired output. Output of link2 can be compared by looking into Link1 here
Desired output Is
COLUMN_NAMES REDUNDANT_INDEXES
1 auth_id `auth_id_3`,`auth_id_2`,`auth_id`
2 id `id_2`,`id_3`
3 subject `subject_1`
Faults in th output of query in Link2
Row1: auth_id is not shown as redundant index because It has nothing to do (compare) with unique key auth_id_4 on the same field. But I need it because I don’t need this index when same column has a unique index as well
Row2 : I will like to say all other indices redundant when there exists primary key
index on some column
Row3 : All right
I have made an application in c#. It removes duplicate indices according to your priority, Hope it would help
It might need much improvements but what I know is… It does not remove only those duplicate indices which are involved in primary(composite) and foreign key at a time (which should not be usually good approach)
Here is download link for complete application with source
Following is the main file of above link
You may ignore/remove gridviews they are just for showing your indices. You just need to call main function