SQL面试题:第二个优化案例

今天继续给大家分享一个 SQL 优化案例。

问题描述

已知表结构如下:

sql 复制代码
CREATE TABLE `customer` (
`C_CUSTKEY` int NOT NULL,
`C_NAME` varchar(25) NOT NULL,
`C_ADDRESS` varchar(40) NOT NULL,
`C_NATIONKEY` int NOT NULL,
`C_PHONE` char(15) NOT NULL,
`C_ACCTBAL` decimal(15,2) NOT NULL,
`C_MKTSEGMENT` char(10) NOT NULL,
`C_COMMENT` varchar(117) NOT NULL,
PRIMARY KEY (`C_CUSTKEY`)
) ENGINE=InnoDB;

CREATE TABLE `orders` (
`O_ORDERKEY` int NOT NULL,
`O_CUSTKEY` int NOT NULL,
`O_ORDERSTATUS` char(1) NOT NULL,
`O_TOTALPRICE` decimal(15,2) NOT NULL,
`O_ORDERDATE` date NOT NULL,
`O_ORDERPRIORITY` char(15) NOT NULL,
`O_CLERK` char(15) NOT NULL,
`O_SHIPPRIORITY` int NOT NULL,
`O_COMMENT` varchar(79) NOT NULL
PRIMARY KEY (`C_ORDERKEY`)
) ENGINE=InnoDB;

请问下面的查询语句应该如何优化?

sql 复制代码
SELECT o_custkey, c_name, sum(o.o_totalprice)
FROM customer c, orders o 
WHERE o_custkey = c_custkey 
GROUP BY o_custkey, c_name
ORDER BY c_name;

思路分析

首先,这个查询的作用是统计每个客户的订单金额汇总,数据来自 orders 表;最后按照客户名称排序,名称来自 customer 表,两个表通过 c_custkey 字段关联。看一下执行计划:

sql 复制代码
EXPLAIN
select c_custkey, c_name, sum(o.o_totalprice)
from customer c, orders o 
where o_custkey = c_custkey 
group by c_name, c_custkey
order by c_name;

id|select_type|table|partitions|type  |possible_keys|key    |key_len|ref           |rows|filtered|Extra                          |
--+-----------+-----+----------+------+-------------+-------+-------+--------------+----+--------+-------------------------------+
 1|SIMPLE     |o    |          |ALL   |             |       |       |              |   1|   100.0|Using temporary; Using filesort|
 1|SIMPLE     |c    |          |eq_ref|PRIMARY      |PRIMARY|4      |hr.o.O_CUSTKEY|   1|   100.0|                               |

orders 表使用了 ALL 全表扫描,由于 group by,所以 Extra 包含了临时表(Using temporary);由于 order by,所以 Extra 包含了排序操作(Using filesort)。这些都会导致性能问题。

第一步,考虑到每个客户的订单金额汇总只需要 orders 表中的数据,可以单独按照 o_custkey 分组,并且基于 o_custkey 和 o_totalprice 创建复合索引,利用索引覆盖扫描优化;然后再和 customer 表关联。另外,c_custkey 是主键,决定了 c_name,延迟关联可以避免 group by 中的 c_name。

sql 复制代码
-- 创建索引
CREATE INDEX idx_orders_custkey_totalprice ON orders(o_custkey, o_totalprice);

EXPLAIN
select o.o_custkey, c.c_name, o.sumprice
from
(select o_custkey, sum(o_totalprice) sumprice
from orders 
group by o_custkey) o, customer c
where o_custkey = c_custkey 
order by c_name;

id|select_type|table     |partitions|type |possible_keys                |key                          |key_len|ref           |rows|filtered|Extra         |
--+-----------+----------+----------+-----+-----------------------------+-----------------------------+-------+--------------+----+--------+--------------+
 1|PRIMARY    |c         |          |ALL  |PRIMARY                      |                             |       |              |   1|   100.0|Using filesort|
 1|PRIMARY    |<derived2>|          |ref  |<auto_key0>                  |<auto_key0>                  |4      |hr.c.C_CUSTKEY|   2|   100.0|              |
 2|DERIVED    |orders    |          |index|idx_orders_custkey_totalprice|idx_orders_custkey_totalprice|11     |              |   1|   100.0|Using index   |

子查询 o 已经优化完成,Extra 中的 Using index 显示了索引覆盖扫描。剩下的问题就是 customer 表上的排序操作,对应 Extra 中的 Using filesort。

第二步,基于 c_name 字段创建索引,避免排序操作。

sql 复制代码
-- 创建索引
CREATE INDEX idx_customer_name ON customer(c_name);

EXPLAIN
select c.c_custkey, c.c_name, o.sumprice
from
(select o_custkey, sum(o_totalprice) sumprice
from orders 
group by o_custkey) o, customer c
where o_custkey = c_custkey 
order by c_name;

id|select_type|table     |partitions|type |possible_keys                |key                          |key_len|ref           |rows|filtered|Extra      |
--+-----------+----------+----------+-----+-----------------------------+-----------------------------+-------+--------------+----+--------+-----------+
 1|PRIMARY    |c         |          |index|PRIMARY                      |idx_customer_name            |102    |              |   1|   100.0|Using index|
 1|PRIMARY    |<derived2>|          |ref  |<auto_key0>                  |<auto_key0>                  |4      |hr.c.C_CUSTKEY|   2|   100.0|           |
 2|DERIVED    |orders    |          |index|idx_orders_custkey_totalprice|idx_orders_custkey_totalprice|11     |              |   1|   100.0|Using index|

可以看到,对于 customer 表的访问,也使用了索引覆盖扫描,因为二级索引包含了主键字段(c_custkey)。

额外补充,还有一种写法就是调整 group by 字段的顺序,把 c_name 放在第一位,并且把 c_custkey 改成 o_custkey:

sql 复制代码
EXPLAIN 
select o_custkey, c_name, sum(o.o_totalprice)
from customer c, orders o 
where o_custkey = c_custkey 
group by c_name, o_custkey
order by c_name;

id|select_type|table|partitions|type |possible_keys                |key                          |key_len|ref           |rows|filtered|Extra      |
--+-----------+-----+----------+-----+-----------------------------+-----------------------------+-------+--------------+----+--------+-----------+
 1|SIMPLE     |c    |          |index|PRIMARY,idx_customer_name    |idx_customer_name            |102    |              |   1|   100.0|Using index|
 1|SIMPLE     |o    |          |ref  |idx_orders_custkey_totalprice|idx_orders_custkey_totalprice|4      |hr.c.C_CUSTKEY|   1|   100.0|Using index|

留个作业,为什么呢?

相关推荐
时间售卖铺1 小时前
触发器与函数实战:MySQL高效编程秘诀
运维·数据库·mysql·云原生
qetfw1 小时前
UOS MariaDB 数据库:安装、最小权限与备份验证
linux·数据库·mariadb·uos
数智园区合作社2 小时前
问数平台怎么把同一个指标锁成一个数
大数据·数据库·人工智能·ai
lbb 小魔仙2 小时前
Windows搭建PaddleOCR+飞书Webhook自动打卡通知机器人:识别、推送与远程触发完整教程
数据库·人工智能·prometheus·时序数据库
captain3763 小时前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis
心中有你02144 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
龙亘川4 小时前
综合素质评价业务建模:从过程性评价到成长档案全链路闭环
数据库·智慧城市·数据可视化
数据库小组4 小时前
NineData 实战教程:MySQL 5.5 数据迁移同步到 MySQL 8.0(跨版本升级)
数据库·mysql·mysql优化·数据库开发·数据库迁移·版本升级·数据复制
zhangjw345 小时前
1-5 PostgreSQL 数据类型全集:基础类型、字符串、数值、时间类型
数据库·postgresql
王中阳Go5 小时前
5.27 分钟干完后端和全部测试:一场三小时笔试的五个失分点,四个不在代码上
大数据·数据库·人工智能·后端·大语言模型