--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
) aleft 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 兩表索引優化
建表語句 create table if not exists class id int 10 unsigned not null auto increment,card int 10 unsigned not null,primary key id create table if not exis...
雙索引 mysql (五)MySQL兩表索引優化
建表語句 create table if not exists class id int 10 unsigned not null auto increment,card int 10 unsigned not null,primary key id create table if not exis...
mysql的兩大資料庫引擎
mysql儲存引擎中的myisam和innodb區別詳解 在mysql 5.5之前,myisam是mysql的預設資料庫引擎,其由早期的isam indexed sequential access method 有索引的順序訪問方法 所改良。雖然myisam效能極佳,但卻有乙個顯著的缺點 不支援事務...