简体   繁体   English

SQL Server 2005中的复合聚集索引和非聚集索引

[英]Composite clustered index and non clustered index in sql server 2005

I created a new table with composite primary key, Lets say PrmID and Type, therefor a new composite clustered index has created (PrmID is first).I add another non clustered index for Type. 我用组合主键(假设PrmID和Type)创建了一个新表,因此创建了一个新的组合聚簇索引(第一个是PrmID)。我为Type添加了另一个非聚簇索引。

My question is - when I generate a queries that perform any statement on Type (such as GROUP BY), is the SQL engine using the non clustered index table or the PK clustered index (which more expensive for that kind of queries) ? 我的问题是-当我生成对Type执行任何语句的查询时(例如GROUP BY),SQL引擎使用的是非聚集索引表还是PK聚集索引(对于此类查询而言更昂贵)?

-- EDIT -- -编辑-

Thanks marc, I missed that... 谢谢马克,我错过了...

-- END EDIT -- -结束编辑-

Second, is PrimID really not unique in your table, and is only unique in combination with Type? 其次,PrimID在表中是否真的不是唯一的,并且仅与Type结合使用才唯一? If PrimID is not duplicated in the table, maybe reconsider making it a composite PK. 如果表中没有重复PrimID,请重新考虑使其成为复合PK。

Third, the best way to answer these types of questions is to look at the execution plan for your query. 第三,回答这些类型问题的最佳方法是查看查询的执行计划。 We can give you an answer on how we think SQL should handle the plan, but SQL Server will change the execution plan for various circumstances based on the data in your database, your hardware, etc... 我们可以给你我们是怎么的SQL应该处理计划的答案,但SQL Server将更改基于您的数据库,硬件等数据的各种情况下的执行计划...

Here's what the execution plan will look like: 执行计划如下所示: 在此处输入图片说明

You can see that it tells you excatly which idexes are used, and how they are used... here's an article that gives a good introduction into understanding the execution plans. 您可以看到它很好地告诉您使用了哪些idexe,以及如何使用它们... 这是一篇文章,对理解执行计划进行了很好的介绍。

It depends on what the query look like, what columns are accessed, is it covering etc 它取决于查询的外观,访问的列,覆盖范围等。

A simple GROUP BY on type will most likely use the NC index. 一个简单的GROUP BY on类型很可能会使用NC索引。 If you use other columns you may get a bookmap/key lookup or the index will be ignored and you'll have an inefficient PK scan 如果您使用其他列,则可能会进行书本地图/键查找,否则索引将被忽略,并且PK扫描效率低下

My understanding is that when you have a multi-field index and your query is not using the first field in the index, then that index will not be used (too difficult, not efficient, etc.) It will then use the other index. 我的理解是,当您具有多字段索引并且查询使用索引中的第一个字段时,则将不使用该索引(太困难,效率不高等),然后将使用其他索引。

But, to know this definitely , run your query with an execution plan, or even just an estimated execution plan. 但是,要明确知道这一点 ,请使用执行计划甚至只是估计的执行计划来运行查询。 If you are using SSMS this is easy as pressing a tool bar button. 如果您使用的是SSMS,则只需按工具栏按钮即可。 It is the one that looks like three little blue and green boxes in an inverted "L" pattern. 它看起来像是三个小小的蓝色和绿色方框,呈倒“ L”形。 This will tell you exactly what index is being used. 这将准确告诉您正在使用什么索引。 And while execution plans can change over the life of a query (as the data changes), it should not for this answer. 而且,尽管执行计划可以在查询的整个生命周期中发生变化(随着数据的变化),但不应针对此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM