SQL Server Index maintenance SQL statement _mssql

Source: Internet
Author: User
Tags rtrim
Use the following script to view the size of the database index fragmentation:
Copy Code code as follows:

DBCC showcontig with FAST, Tableresults, All_indexes, no_infomsgs

The following scripts are used to process maintenance jobs:
Copy Code code as follows:

/*perform a ' use <database name> ' to select the database in which to run the script.*/
--Declare variables
SET NOCOUNT on;
DECLARE @tablename varchar (255);
DECLARE @execstr varchar (400);
DECLARE @objectid int;
Declare @IndexName varchar (500);
DECLARE @indexid int;
DECLARE @frag Decimal;
DECLARE @maxfrag Decimal;
DECLARE @TmpName varchar (500);

--Declare @TmpName = '
Set @TmpName = '

--Decide on the maximum fragmentation to allow for.
SELECT @maxfrag = 30.0;

--Declare a cursor.
DECLARE Tables CURSOR for
SELECT Table_schema + '. ' + table_name
From INFORMATION_SCHEMA. TABLES
WHERE table_type = ' BASE TABLE ';

--Create the table.
CREATE TABLE #fraglist (
ObjectName char (255),
ObjectId int,
IndexName char (255),
IndexID int,
LVL int,
Countpages int,
countrows int,
minrecsize int,
maxrecsize int,
avgrecsize int,
Forreccount int,
extents int,
extentswitches int,
avgfreebytes int,
avgpagedensity int,
Scandensity Decimal,
Bestcount int,
Actualcount int,
Logicalfrag Decimal,
Extentfrag decimal);

--Open the cursor.
OPEN tables;

--Loop through all of the tables in the database.
FETCH NEXT
From tables
into @tablename;

While @ @FETCH_STATUS = 0
BEGIN;
--Do the showcontig of all indexes of the table
INSERT into #fraglist
EXEC (' DBCC showcontig (' + @tablename + ')
With FAST, Tableresults, all_indexes, No_infomsgs ');
FETCH NEXT
From tables
into @tablename;
End;

--Close and deallocate the cursor.
Close tables;
DEALLOCATE tables;

--Declare the cursor for the "list of indexes to be defragged."
DECLARE Indexes CURSOR for
SELECT objectname, Objectid,indexname,indexid, Logicalfrag
From #fraglist
WHERE indexproperty (ObjectId, IndexName, ' indexdepth ') > 0;

--Open the cursor.
OPEN indexes;

--Loop through the indexes.
FETCH NEXT
From indexes
Into @tablename, @objectid, @IndexName, @indexid, @frag;


While @ @FETCH_STATUS = 0
BEGIN;
If @frag < @maxfrag
Begin
SELECT @execstr = ' ALTER INDEX [' + RTRIM (@IndexName) + '] on [' + RTRIM (@tablename) + '] REORGANIZE with (lob_compaction = ON) '
Print @maxfrag + ' + @execstr
End
Else
Begin
SELECT @execstr = ' ALTER INDEX [' + RTRIM (@IndexName) + '] on [' + RTRIM (@tablename) + '] REBUILD with (Pad_index = off, Statistics_norecompute = off, Allow_row_locks = on, Allow_page_locks = on, sort_in_tempdb = off, ONLINE = off) '
Print @maxfrag + ' + @execstr
End

EXEC (@execstr);

--Update statistical information
IF @TmpName <> @tablename
BEGIN
SET @tmpName = @tableName
PRINT ' UPDATE STATISTICS ' + @TableName + ' with Fullscan '
EXEC (' UPDATE STATISTICS ' + @TableName + ' with Fullscan ')
End


FETCH NEXT
From indexes
Into @tablename, @objectid, @IndexName, @indexid, @frag;
End;

--Close and deallocate the cursor.
Close indexes;
deallocate indexes;

--Delete the temporary table.
DROP TABLE #fraglist;
Go
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.