在我的 SQL Server 中创建 tpch 数据库后,我尝试了以下查询:
set statistics io on
DBCC DROPCLEANBUFFERS;
select top 100 * from dbo.lineitem order by l_partkey;
Run Code Online (Sandbox Code Playgroud)
表 lineitem 在 l_partkey 上有一个非聚集索引。我多次发出上述查询,发现每次逻辑读取都不同:
Table 'lineitem'. Scan count 1, logical reads 1019, physical reads 4, read-ahead reads 1760, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'lineitem'. Scan count 1, logical reads 1007, physical reads 4, read-ahead reads 1720, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'lineitem'. …Run Code Online (Sandbox Code Playgroud) 在这篇文章中,作者多次运行查询。我注意到逻辑读取在执行过程中略有不同。总共读了几千页,大约有两页的差异。从上下文中我可以清楚地看出,两次之间不会有写入活动。如果计划发生变化,我预计会有比百分之几更大的变化。
问:哪些因素会导致 SQL Server 在没有数据写入的情况下对同一查询报告不同的逻辑读取计数?