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/...

相关推荐
rebel3 分钟前
若依框架整合 CXF 实现 WebService 改造流程(后端)
java·后端
极客悟道13 分钟前
颠覆传统虚拟化:在Docker容器中运行Windows系统的开源黑科技
前端·后端
调试人生的显微镜41 分钟前
WebView 中 Cookie 丢失怎么办?跨域状态不同步的调试与修复经验
后端
weixin_437398211 小时前
转Go学习笔记(2)进阶
服务器·笔记·后端·学习·架构·golang
极客悟道1 小时前
巧解 Docker 镜像拉取难题:无需梯子和服务器,拉取数量无限制
后端·github
aiopencode1 小时前
iOS 出海 App 安全加固指南:无源码环境下的 IPA 加固与防破解方法
后端
liangdabiao1 小时前
AI一人公司?先搞定聚合支付!一天搞定全能的聚合支付系统
后端
AillemaC2 小时前
三分钟看懂回调函数
后端
yeyong2 小时前
越学越糟心,今天遇到又一种新的服务控制方式 snap,用它来跑snmpd
后端
喷火龙8号2 小时前
深入理解MSC架构:现代前后端分离项目的最佳实践
后端·架构