I’m sure it must have been asked before, but I can’t find it:
I have a CMS that is under constant development. I have built a number of websites using the CMS, so their are a number of databases in existence.
I want to extract the indices from the development db and apply them to the production dbs. Is there an easy way to extract indices as SQL?
Something along the lines of:
create index idx1 on one (lft, rght);
create index idx1 on two (alias);
create index acos_idx3 on three (model, related_id);
You can extract indices from INFORMATION_SCHEMA database, then add them on another database, but it’s not quite easy.
To give you an example (code from a stored procedure used for deployment), this adds an unique key if it’s not already there:
You can basically find whatever you need in INFORMATION_SCHEMA. I believe you can write code to dynamically check for all these indices, but I’m not sure if it’s easy for you.
UPDATE:
You can also use
show index from database.table, as you can see at the link provided by MaasSql’s answer. Then loop throuth the results and add each index if it’s not in the database.Or you can try this: