gpt4 book ai didi

mysql - 在 varchar 键上连接多个表 - MYSQL : Performance Issues

转载 作者:行者123 更新时间:2023-11-29 12:07:39 26 4
gpt4 key购买 nike

我的问题是,对于 150 万个数据集,连接查询大约需要 7-15 秒,我想显着减少它,但不知道如何实现。

我有一个数据库设计如下:

Tbl1:ID - 时间 1 - 时间 2 - 时间 2

Tbl2:ID - Topic_ID

Tbl3:ID - Location_ID

Tbl4:ID - Status_ID

第一个 ID 始终相同,并且两个列上始终都有键。很容易加入 2 个表,但一旦我尝试加入第 3 个表,例如:

Select t1.Time1,t2.Topic_ID,t3_Location_id 
from
(select ID,Time1 from tbl1 where time > 400 and < 500 ) as t1
inner join tbl2 as t2 on t2.Id = t1.ID
inner join tbl3 as t3 on t2.id = t3.ID
where t2.topic_ID = 2

时间从 0.02 秒大幅增长到 7-15 秒。

我现在的问题是,除了表 2 之外的所有表都存在 1:1 关系,其中一个 Id 可能有多个主题。

我希望我正确地解释了我的问题。

非常感谢您的帮助

最佳答案

您可以尝试以下查询并检查一次吗?我刚刚在派生表中添加了一个 group by 子句。我的感觉是,group by 将为您提供 ID 和 TIME 的唯一组合,从而减少您的派生表中生成的记录集,进而降低性能。

            Select t1.Time1,t2.Topic_ID,t3_Location_id 
from
(select ID,Time1 from tbl1 where time > 400 and < 500 group by ID,Time1) as t1
inner join tbl2 as t2 on t2.Id = t1.ID
inner join tbl3 as t3 on t2.id = t3.ID
where t2.topic_ID = 2

关于mysql - 在 varchar 键上连接多个表 - MYSQL : Performance Issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201762/

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