填充因子 - SQL Server,有没有办法在数据库,服务器或模式中设置填充因子?

时间:2015-12-29 16:36:10

标签: sql sql-server fillfactor

我了解您可以设置默认填充因子,但我想在整个服务器上将现有填充因子更改回默认值。

3 个答案:

答案 0 :(得分:0)

DECLARE @Database VARCHAR(255)   
DECLARE @Table VARCHAR(255)  
DECLARE @cmd NVARCHAR(500)  
DECLARE @fillfactor INT 

SET @fillfactor = 90 

DECLARE DatabaseCursor CURSOR FOR  
SELECT name FROM master.dbo.sysdatabases   
WHERE name NOT IN ('master','msdb','tempdb','model','distribution')   
ORDER BY 1  

OPEN DatabaseCursor  

FETCH NEXT FROM DatabaseCursor INTO @Database  
WHILE @@FETCH_STATUS = 0  
BEGIN  

   SET @cmd = 'DECLARE TableCursor CURSOR FOR SELECT ''['' + table_catalog + ''].['' + table_schema + ''].['' + 
  table_name + '']'' as tableName FROM [' + @Database + '].INFORMATION_SCHEMA.TABLES 
  WHERE table_type = ''BASE TABLE'''   

   -- create table cursor  
   EXEC (@cmd)  
   OPEN TableCursor   

   FETCH NEXT FROM TableCursor INTO @Table   
   WHILE @@FETCH_STATUS = 0   
   BEGIN   

       IF (@@MICROSOFTVERSION / POWER(2, 24) >= 9)
       BEGIN
           -- SQL 2005 or higher command 
           SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')' 
           EXEC (@cmd) 
       END
       ELSE
       BEGIN
          -- SQL 2000 command 
          DBCC DBREINDEX(@Table,' ',@fillfactor)  
       END

       FETCH NEXT FROM TableCursor INTO @Table   
   END   

   CLOSE TableCursor   
   DEALLOCATE TableCursor  

   FETCH NEXT FROM DatabaseCursor INTO @Database  
END  
CLOSE DatabaseCursor   
DEALLOCATE DatabaseCursor

答案 1 :(得分:0)

默认填充因子为0(100%),本文中的脚本将为您提供所需

https://www.mssqltips.com/sqlservertip/1367/sql-server-script-to-rebuild-all-indexes-for-all-tables-and-all-databases/

答案 2 :(得分:0)

BEGIN TRAN

DECLARE @table_name VARCHAR(MAX)

DECLARE table_cursor CURSOR LOCAL FOR
选择 TABLE_NAME 来自INFORMATION_SCHEMA.TABLES

OPEN table_cursor

FETCH NEXT FROM table_cursor INTO @table_name

    WHILE @@FETCH_STATUS = 0

        BEGIN 

        PRINT 'ALTER INDEX ALL ON '+@table_name+'

REBUILD WITH(FILLFACTOR = 100)'

        END
        CLOSE table_cursor;
    DEALLOCATE table_cursor;

ROLLBACK TRAN