简体   繁体   中英

mysql : loop over tables and alter table add index

I have ~1000 tables that start with the same prefix : table_prefix_{SOME_ID} (i can take the ids from another table)

what is the fast way to loop over all the tables in mysql and do :

   ALTER TABLE `table_prefix_{some_id}` ADD INDEX `fields` (`field`)

Forget looping. Just do this:

select concat( 'alter table ', a.table_name, ' add index `fields` (`field`);' )
from information_schema.tables a 
where a.table_name like 'table_prefix_%';

Then take the result set and run it as a SQL script.

BTW, you probably mean create index index_name on table_name( column_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM