多索引与多列索引 [英] Multiple Indexes vs Multi-Column Indexes
问题描述
我刚刚在SQL Server 2005中的一个表中添加了一个索引,它让我想到了。创建1个索引和定义多个列之间有什么区别,每个列要索引1个索引。
有一些原因, ?
例如
创建非聚簇索引IX_IndexName On TableName
(Column1 Asc,Column2 Asc,Column3 Asc)
与
创建非聚簇索引IX_IndexName1在TableName上
(Column1 Asc)
创建非聚簇索引IX_IndexName2对TableName
Asc)
创建非聚簇索引IX_IndexName3对TableName
(Column3 Asc)
我同意 Cade Roux 。
这篇文章应该可以帮助你正确的:
需要注意的是,聚簇索引应该具有唯一键(我将推荐的标识列)作为第一列。
基本上,它可以帮助你的数据插入到索引的结尾,而不会导致大量的磁盘IO和页面拆分。
其次,如果你正在创建其他索引对你的数据,他们被巧妙地构造,他们将被重用。
例如。假设您在三列上搜索表格:
州,县,邮编。
- 您有时只按状态搜索。
- 您有时根据州和县搜索。
- 您经常按州,县,邮政搜索。
然后是具有州,县,邮政编码的索引。将用于所有这三个搜索。
如果你通过zip单独搜索相当多,那么上述索引不会被使用(由SQL Server反正)作为zip是该索引的第三部分,查询优化程序将不会看到该索引有帮助。
然后,您可以单独在Zip上创建一个将在此实例中使用的索引。
回答你正在寻找的是,它取决于你经常使用的查询的where子句和你的分组。
这篇文章将有很大帮助。 : - )
I've just been adding an Index to a table in SQL Server 2005 and it got me thinking. What is the difference between creating 1 index and defining multiple columns over having 1 index per column you want to index.
Are there certain reasons why one should be used over the other?
For example
Create NonClustered Index IX_IndexName On TableName
(Column1 Asc, Column2 Asc, Column3 Asc)
Versus
Create NonClustered Index IX_IndexName1 On TableName
(Column1 Asc)
Create NonClustered Index IX_IndexName2 On TableName
(Column2 Asc)
Create NonClustered Index IX_IndexName3 On TableName
(Column3 Asc)
I agree with Cade Roux.
This article should get you on the right track:
- Indexes in SQL Server 2005/2008 – Best Practices, Part 1
- Indexes in SQL Server 2005/2008 – Part 2 – Internals
One thing to note, clustered indexes should have a unique key (an identity column I would recommend) as the first column. Basically it helps your data insert at the end of the index and not cause lots of disk IO and Page splits.
Secondly, if you are creating other indexes on your data and they are constructed cleverly they will be reused.
e.g. imagine you search a table on three columns
state, county, zip.
- you sometimes search by state only.
- you sometimes search by state and county.
- you frequently search by state, county, zip.
Then an index with state, county, zip. will be used in all three of these searches.
If you search by zip alone quite a lot then the above index will not be used (by SQL Server anyway) as zip is the third part of that index and the query optimiser will not see that index as helpful.
You could then create an index on Zip alone that would be used in this instance.
I guess the answer you are looking for is that it depends on your where clauses of your frequently used queries and also your group by's.
The article will help a lot. :-)
这篇关于多索引与多列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!