在 varchar 键上连接多个表 - MYSQL:性能问题

Joining mulitple tables on a varchar key - MYSQL : Performance Issues

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

我的数据库设计如下:

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

Tbl2: ID - Topic_ID

Tbl3: ID - Location_ID

Tbl4: ID - Status_ID

第一个 ID 始终相同,并且两列上始终有键。加入 2 table 很容易,但是一旦我尝试加入第 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 秒。

我现在的问题是 table2 的所有 tables execpt 都有一个 1:1 关系,其中一个 ID 可能有多个主题。

我希望我能很好地解释我的问题。

非常感谢您的帮助

你能试试下面的查询并检查一次吗?我刚刚在您的派生 table 中添加了一个 group by 子句。我的感觉是 group by 会给你 ID 和 TIME 的唯一组合,因此会减少你派生的 table 中生成的记录集,进而降低性能。

            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