How do I check if an index exists on a table field in MySQL?

前端 未结 10 1279
孤独总比滥情好
孤独总比滥情好 2020-12-12 16:55

I\'ve needed to Google this a couple times, so I\'m sharing my Q/A.

10条回答
  •  悲&欢浪女
    2020-12-12 17:28

    you can use the following SQL statement to check the given column on table was indexed or not

    select  a.table_schema, a.table_name, a.column_name, index_name
    from    information_schema.columns a
    join    information_schema.tables  b on a.table_schema  = b.table_schema and
                                        a.table_name = b.table_name and 
                                        b.table_type = 'BASE TABLE'
    left join (
     select     concat(x.name, '/', y.name) full_path_schema, y.name index_name
     FROM   information_schema.INNODB_SYS_TABLES  as x
     JOIN   information_schema.INNODB_SYS_INDEXES as y on x.TABLE_ID = y.TABLE_ID
     WHERE  x.name = 'your_schema'
     and    y.name = 'your_column') d on concat(a.table_schema, '/', a.table_name, '/', a.column_name) = d.full_path_schema
    where   a.table_schema = 'your_schema'
    and     a.column_name  = 'your_column'
    order by a.table_schema, a.table_name;
    

    since the joins are against INNODB_SYS_*, so the match indexes only came from INNODB tables only

提交回复
热议问题