两列上的聚集索引
我有一个多对多表,比方说:
PersonJob(personId,jobId)
带有聚集索引(personId,jobId)。
问题是:
如果在 SQL 中的某个位置我将进行如下查询:
SELECT *
FROM PersonJob JOIN Job ON PersonJob.jobId = Job.jobId
.......
它会利用该聚集索引来查找 PersonJob 表中具有特定 jobId 的记录吗?或者我最好在 PersonJob 表中的 jobId 列上创建新的非聚集非唯一索引?
谢谢 帕维尔
I've a many-to-many table, let's say:
PersonJob(personId,jobId)
with clustered index (personId,jobId).
The question is:
If somewhere in SQL I'll make a query like:
SELECT *
FROM PersonJob JOIN Job ON PersonJob.jobId = Job.jobId
.......
will it take advantage of that clustered index to find records with particular jobId in PersonJob table ? Or I would be better of creating new non-clusterd non-unique index on jobId column in PersonJob table?
Thanks
Pawel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会从聚集索引中获得任何优势,并且您的查询仍然需要扫描 PersonJob 表的所有行。
如果聚集索引(jobID、personId)中的列颠倒了,那么您将利用该索引。
考虑聚集索引根据形成索引的列的值对表中的实际行进行排序。因此,通过 (personId, jobID) 上的聚集索引,您可以将具有相同 personId 的所有行“分组”在一起(按 jobID 的顺序),但具有相同 jobID 的行仍然分散在表中。
You will not have any advantage from the clustered index and your query would still need to scan all rows of the PersonJob table.
If the columns were reversed in your clustered index (jobID, personId) then you would take advantage of the index.
Consider that a clustered index sorts the actual rows in your table by the values of the columns that form the index. So with a clustered index on (personId, jobID) you have all the rows with the same personId "grouped" together (in order of jobID), but the rows with the same jobID are still scattered around the table.