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

相关推荐
码事漫谈4 小时前
别写Prompt了,现在流行给AI“写循环”
后端
Kyrie_Li5 小时前
Spring Boot Kafka 生产级配置全解析:从入门到精通
spring boot·后端·kafka
Coder_Shenshen6 小时前
西门子S7CommPlus协议鉴权算法原理与流程详解
网络·后端·算法
南墙上的石头6 小时前
麒麟 V10 重装人大金仓 V8R6 踩坑实录(含 MySQL 兼容模式)
数据库·mysql
yuhaiqiang7 小时前
随手 vibecoding 的浏览器插件已经 6000 多次下载,聊聊他的产品设计
前端·后端·面试
geovindu8 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
执子手 吹散苍茫茫烟波9 小时前
RC 隔离级别下 MySQL InnoDB 死锁典型案例
数据库·mysql
卷无止境10 小时前
C++ 存储类说明符(Storage Class Specifier)大横评
c++·后端
用户0190275816110 小时前
量化数据的 batch 接口有多好用?从 1 只到 500 只,批量拉数据的正确姿势
后端
rruining10 小时前
Java设计模式——结构型
后端