Query for tables with the same index name and field name and find duplicate indexes

Source: Internet
Author: User


View statistics Structure:
Information_schema 02:51:12>DESC Statistics;
+---------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| Table_catalog | VARCHAR (512) |     NO |         |       | |
| Table_schema | VARCHAR (64) |     NO |         |       | |
| table_name | VARCHAR (64) |     NO |         |       | |
| Non_unique | bigint (1) |     NO | |       0 | |
| Index_schema | VARCHAR (64) |     NO |         |       | |
| index_name | VARCHAR (64) |     NO |         |       | |
| Seq_in_index | bigint (2) |     NO | |       0 | |
| column_name | VARCHAR (64) |     NO |         |       | |
| Collation | varchar (1) |     YES | |       NULL | |
| Cardinality | bigint (21) |     YES | |       NULL | |
| Sub_part | bigint (3) |     YES | |       NULL | |
| PACKED | varchar (10) |     YES | |       NULL | |
| NULLABLE | varchar (3) |     NO |         |       | |
| Index_type | varchar (16) |     NO |         |       | |
| COMMENT | varchar (16) |     YES | |       NULL | |
| index_comment | varchar (1024) |     NO |         |       | |
+---------------+---------------+------+-----+---------+-------+
Rows in Set (0.00 sec)

Query Use cases:
Select Table_schema,table_name,column_name,index_name from Information_schema.statistics where
Table_schema not in (' MySQL ', ' performance_schema ', ' information_schema ', ' Common_schema ') and table_name= ' G_a_task '

+---------------+-------------+---------------+------------------------+
| Table_schema | table_name | column_name | index_name |
+---------------+-------------+---------------+------------------------+
| M_g_ver | G_a_task | _id | PRIMARY |
| M_g_ver | G_a_task | ID | ID |
| M_g_ver | G_a_task | Task_no | Idx_gsat_task_no |
| M_g_ver | G_a_task | Upper_task_no | Idx_gsat_upper_task_no |
+---------------+-------------+---------------+------------------------+

View columns Structure:
DESC columns;
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| Table_catalog | VARCHAR (512) |     NO |         |       | |
| Table_schema | VARCHAR (64) |     NO |         |       | |
| table_name | VARCHAR (64) |     NO |         |       | |
| column_name | VARCHAR (64) |     NO |         |       | |
| ordinal_position | bigint (unsigned) |     NO | |       0 | |
| Column_default | Longtext |     YES | |       NULL | |
| is_nullable | varchar (3) |     NO |         |       | |
| Data_type | VARCHAR (64) |     NO |         |       | |
| Character_maximum_length | bigint (unsigned) |     YES | |       NULL | |
| Character_octet_length | bigint (unsigned) |     YES | |       NULL | |
| numeric_precision | bigint (unsigned) |     YES | |       NULL | |
| Numeric_scale | bigint (unsigned) |     YES | |       NULL | |
| datetime_precision | bigint (unsigned) |     YES | |       NULL | |
| Character_set_name | varchar (32) |     YES | |       NULL | |
| Collation_name | varchar (32) |     YES | |       NULL | |
| Column_type | Longtext |     NO | |       NULL | |
| Column_key | varchar (3) |     NO |         |       | |
| EXTRA | varchar (27) |     NO |         |       | |
| privileges | varchar (80) |     NO |         |       | |
| column_comment | varchar (1024) |     NO |         |       | |
+--------------------------+---------------------+------+-----+---------+-------+
Rows in Set (0.00 sec)

Query use case:
Select table_name, column_name from Information_schema.columns where Table_schema don't in (' MySQL ', ' Performance_schema ', ' information_schema ', ' Common_schema ')
and column_name= ' vamp_no ';
+------------------- ----+-------------+
| table_name            | column_name |
+-----------------------+-------------+
| go_task                | vamp_no     |
| g_t_y | vamp_no     |
| g_a_task           | vamp_no     |
| gs_ship_task          | vamp_no     |
+-----------------------+-------------+

Query for tables and fields with the same index name and field name:
Method One:
Select Table_schema,table_name,column_name,index_name from Information_ Schema.statistics i where 
I.table_schema not in (' MySQL ', ' performance_schema ', ' information_schema ', ' common _schema ') and i.index_name=i.column_name;
Method Two:
Select C.table_schema, C.table_name, c.column_name,i.index_name from Information_schema.statistics C, information_schema.statistics  I
 where c.table_schema not in (' MySQL ', ' performance_schema ', ' Information_schema ', ' Common_schema ')
and C.column_name=i.index_name and C.table_name=i.table_name and C.COLUMN_ Name=i.column_name;
Method Three:
Select C.table_schema, C.table_name, c.column_name,i.index_name from Information_schema.columns C, information_schema.statistics  I
 where c.table_schema not in (' MySQL ', ' performance_schema ', ' Information_schema ', ' Common_schema ')
and C.column_name=i.index_name and C.table_name=i.table_name and C.COLUMN_ Name=i.column_name

Verify that three of the above methods are correct:

The following field names Vamp_no and index names are the same
Show CREATE TABLE M_g_ver.g_t_y\g
PRIMARY KEY (' _id '),
UNIQUE KEY ' id ' (' id '),----field and index name
KEY ' idx_gotr_task_no ' (' task_no '),
KEY ' vamp_no ' (' vamp_no ')----field and index name are the same

Method One:
Select a.* from (
Select Table_schema,table_name,column_name,index_name from Information_ Schema.statistics i where 
I.table_schema not in (' MySQL ', ' performance_schema ', ' information_schema ', ' common _schema ') and I.index_name=i.column_name) a
where a.table_name= ' g_t_y ';
+---------------+------------------- ----+-------------+------------+
| table_schema  | table_name             | column_name | index_name |
+---------------+-----------------------+-------------+------------+
| m_g_ver | g_t_y | id           | id         |
| m_g_ver | g_t_y | vamp_no     | vamp_no    |
+---------------+-----------------------+-------------+------------+


Method Two:
Select a.* from (
Select C.table_schema, C.table_name, c.column_name,i.index_name from information_s Chema.statistics c,information_schema.statistics  I
 where c.table_schema not in (' MySQL ', ' performance_ Schema ', ' information_schema ', ' Common_schema ')
and C.column_name=i.index_name and C.table_name=i.table_name and C . Column_name=i.column_name) a
where a.table_name= ' g_t_y ';
+---------------+-----------------------+-------- -----+------------+
| table_schema  | table_name             | column_name | index_name |
+---------------+-----------------------+-------------+------------+
| m_g_ver | g_t_y | id           | id         |
| m_g_ver | g_t_y | vamp_no     | vamp_no    |
+---------------+-----------------------+-------------+------------+

Method Three:
Select a.* from (
Select C.table_schema, C.table_name, c.column_name,i.index_name from Information_ Schema.columns c,information_schema.statistics  I
 where c.table_schema not in (' MySQL ', ' performance_ Schema ', ' information_schema ', ' Common_schema ')
and C.column_name=i.index_name and C.table_name=i.table_name and C . Column_name=i.column_name) a
where a.table_name= ' g_t_y ';
+---------------+-----------------------+-------- -----+------------+
| table_schema  | table_name            | column_name | index_name |
+---------------+-----------------------+-------------+------------+
| m_g_ver | g_t_y | id           | id         |
| m_g_ver | g_t_y | vamp_no     | vamp_no    |
+---------------+-----------------------+-------------+------------+

Find Duplicate index:
SELECT table_name,column_name,count (1) CT
From Information_schema. ' STATISTICS '
WHERE seq_in_index=1
and Table_schema= ' M_g_ver '
GROUP by Table_name,column_name
Having CT >=2;
+----------------+-------------+----+
| table_name | column_name | CT |
+----------------+-------------+----+
| G_s_so |  Bking_no | 2 |
+----------------+-------------+----+
1 row in Set (0.03 sec)


The UNIQUE KEY ' unique_booking_no ' (' bking_no ') USING btree,
KEY ' idx_gsss_booking_no ' (' Bking_no ')

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.