UPDATE STATISTICS…WITH ROWCOUNT后如何重置统计信息


11

为了进行查询调整和测试,您可以通过运行手动将行计数和页计数分配给表的索引统计信息UPDATE STATISTICS。但是,如何将统计信息重新计算/重置为表的实际内容?

--- Create a table..
CREATE TABLE dbo.StatTest (
    i      int NOT NULL,
    CONSTRAINT PK_StatTest PRIMARY KEY CLUSTERED (i)
);
GO

--- .. and give it a thousand-or-so rows:
DECLARE @i int=1;
INSERT INTO dbo.StatTest (i) VALUES (@i);

WHILE (@i<1000) BEGIN;
    INSERT INTO dbo.StatTest (i) SELECT @i+i FROM dbo.StatTest;
    SET @i=@i*2;
END;

虚拟查询:

SELECT i%100, COUNT(*) FROM dbo.StatTest GROUP BY i%100;

...将返回以下查询计划(“索引扫描”中的行估计为1024行)。

万行

运行UPDATE STATISTICS命令。

UPDATE STATISTICS dbo.StatTest WITH ROWCOUNT=10000000;

...计划看起来像这样,现在估计有1000万行:

一千万行

如何在不使用的情况下将行计数重置为表的实际内容WITH ROWCOUNT

我试过WITH FULLSCANWITH RESAMPLEWITH SAMPLE n ROWS,但统计数据行数保持在10万行。插入一行甚至删除所有行都不会更新统计信息,因为更改很小。

Answers:


15

DBCC UPDATEUSAGE与该COUNT_ROWS选项一起使用。

DBCC UPDATEUSAGE 
(   { database_name | database_id | 0 } 
    [ , { table_name | table_id | view_name | view_id } 
    [ , { index_name | index_id } ] ] 
) [ WITH [ NO_INFOMSGS ] [ , ] [ COUNT_ROWS ] ] 

文献资料

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.