gpt4 book ai didi

mysql - 请帮我分析一下Mysql慢查询

转载 作者:行者123 更新时间:2023-11-29 02:08:21 25 4
gpt4 key购买 nike

我正在尝试分析这个 mysql 查询。它很慢,这里的访问者表大约有 50K 个条目,这个查询永远不会返回。当我尝试解释语句时,我发现访问者表上没有使用索引,尽管索引可用。现在这是我需要帮助解决的大难题。任何提示表示赞赏。

查询:

select distinct
visitor0_.ID as ID130_,

case when visitor0_1_.id is not null then 1 when
visitor0_.ID is not null then 0
end as clazz_

from Visitor visitor0_
left outer join Operator visitor0_1_ on visitor0_.ID=visitor0_1_.id
where (visitor0_.ID not in
(select operator1_.id
from Operator operator1_
inner join Visitor operator1_1_ on operator1_.id=operator1_1_.ID))
and (exists
(select visitorpro2_.ID
from VisitorProfileField visitorpro2_, ProfileField profilefie3_
where visitorpro2_.profileFieldID=profilefie3_.ID
and visitorpro2_.visitorID=visitor0_.ID
and profilefie3_.name='subscription86'
and visitorpro2_.numberVal=1
and visitorpro2_.stringVal='Manual'))

解释输出屏幕截图: http://grab.by/grabs/9c3a629a25fc4e9ec0fa54355d4a092c.png

最佳答案

根据我对您的查询的推断,以下应该产生相同的结果,没有子查询并且性能更快。

select v.ID as ID130_, 0 as clazz_
from Visitor v
left outer join (VisitorProfileField vpf join ProfileField pf
on vpf.profileFieldID = pf.ID)
on v.ID = vpf.visitorID and pf.name='subscription86'
and vpf.numberVal=1 and vpf.stringVal='Manual'
left outer join Operator o on v.ID = o.ID
where o.ID IS NULL;

如果我理解有误,请解释。您的 NOT IN 谓词似乎排除了与 Operator 中的任何 id 匹配的任何 Visitor id。也就是说,子查询生成一个包含两个表中的 all id 的列表,因此 NOT IN 条件等效于 Operator 的外部连接> 和一个简单的测试,其中 o.ID IS NULL

这意味着您的选择列表中的 CASE 表达式是无意义的,因为如果您的条件仅匹配 Visitor 行而不匹配任何行,它肯定为 0在 Operator 中。

我认为您的查询中有些地方很困惑。

此外,您似乎在 VisitorProfileFieldProfileField 表中使用了 EAV 反模式。这会给你带来很多麻烦。

关于mysql - 请帮我分析一下Mysql慢查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2053757/

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