您好,登錄后才能下訂單哦!
這篇文章主要介紹“SQL Server索引維護的sql語句”,在日常操作中,相信很多人在SQL Server索引維護的sql語句問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SQL Server索引維護的sql語句”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
使用以下腳本查看數據庫索引碎片的大小情況:
復制代碼 代碼如下:
DBCC SHOWCONTIG WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS
以下使用腳本來處理維護作業:
復制代碼 代碼如下:
/*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;
到此,關于“SQL Server索引維護的sql語句”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。