gpt4 book ai didi

sql - 加入索引和位置

转载 作者:行者123 更新时间:2023-12-03 23:19:23 25 4
gpt4 key购买 nike

给定下一条 SQL 语句:

Select * 
from A join B
on A.id1=B.id1 and
A.id2=B.id2
where A.year=2016
and B.year=2016

并且知道表 A 比表 B 小很多,所以我需要数据库先按年访问 A 表,然后加入,然后按年过滤 B 表,我的问题是:

B 上创建索引(如 (id1,id2,year))以提高性能是否有意义?

非常感谢!

最佳答案

对于此查询:

Select *
from A join
B
on A.id1 = B.id1 and A.id2 = B.id2
where A.year = 2016 and B.year = 2016;

我会建议 A(year, id1, id2)B(id1, id2, year) 上的索引。

您还可以将查询编写为:
Select *
from A join
B
on A.id1 = B.id1 and A.id2 = B.id2 and A.year = B.year
where A.year = 2016;

您的问题的答案是"is", B 上的索引是正确的做法。在这个版本中,索引中列的顺序并不重要。

关于sql - 加入索引和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41263316/

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