SQL Server构建采样统计时,采样百分比是如何计算的?
例如,更新Stack Overflow 2013 数据库中dbo.Votes表的PostId列的统计信息:
UPDATE STATISTICS dbo.Votes
(
_WA_Sys_00000002_0AD2A005
)
WITH MAXDOP = 1;
SQL Server 构建一个内部语句:
SELECT
StatMan
(
[SC0],
[SB0000]
)
FROM
(
SELECT TOP 100 PERCENT
[SC0],
step_direction([SC0]) over (order by NULL) AS [SB0000]
FROM
(
SELECT [PostId] AS [SC0]
FROM [dbo].[Votes]
TABLESAMPLE SYSTEM (9.234204e-01 PERCENT)
WITH (READUNCOMMITTED)
) AS _MS_UPDSTATS_TBL_HELPER
ORDER BY
[SC0],
[SB0000]
) AS _MS_UPDSTATS_TBL
OPTION (MAXDOP 1)
它从哪里来9.234204e-01 PERCENT
?
采样百分比是根据表中的行数和为统计信息收集选择的索引中的叶级页数得出的。该“索引”通常是基表(聚集索引或堆)。
这两个输入参数可以从 DMV 获得:
计算公式为:
这将返回:
9.234204e-01 PERCENT
这与问题中给出的相符。