In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "the sql statement of SQL Server index maintenance". In the daily operation, I believe that many people have doubts about the sql statement of SQL Server index maintenance. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "sql statement of SQL Server index maintenance". Next, please follow the editor to study!
Use the following script to view the size of database index fragmentation:
The copy code is as follows:
DBCC SHOWCONTIG WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS
The following uses scripts to handle maintenance jobs:
The copy code is as follows:
/ * Perform a 'USE' to select the database in which to run the script.*/-- Declare variables SET NOCOUNT ON; DECLARE @ tablename varchar; DECLARE @ execstr varchar; DECLARE @ objectid int; Declare @ IndexName varchar; DECLARE @ indexid int; DECLARE @ frag decimal; DECLARE @ maxfrag decimal; DECLARE @ TmpName varchar;-- 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, ObjectId int, IndexName char, 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) Update statistics 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; at this point, the study of "sql statements for SQL Server index maintenance" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.