gpt4 book ai didi

sql - 大表左连接更高效

转载 作者:行者123 更新时间:2023-12-02 05:07:08 25 4
gpt4 key购买 nike

我有以下(简化的)查询

select P.peopleID, P.peopleName, ED.DataNumber
from peopleTable P
left outer join (
select PE.peopleID, PE.DataNumber
from formElements FE
inner join peopleExtra PE on PE.ElementID = FE.FormElementID
where FE.FormComponentID = 42
) ED on ED.peopleID = P.peopleID

如果没有子查询,此过程大约需要 7 秒,但如果有子查询,则需要大约 3 分钟。

鉴于表 peopleExtra 相当大,是否有更有效的方法来执行该连接(无需重组数据库)?

更多详细信息:

子查询的内部部分,例如

  select PE.peopleID, PE.DataNumber 
from formElements FE
inner join peopleExtra PE on PE.ElementID = FE.FormElementID
where FE.FormComponentID = 42

执行时间为 <1 到 5 秒,并返回 95k 行

peopleTable 中有 1500 个条目。

最佳答案

您的查询没问题,只需创建以下索引:

PeopleExtra (PeopleID) INCLUDE (DataNumber, ElementID)
FormElements (FormComponentID, FormElementID)

不需要重写连接(SQL Server 的优化器可以很好地处理嵌套查询),尽管它可以使您的查询更易于阅读。

关于sql - 大表左连接更高效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2766570/

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