Sql-Server

重建索引 –> 更新統計資訊?

  • January 28, 2011

如果我在維護計劃中執行 Rebuild Index Task,我是否必須使用 Update Statistcs Task 更新統計資訊?

是的

不重建列統計資訊,僅重建索引統計資訊

--random data
CREATE TABLE dbo.foo (bar int PRIMARY KEY, humbug char(2000))
INSERT dbo.foo (bar, humbug) VALUES (1, 'aaa')
INSERT dbo.foo (bar, humbug) VALUES (2, 'bbb')
INSERT dbo.foo (bar, humbug) VALUES (3, 'ccc')
INSERT dbo.foo (bar, humbug) VALUES (4, 'aaa')
INSERT dbo.foo (bar, humbug) VALUES (5, 'bbb')
INSERT dbo.foo (bar, humbug) VALUES (6, 'ccc')
INSERT dbo.foo (bar, humbug) VALUES (7, 'ccc')
INSERT dbo.foo (bar, humbug) VALUES (8, 'ccc')
INSERT dbo.foo (bar, humbug) VALUES (9, 'ccc')
INSERT dbo.foo (bar, humbug) VALUES (10, 'cc')

INSERT dbo.foo SELECT bar+10, humbug FROM dbo.foo

--generate index stats + column stats
SELECT * FROM foo WHERE bar = 12
SELECT * FROM foo WHERE humbug = 'bbb'

--created date
SELECT
   OBJECT_SCHEMA_NAME([id]) + '.' + OBJECT_NAME([id]),
   NAME,
   STATS_DATE ([id], indid)
FROM
   dbo.sysindexes
WHERE
   OBJECT_NAME([id]) = 'foo'
ORDER BY
   STATS_DATE ([id], indid)  DESC

--Relax, have a coffee

--rebuild
ALTER INDEX ALL ON dbo.foo REBUILD WITH (FILLFACTOR = 90)

--check again
SELECT
   OBJECT_SCHEMA_NAME([id]) + '.' + OBJECT_NAME([id]),
   NAME,
   STATS_DATE ([id], indid)
FROM
   dbo.sysindexes
WHERE
   OBJECT_NAME([id]) = 'foo'
ORDER BY
   STATS_DATE ([id], indid)  DESC

--only index stats were updated

引用自:https://serverfault.com/questions/228122