简体   繁体   English

SQL Server索引用法取决于列值

[英]Sql server index usage depending on column value

I have a huge table (it has more than 40 milion records) and an index on one of the column. 我有一个巨大的表(它有超过4000万条记录)和其中一个列的索引。 The column is a reference column - typeId. 该列是参考列-typeId。

I run into an issue that while filtering the data on that column using different column values, the index is sometimes being used and sometimes not. 我遇到一个问题,即使用不同的列值过滤该列上的数据时,有时会使用索引,有时却不使用索引。

Eg. 例如。 simply running: 只需运行:

select * from LargeNumberOfItemsTable where typeid=4

makes SQL Server use index, but 使SQL Server使用索引,但是

select * from LargeNumberOfItemsTable where typeid=3

does not use index. 不使用索引。

When the index is not being used, the query takes a long time, but when I force a hint that the index should be used, it takes just seconds to run it. 当不使用索引时,查询会花费很长时间,但是当我强制提示应该使用索引时,只需几秒钟即可运行它。

The strange thing is that statistics have been updated and the index has been rebuilt. 奇怪的是统计信息已更新,索引已重建。

Does anyone have an idea, why such behaviour could occur? 有谁知道,为什么会发生这种现象? I'm looking for some hints where to look for the cause. 我正在寻找一些提示以寻找原因。

SQL Server chooses the plan based on estimated row counts. SQL Server根据估计的行数选择计划。 If the estimated row counts are accurate, the time taken for a full table scan versus a seek and key lookup will depend much on your hardware and available resources. 如果估计的行数准确,则进行全表扫描与进行查找和键查找所需的时间将在很大程度上取决于您的硬件和可用资源。

Make sure indexes were rebuilt with MAXDOP 1. Otherwise, the out-of-order extents will mitigate the benefits of read-ahead scans, causing the plan with the scan to perform more poorly when data are not cached. 确保使用MAXDOP 1重建了索引。否则,乱序范围将减轻预读扫描的好处,从而导致在不缓存数据时,扫描计划的执行效果更差。

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

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