MySQL索引优化-show index

查询数据库索引有很多种方法,本文只讨论 show index语句,先给出基础语法:

sql 复制代码
SHOW [EXTENDED] {INDEX | INDEXES | KEYS} 
    {FROM | IN} tbl_name
    [{FROM | IN} db_name]
    [WHERE expr]

先创建一个表 t_user_order,包含一个主键索引,一个唯一索引,三个普通索引,且随机录入10480条数据,如下:

sql 复制代码
-- auto-generated definition
create table t_user_order
(
    id           int unsigned auto_increment comment '主键自增id'
        primary key,                    -- 主键索引
    code         varchar(64) not null comment '订单号',
    uid          varchar(64) not null comment '用户uid',
    pay_amount   int         not null comment '支付金额',
    pay_status   tinyint     not null comment '支付状态,1:支付中,2:支付成功,4:支付关闭',
    order_status tinyint     not null comment '订单状态,1:已创建,2:已收款,4:已完成,8:已关闭',
    constraint uniq_code
            unique (code)               -- 唯一索引
);

create index idx_order_status
    on t_user_order (order_status);     -- 普通索引

create index idx_pay_amount
    on t_user_order (pay_amount);       -- 普通索引

create index idx_uid
    on t_user_order (uid)
    comment '用户uid索引';               -- 普通索引

接下来执行下面任意一条SQL,结果都是相同的:

sql 复制代码
show index from t_user_order;
show index from t_user_order in test;
show index from test.t_user_order;

show keys from t_user_order;
show keys in t_user_order from test;
show keys from test.t_user_order;

show indexes from t_user_order;
show indexes from t_user_order in test;
show indexes from test.t_user_order;

输出:

Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
t_user_order 0 PRIMARY 1 id A 10379 BTREE YES
t_user_order 0 uniq_code 1 code A 10638 BTREE YES
t_user_order 1 idx_order_status 1 order_status A 4 BTREE YES
t_user_order 1 idx_pay_amount 1 pay_amount A 358 BTREE YES
t_user_order 1 idx_uid 1 uid A 835 BTREE 用户uid索引 YES

可选的 EXTENDED 关键字会输出包括 MySQL 内部的隐藏索引信息,举个栗子:

sql 复制代码
show extended index from t_user_order in test;

输出结果:

Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
t_user_order 0 PRIMARY 1 id A 10379 null null BTREE YES null
t_user_order 0 PRIMARY 2 DB_TRX_ID A null null null BTREE YES null
t_user_order 0 PRIMARY 3 DB_ROLL_PTR A null null null BTREE YES null
t_user_order 0 PRIMARY 4 code A null null null BTREE YES null
t_user_order 0 PRIMARY 5 uid A null null null BTREE YES null
t_user_order 0 PRIMARY 6 pay_amount A null null null BTREE YES null
t_user_order 0 PRIMARY 7 pay_status A null null null BTREE YES null
t_user_order 0 PRIMARY 8 order_status A null null null BTREE YES null
t_user_order 0 uniq_code 1 code A 10638 null null BTREE YES null
t_user_order 0 uniq_code 2 id A null null null BTREE YES null
t_user_order 1 idx_order_status 1 order_status A 4 null null BTREE YES null
t_user_order 1 idx_order_status 2 id A null null null BTREE YES null
t_user_order 1 idx_pay_amount 1 pay_amount A 358 null null BTREE YES null
t_user_order 1 idx_pay_amount 2 id A null null null BTREE YES null
t_user_order 1 idx_uid 1 uid A 835 null null BTREE 用户uid索引 YES null
t_user_order 1 idx_uid 2 id A null null null BTREE 用户uid索引 YES null

where子句则可以对输出的表格做进一步的过滤。

然后给出输出结果的表格头代表的含义:

  • Table
    表名
  • Non_unique
    如果索引不能包含重复内容,则为 0;如果可以,则为 1。
  • Key_name
    索引的名称。如果索引是主键,则名称总是 PRIMARY。
  • Seq_in_index
    索引中的列序列号,从 1 开始。
  • Column_name
    列名
  • Collation
    列在索引中的排序方式。其值可以是 A(升序)、D(降序)或 NULL(未排序)。
  • Cardinality
    估算的唯一值数量(基于采样统计),可以运行 ANALYZE TABLE 来更新。它的值是根据以整数形式存储的统计数据计算的,所以即使是小表,值也不一定精确。值越大,MySQL 在进行连接时使用索引的机会就越大。
  • Sub_part
    索引前缀。如果列只有部分索引,则为索引字符数;如果整个列都有索引,则为 NULL。
  • Packed
    索引压缩方式(如COMPRESSED行格式),默认NULL表示未压缩
  • Null
    索引是否包含空值。
  • Index_type
    索引结构类型(BTREEFULLTEXTHASHRTREE),
  • Comment
    索引扩展信息: disabled=索引被禁用 ;INVISIBLE=MySQL 8.0+不可见索引
  • Index_comment
    索引注释
  • Visible
    索引是否可见(详细可点击查看
  • Expression
    MySQL 8.0.13 及更高版本支持函数索引部分(详细可点击查看),如果使用了函数索引,Column_name 列为空,Expression 表示索引部分的表达式。

参考文档:dev.mysql.com/doc/refman/...

相关推荐
lzhdim17 分钟前
SQL 入门 15:SQL 事务:从 ACID 到四种常见的并发问题
数据库·sql
敲个大西瓜1 小时前
Java项目常用数据归档方式
mysql
IT_陈寒2 小时前
Redis缓存击穿把我整不会了,原来还有这手操作
前端·人工智能·后端
kyriewen3 小时前
面试官让我查各部门工资最高的员工,我用AI三秒写出窗口函数,他愣了
后端·mysql·面试
文心快码BaiduComate3 小时前
干货|Comate Harness Engineering工程实践指南
前端·后端·程序员
光辉GuangHui3 小时前
Agent Skill 也需要测试:如何搭建 Skill 评估框架
前端·后端·llm
小码工作室3 小时前
使用 HAVING 进行 MySQL 集合筛选
mysql
我是谁的程序员3 小时前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
后端·ios
irving同学462383 小时前
Node 后端实战:JWT 认证与生产级错误处理
前端·后端
Master_Azur4 小时前
单元测试——Junit单元测试框架
后端