mysql - MySql ORDER BY 连接慢,2 个查询快
问题描述
当我提供内联值时,为什么以下查询慢而快?
select u.* from user u
join user_group g on u.group_id = g.id
where g.account_id = 1
order by u.id limit 10;
-- takes ~30ms
select id from user_group where account_id = 1;
-- which is (99,198,297,396,495,594,693,792,891,990)
select * from user
where group_id in (99,198,297,396,495,594,693,792,891,990)
order by id limit 10;
-- takes ~1ms
子查询也很慢。计划与加入相同。
select u.* from user u
where u.group_id in (select id from user_group where account_id = 1)
order by u.id limit 10;
-- ~30ms
所有查询都产生相同的结果。
98 0 99
197 0 198
296 0 297
395 0 396
494 0 495
593 0 594
692 0 693
791 0 792
890 0 891
989 0 990
架构
我有以下简单的表结构,有 100 个帐户 1k user_groups 和 1000 万用户。
create table account(
id int primary key auto_increment
);
create table user_group(
id int primary key auto_increment,
account_id int not null,
foreign key (account_id) references account(id)
);
create table user(
id int primary key auto_increment,
deleted tinyint default 0,
group_id int not null,
foreign key (group_id) references user_group(id)
);
-- I've been trying with this index, but it doesnt seem to help.
create index user_1 on user(group_id, id, deleted);
计划
我不明白为什么 MySql 似乎认为它实际上需要执行完整的连接来过滤数据。我们显然没有从user_group
.
为了比较,我在 PostgreSQL 中尝试了同样的事情,并且两个查询都运行得很快。
为什么它很慢,有没有办法编写快速完成的查询(单个查询!)?子选择不起作用。
这个 dbfiddle 显示了问题。 https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=8ed68310d8ca72e9daef389dc0469a6f
使用 MySQL 8.0.23
谢谢
编辑
以下是完整的会话状态处理程序调试详细信息。看起来慢速查询正在读取与快速查询不同的每个值。
-- FLUSH STATUS;
-- select u.* from user u join user_group g on u.group_id = g.id where g.account_id = 1 order by u.id limit 10;
-- SHOW SESSION STATUS LIKE 'Handler%';
Handler_commit 1
Handler_delete 0
Handler_discover 0
Handler_external_lock 4
Handler_mrr_init 0
Handler_prepare 0
Handler_read_first 0
Handler_read_key 11
Handler_read_last 0
Handler_read_next 100110
Handler_read_prev 0
Handler_read_rnd 0
Handler_read_rnd_next 0
Handler_rollback 0
Handler_savepoint 0
Handler_savepoint_rollback 0
Handler_update 0
Handler_write 0
-- FLUSH STATUS;
-- set @uGroups := (select group_concat(id) from user_group where account_id = 1 group by account_id);
-- select * from user where group_id in (select @uGroups) order by id limit 10;
-- SHOW SESSION STATUS LIKE 'Handler%';
Handler_commit 2
Handler_delete 0
Handler_discover 0
Handler_external_lock 4
Handler_mrr_init 0
Handler_prepare 0
Handler_read_first 0
Handler_read_key 2
Handler_read_last 0
Handler_read_next 19
Handler_read_prev 0
Handler_read_rnd 0
Handler_read_rnd_next 0
Handler_rollback 0
Handler_savepoint 0
Handler_savepoint_rollback 0
Handler_update 0
Handler_write 0
解决方案
使用 straight_join 可以获得我预期的性能。
select straight_join u.* from user u
join user_group g on u.group_id = g.id
where g.account_id = 1
order by u.id limit 10;
推荐阅读
- javascript - 如何隐藏 GIF 并用图像替换(以便使用放大工具)
- google-cloud-platform - Google Compute Engine:启动脚本未在容器优化操作系统中运行
- javascript - 地图中的顺序 axios 请求
- google-cloud-platform - 从带有 GPU 的机器映像创建不带 GPU 的 GCP 实例
- vba - 什么是使用访问 vba 的最佳事件
- python - Python字符串出现计数正则表达式性能
- json - 如何从 Ionic 3 中的 JSON 对象获取不是 URL 的图标值
- python - 无法在 x86 docker 上安装 python for arm
- amazon-web-services - 如何在自动缩放组中使用更新/新的 AMI
- php - 当用户在 PHP 中注册 WordPress 时,如何创建自定义用户角色?