[英]In this demo,should I create a index on each column or just one Nonclustered index (Index Scan、Seek)
Test DDL: 测试DDL:
CREATE TABLE TestTable([col1] varchar(2), [col2] varchar(2), [col3] varchar(2));
INSERT INTO TestTable ([col1], [col2], [col3]) VALUES ('a1', 'b1', 'c1');
example1(just one Nonclustered Index): example1(只有一个非聚集索引):
Create Nonclustered Index Index_TestTable on TestTable ([col1], [col2], [col3]) ;
select [col2], [col3] from TestTable
where [col2] = 'b1' ;
result: 结果:
Index Scan
example2(two Nonclustered Index): example2(两个非聚集索引):
Create Nonclustered Index Index_TestTable on TestTable ([col1], [col2], [col3]) ;
Create Nonclustered Index Index_TestTable2 on TestTable ([col2], [col1], [col3]) ;
select [col2], [col3] from TestTable
where [col2] = 'b1' ;
result: 结果:
Index Seek
Q. Should I create a index on each column or just one index? 问:我应该在每一列上创建一个索引还是仅创建一个索引?
ps. PS。 select .. where col1,col2,col3
's columns-order are random. select .. where col1,col2,col3
的列顺序是随机的。
如果您总是要使用col2
查询表,那么您需要在索引中包括col1和col3,您可以为col2
创建一个索引,如下所示:
Create Nonclustered Index Index_TestTable on TestTable ([col2]) ;
You have not provided any information on why you need an index on those columns, but as a general rule of thumb, I can tell you that creating an index on every single column of your table is not the right approach. 您尚未提供有关为什么需要在这些列上建立索引的任何信息,但是根据一般经验,我可以告诉您,在表的每个单列上创建索引都不是正确的方法。
Non-clustered index could improve your search performance but would have a negative impact on your insert/update/delete operation (and you could waste storage space). 非聚集索引可以提高搜索性能,但会对插入/更新/删除操作产生负面影响(并且可能浪费存储空间)。
If you add a new non-clustered index to the table, in practice a new table is created to maintain that index. 如果在表中添加新的非聚集索引,则实际上会创建一个新表来维护该索引。 For example if you create a non-clustered index on LastName column, then you would have a new table created to store a sorted list of LastName s with a pointer to the corresponding row. 例如,如果您在LastName列上创建非聚集索引,则将创建一个新表来存储LastName的排序列表以及指向相应行的指针。 This could improve your performance if you are searching the table on LastName but it has a negative impact on your table update, because every time you update the table you need to update/sort the index too. 如果您在LastName上搜索表,这可能会提高性能,但对表更新有负面影响,因为每次更新表时,您也需要更新/排序索引。
Non-clustered index should only be created on a column that you regularly search on. 非聚集索引只能在您定期搜索的列上创建。
I totally agree with Hooman's comments. 我完全同意Hooman的评论。 However, I just want to explain the reason why you got Index Seek in example2 as follows: 但是,我只想解释一下为什么在example2中获得Index Seek的原因,如下所示:
Since TestTable
only contains three columns which are all included in the non-clustered index, their order in the index caused the differences. 由于TestTable
仅包含三列,所有列均包含在非聚集索引中,因此它们在索引中的顺序导致了差异。
In example2, since index Index_TestTable2
has [col2] first, only a single seek is performed (based on where [col2] = 'b1'
). 在example2中,由于索引Index_TestTable2
具有[col2],因此仅执行一次查找(基于where [col2] = 'b1'
)。 Therefore, if you remove index Index_TestTable
in example2, you should still get Index Seek. 因此,如果在example2中删除索引Index_TestTable
,则仍应获得“索引查找”。 please refer to here for more details. 请参阅此处了解更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.