gpt4 book ai didi

mysql - 我如何优化此查询,执行时间超过一分钟

转载 作者:行者123 更新时间:2023-11-29 00:47:41 24 4
gpt4 key购买 nike

我有以下查询需要一分多钟才能执行,我该如何优化它。由于 order by o.id desc,它很慢,如果我删除它,查询会在几毫秒内执行。


select o.*, per.email, p.name
from order o
inner join product p
on o.product_id=p.id
inner join person per
on o.person_id=per.id
order by o.id desc
limit 100;

下面是解释的结果


1 SIMPLE p index PRIMARY FK2EFC6C1E5DE2FC 8 NULL 6886 Using index; Using temporary; Using filesort
1 SIMPLE o ref FK67E9050121C383DB,FK67E90501FC44A17C FK67E90501FC44A17C 8 dev.p.id 58
1 SIMPLE per eq_ref PRIMARY PRIMARY 8 dev.o.person_id 1 Using index

所有表都是 InnoDB 并且连接基于主键和外键。除此之外,索引位于 Personemail 列和 Order

status

每张表的记录数

人:1,300,000产品:7,000订单:70,000

最佳答案

计划者很可能没有使用 limit 提示在连接之前从订单表中删除行。所以服务器必须对所有行进行连接,然后只返回几行。

试试这个:

select o.* from
(select * order order by id desc limit 100) o
inner join product p
on o.product_id=p.id
inner join person per
on o.person_id=per.id
order by o.id desc limit 100;

编辑:仅当存在保证相应行存在于 Product 和 Person 表中的约束时,这才有效。

关于mysql - 我如何优化此查询,执行时间超过一分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9924643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com