SQL Server 索引维护sql语句
2022-11-12 09:51:24
内容摘要
这篇文章主要为大家详细介绍了SQL Server 索引维护sql语句,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!使用以下脚本查看数据库索引碎
文章正文
这篇文章主要为大家详细介绍了SQL Server 索引维护sql语句,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
使用以下脚本查看数据库索引碎片的大小情况:代码如下:
1 2 | <code>DBCC SHOWCONTIG WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | <code> /*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 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); --更新统计信息 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 </code> |
注:关于SQL Server 索引维护sql语句的内容就先介绍到这里,更多相关文章的可以留意
代码注释