mysql:循环遍历表和alter table添加索引 [英] mysql : loop over tables and alter table add index
本文介绍了mysql:循环遍历表和alter table添加索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有〜1000个以相同前缀开头的表:
table_prefix_ {SOME_ID}
(我可以从另一个表中获取ID)
I have ~1000 tables that start with the same prefix :
table_prefix_{SOME_ID}
(i can take the ids from another table)
在mysql中循环遍历 所有 表的快速方法是什么:
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_%';
然后获取结果集并将其作为SQL脚本运行。
Then take the result set and run it as a SQL script.
BTW,你可能意味着在table_name(column_name)上创建索引index_name;
BTW, you probably mean create index index_name on table_name( column_name);
这篇关于mysql:循环遍历表和alter table添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文