MySQL development and production environment index comparison

Source: Internet
Author: User

--1. Creating an index information table
CREATE TABLE ' T_index_update ' (
' table_name ' varchar (COLLATE) Gbk_bin DEFAULT NULL,
' index_name ' varchar (COLLATE) Gbk_bin DEFAULT NULL,
' Index_cols ' varchar (+) COLLATE gbk_bin DEFAULT NULL
) Engine=innodb DEFAULT CHARSET=GBK Collate=gbk_bin;
--2. Inserting a line down index information table
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. Synchronize the offline index information table to the line
--4. Building DELETE statements for deleted and modified indexes
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 joins 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. Constructing new add-on statements with new indexes
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 development and production environment index comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.