“join”上的聚集索引栏或“哪里”柱子
我在 SQL Server 中有两个表。
表 A 有两列:ID 和名称。他们都是独一无二的。
表 B 在名称列上连接到表 A。
假设对这些表的大多数查询将是:
Select A.Name, B.xyz From A Inner Join B on A.Name = B.Name Where ID = 1
我要在表 A 中的哪一列上创建聚集索引? ID 来加速 WHERE 还是 Name 来加速 JOIN?
I have two tables in SQL Server.
Table A has two columns: ID and Name. They are both unique.
Table B is joined to table A on the Name column.
Assuming most of the queries to these tables will be:
Select A.Name, B.xyz From A Inner Join B on A.Name = B.Name Where ID = 1
Which column in table A do I want to create a clustered index on? ID to speed up the WHERE or Name to speed up the JOIN?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您根本不需要聚集索引。您只需要一个常规索引。 (除非您希望按顺序读取所有数据。)
您真正想要的是两个字段一起的索引,首先是
ID
,然后是Name
。所以三个索引:
创建最后一个索引的唯一原因是您的数据库是否支持索引合并。否则,您只需要
ID
上的索引。索引中不使用A.Name
,A.Name
是数据,而不是搜索键。您需要B.Name
上的索引。I don't think you want a clustered index at all. You want just a regular index. (Unless you expect to read all the data in sequential order.)
What you do want is an index on both fields together, with
ID
first andName
second.So three indexes:
The only reason to create the last index is if your database supports index merges. Otherwise you want just the index on
ID
.A.Name
isn't used from the index,A.Name
is data, not a search key. You want an index onB.Name
.NAME 列,因为它是唯一的,并且您更有可能对此执行搜索/联接。
The NAME column since it's unique and you are more likely to perform searches/joins on this one.