2011-11-30 35 views
5

我使用SQL Server 2008 R2,我需要重建的每一个表的索引在数据库中如何更改为每个表的索引在我的数据库

使用这个脚本,我收到一条错误

USE myDb 
GO 

EXEC sp_MSForEachTable 'ALTER INDEX ALL ON ? REBUILD' 

错误:

ALTER INDEX failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.

任何想法如何解决它?谢谢

+0

退房此链接:http://www.mssqltips.com/sqlservertip/1367/sql-server-script-to-rebuild-all-indexes-for-all-tables-and -all-databases /这应该适合你。 – 2011-11-30 13:22:50

+1

谢谢鲨鱼,但我想知道我的命令中有什么问题。我会继续考虑您的资源,谢谢! – GibboK

回答

9

SQL傻瓜(米歇尔Ufford)有一个伟大的脚本来为你做到这一切 - 所有完成和许多用户测试。

这是一个伟大的作品 - 它允许您定义碎片化程度为您

  • 无能为力
  • 重组索引
  • 重建索引

不要重新发明轮子 - just go see and use the script!

+0

感谢Marc的资源 – GibboK

+0

@marc_s你会介意包括实际的答案吗?我无法访问链接的网站,不幸的是,这个答案几乎毫无价值,因为它的立场 – Breeze

+0

@Breeze:对不起,该脚本超过1000行 - 太长,不能在这里发布。您可以尝试使用此直接下载网址获取它:http://sqlfool.com/wp-content/uploads/2011/06/dba_indexDefrag_sp_v41.txt –

1

一个简单的接近我决定用我的的问题。代码:http://blog.sqlauthority.com/2009/01/30/sql-server-2008-2005-rebuild-every-index-of-all-tables-of-database-rebuild-index-with-fillfactor/

-- Rebuild eve Index for every Table in the Database. 
    -- Resource: http://blog.sqlauthority.com/2009/01/30/sql-server-2008-2005-rebuild-every-index-of-all-tables-of-database-rebuild-index-with-fillfactor/ 
    USE [YourDbName] 
    GO 

    -- Show Fragmentation sample on YourTable Index. 
    select avg_fragmentation_in_percent, avg_fragment_size_in_pages, fragment_count, avg_page_space_used_in_percent 
    from sys.dm_db_index_physical_stats (DB_ID(), object_id('[dbo].[YourTableName]'), NULL, NULL, 'DETAILED') 

    -- Cursor going over each table and rebuilding every index of database. 
    DECLARE @TableName VARCHAR(255) 
    DECLARE @sql NVARCHAR(500) 
    DECLARE @fillfactor INT 
    SET @fillfactor = 80 
    DECLARE TableCursor CURSOR FOR 
    SELECT OBJECT_SCHEMA_NAME([object_id])+'.'+name AS TableName 
    FROM sys.tables 
    OPEN TableCursor 
    FETCH NEXT FROM TableCursor INTO @TableName 
    WHILE @@FETCH_STATUS = 0 
    BEGIN 
    SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')' 
    EXEC (@sql) 
    FETCH NEXT FROM TableCursor INTO @TableName 
    END 
    CLOSE TableCursor 
    DEALLOCATE TableCursor 
    GO 
相关问题