MySQl在开发和生产不同环境下的索引对比


本文摘自PHP中文网,作者巴扎黑,侵删。

--1.创建索引信息表
create table `t_index_update` (
`table_name` varchar(20) COLLATE gbk_bin DEFAULT NULL,
`index_name` varchar(20) COLLATE gbk_bin DEFAULT NULL,
`index_cols` varchar(100) COLLATE gbk_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_bin;
--2.插入线下索引信息表
insert into t_index_update()
select
table_name,
index_name,
group_concat(distinct concat('`', column_name, '`') order by seq_in_index asc separator ', ') as index_cols
from information_schema.STATISTICS
where table_schema= 'elk'
and column_name<>'seq_id'
and index_name<>'primary'
group by table_name, index_name
order by table_name asc, index_name asc;
--3.同步线下索引信息表到线上
--4.构建删除和修改过的索引的删除语句
select concat('alter table `',a.table_name,'` drop index ',a.index_name,';')
from
(
select
table_name,
index_name,
group_concat(distinct concat('`', column_name, '`') order by seq_in_index asc separator ', ') as index_cols
from information_schema.STATISTICS
where table_schema= 'elk'
and column_name<>'seq_id'
and index_name<>'primary'
group by table_name, index_name
) a
left join t_index_update b on b.table_name and b.index_name=a.index_name and b.index_cols=a.index_cols
where b.index_name is null;
--5.构建新加索引的的新加语句
select concat('alter table `',a.table_name,'` add index ',a.index_name,'(',a.index_cols,');')
from t_index_update a
left join (
select
table_name,
index_name,
group_concat(distinct concat('`', column_name, '`') order by seq_in_index asc separator ', ') as index_cols
from information_schema.STATISTICS
where table_schema= 'elk'
and column_name<>'seq_id'
and index_name<>'primary'
group by table_name, index_name
) b on b.table_name and b.index_name=a.index_name and b.index_cols=a.index_cols
where b.index_name is null;

以上就是MySQl在开发和生产不同环境下的索引对比的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

mysql运行sql文件时出现问题怎么办

mysql如何自动为查询数据的结果编上序号方法教程

mysql安装与卸载详细教程

怎样安全地关闭mysql实例

分享mysql常用内核debug几种常见方法

mysql主从复制原理详解

用c++封装mysql的api的教程

mysql 错误1248怎么办

mysql如何导出指定表中的数据

mysql事务之acid特性(详解)

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...