Automatic maintenance of SQL Server Index < 13th >

Source: Internet
Author: User
Tags management studio

In a database with a large number of transactions, tables and indexes are fragmented over time. Therefore, to improve performance, you should periodically check the fragmentation of tables and indexes, and defragment them with a lot of fragmentation.

1. Determine all the tables in the current database that need to be analyzed for fragmentation.

2. Determine the fragmentation of all tables and indexes.

3. Consider the factors to determine the tables and indexes that need to be defragmented.

    • High fragmentation level-avg_fragmentation_in_percent greater than 20%;
    • is not a very small table or index-that is, Page_count is greater than 8;

4, the collation of a large number of fragmented tables and indexes;

Here is a sample SQL stored procedure, which performs the following operations;

    • Traverse all the databases on the system and confirm the indexes on the tables in each database that meet the fragmentation criteria, and save them to a temporary table;
    • Defragment fewer indexes and rebuild many indexes based on fragmentation levels.
CREATE PROCEDURE indexdefragasdeclare @DBName NVARCHAR (255), @TableName NVARCHAR (255), @SchemaName NVARCHAR (255) , @IndexName NVARCHAR (255), @PctFrag decimaldeclare @Defrag NVARCHAR (MAX) IF EXISTS (SELECT * from sys.objects WHERE obje  ct_id = object_id (N ' #Frag ')) DROP table #FragCREATE table #Frag (DBName NVARCHAR (255), TableName NVARCHAR (255), SchemaName NVARCHAR (255), IndexName NVARCHAR (255), avgfragment DECIMAL) EXEC sp_msforeachdb ' INSERT into #Frag (DBName, Tablenam E, SchemaName, IndexName, avgfragment) SELECT '? ' As DBName, t.name as TableName, SC. Name as SchemaName, i.name as IndexName, s.avg_fragmentation_in_percent from?.        Sys.dm_db_index_physical_stats (db_id ("?"), NULL, NULL, NULL, ' Sampled ') as S JOIN?. sys.indexes i on s.object_id = i.object_id and s.index_id = i.index_id JOIN?. Sys.tables T on i.object_id = t.object_id JOIN?. Sys.schemas sc on t.schema_id = sc. Schema_idwhere s.avg_fragmentation_in_percent > 20AND t.type = "U" and S.page_count > 8ORDER by Tablename,indexnam E ' DECLARE cList cursorfor SELECT * from #FragOPEN clistfetch NEXT from Clistinto @DBName, @TableName, @SchemaName, @IndexNam  E, @PctFragWHILE @ @FETCH_STATUS = 0BEGIN IF @PctFrag between 20.0 and 40.0 BEGIN SET @Defrag = N ' ALTER INDEX '                + @IndexName + ' on ' + @DBName + '. ' + @SchemaName + '. ' + @TableName + ' REORGANIZE ' EXEC sp_executesql @Defrag PRINT ' Reorganize index: ' + @DBName + '. ' + @SchemaName + '. ' + @TableName + '. ' + @IndexName END ELS E IF @PctFrag > 40.0 BEGIN SET @Defrag = N ' ALTER INDEX ' + @IndexName + ' on ' + @DBName + '. ' + @SchemaName + '. ' + @TableName + ' REBUILD ' EXEC sp_executesql @Defrag PRINT ' REBUILD index: ' + @DBName + '. ' + @SchemaN Ame + '. ' + @TableName + '. ' + @IndexName END FETCH NEXT from CList to @DBName, @TableName, @SchemaName, @IndexName, @PctFragENDCLOSE clistdeallocate clistdrop TABLE #Frag 

In order to automate the fragmentation analysis process, you can create a SQL Server task from SQL Server Enterprise Manager with the following simple steps.

1, open SQL Server Agent;

  

2, open management Studio, right-click, select new = "task;

  

3. In the General page of the New Task dialog box, enter the task name and other details:

  

4. On the steps page of the New Task dialog box, click New and enter the SQL command for the user database.

  

5. On the Advanced page of the New Task Step dialog box, enter the name of the output file that reports the fragmentation analysis results:

  

6. Click the OK button to return to the new Job dialog box;

7. On the schedule page of the New Task dialog box, click New Schedule and enter the appropriate plan for running the SQL Server task:

  

Schedule this stored procedure to execute at non-peak. To determine the database schema of the database, record the full day of SQL Server:sql statistics\batch requests/sec performance counters, which will show the fluctuations in database load.

8. Click the OK button to return to the New Task dialog box.

9. After you have entered all the information, click the OK button in the New Task dialog box to create the SQL Server task. Create a SQL Server task that schedules the Sp_indexdefrag stored procedure to run at a fixed interval (weekly).

10. Ensure that SQL Server Agent runs so that the SQL Server task will automatically run according to the set schedule.

This SQL task will parse each database and defragment it every Sunday at 1 o'clock in the morning.

Automatic maintenance of SQL Server Index < 13th >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.