生成 MySQL 删除索引、创建索引、分析表的 SQL 语句
短信预约 -IT技能 免费直播动态提醒
目录
1. 生成删除索引的 SQL 语句
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock -e "select concat('alter table \`',table_schema,'\`.\`',table_name,'\` ',drop_index,';') from (select table_schema, table_name, group_concat(if(index_name='PRIMARY','drop primary key',concat('drop index \`',index_name,'\`'))) drop_index from (select distinct table_schema, table_name, index_name from information_schema.statistics where table_schema in ('test') and (table_schema, table_name, index_name) not in (select t1.table_schema, t1.table_name, t1.index_name from information_schema.statistics t1, information_schema.columns t2 where t1.table_schema in ('test') and t1.table_schema = t2.table_schema and t1.table_name = t2.table_name and t1.column_name = t2.column_name and t2.extra='auto_increment')) t group by table_schema, table_name) t;" -N > drop_index.sql
2. 生成创建索引的 SQL 语句
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock -e "select concat('alter table \`',table_schema,'\`.\`',table_name,'\` ',create_index,';') from (select table_schema, table_name, group_concat(if(index_name='PRIMARY',concat('add primary key (',index_columns,')'),concat('add index \`',index_name,'\` (',index_columns,')'))) create_index from (select table_schema, table_name, index_name, group_concat(concat('\`',column_name,'\`', if(sub_part is null,'',concat('(',sub_part,')'))) order by seq_in_index) index_columns from information_schema.statistics where table_schema in ('test') and (table_schema, table_name, index_name) not in (select t1.table_schema, t1.table_name, t1.index_name from information_schema.statistics t1, information_schema.columns t2 where t1.table_schema in ('test') and t1.table_schema = t2.table_schema and t1.table_name = t2.table_name and t1.column_name = t2.column_name and t2.extra='auto_increment') group by table_schema, table_name, index_name) t group by table_schema, table_name) t;" -N > create_index.sql
3. 生成分析表的 SQL 语句
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock -e "select concat('analyze table \`',table_schema,'\`.\`',table_name,'\`;') from information_schema.tables where table_schema in ('test');" -N > analyze_table.sql
来源地址:https://blog.csdn.net/wzy0623/article/details/132625216
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341