mysql : loop over tables and alter table add index

后端 未结 1 603
慢半拍i
慢半拍i 2021-02-09 05:48

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

1条回答
  •  滥情空心
    2021-02-09 06:39

    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);

    0 讨论(0)
提交回复
热议问题