sql:reidexsusdb
Rozdíly
Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
Obě strany předchozí revizePředchozí verze | |||
sql:reidexsusdb [12.08.2021 12:53] – Ladislav Vojtíšek | sql:reidexsusdb [12.08.2021 13:00] (aktuální) – Ladislav Vojtíšek | ||
---|---|---|---|
Řádek 16: | Řádek 16: | ||
, fragmentation float | , fragmentation float | ||
, numrows int | , numrows int | ||
- | ) | + | |
- | + | ||
- | DECLARE @objectid int; | + | DECLARE @objectid int; |
- | DECLARE @indexid int; | + | DECLARE @indexid int; |
- | DECLARE @schemaname nvarchar(130); | + | DECLARE @schemaname nvarchar(130); |
- | DECLARE @objectname nvarchar(130); | + | DECLARE @objectname nvarchar(130); |
- | DECLARE @indexname nvarchar(130); | + | DECLARE @indexname nvarchar(130); |
- | DECLARE @numrows int | + | DECLARE @numrows int |
- | DECLARE @density float; | + | DECLARE @density float; |
- | DECLARE @fragmentation float; | + | DECLARE @fragmentation float; |
- | DECLARE @command nvarchar(4000); | + | DECLARE @command nvarchar(4000); |
- | DECLARE @fillfactorset bit | + | DECLARE @fillfactorset bit |
- | DECLARE @numpages int | + | DECLARE @numpages int |
- | + | | |
- | -- Select indexes that need to be defragmented based on the following | + | -- Select indexes that need to be defragmented based on the following |
- | -- * Page density is low | + | -- * Page density is low |
- | -- * External fragmentation is high in relation to index size | + | -- * External fragmentation is high in relation to index size |
- | PRINT ' | + | PRINT ' |
- | INSERT @work_to_do | + | INSERT @work_to_do |
- | SELECT | + | SELECT |
- | f.object_id | + | f.object_id |
- | , index_id | + | , index_id |
- | , avg_page_space_used_in_percent | + | , avg_page_space_used_in_percent |
- | , avg_fragmentation_in_percent | + | , avg_fragmentation_in_percent |
- | , record_count | + | , record_count |
- | FROM | + | FROM |
- | sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ' | + | sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ' |
- | WHERE | + | WHERE |
- | (f.avg_page_space_used_in_percent < 85.0 and f.avg_page_space_used_in_percent/ | + | (f.avg_page_space_used_in_percent < 85.0 and f.avg_page_space_used_in_percent/ |
- | or (f.page_count > 50 and f.avg_fragmentation_in_percent > 15.0) | + | or (f.page_count > 50 and f.avg_fragmentation_in_percent > 15.0) |
- | or (f.page_count > 10 and f.avg_fragmentation_in_percent > 80.0) | + | or (f.page_count > 10 and f.avg_fragmentation_in_percent > 80.0) |
- | + | | |
- | PRINT ' | + | PRINT ' |
- | + | | |
- | PRINT ' | + | PRINT ' |
- | + | | |
- | SELECT @numpages = sum(ps.used_page_count) | + | SELECT @numpages = sum(ps.used_page_count) |
- | FROM | + | FROM |
- | @work_to_do AS fi | + | @work_to_do AS fi |
- | INNER JOIN sys.indexes AS i ON fi.objectid = i.object_id and fi.indexid = i.index_id | + | INNER JOIN sys.indexes AS i ON fi.objectid = i.object_id and fi.indexid = i.index_id |
- | INNER JOIN sys.dm_db_partition_stats AS ps on i.object_id = ps.object_id and i.index_id = ps.index_id | + | INNER JOIN sys.dm_db_partition_stats AS ps on i.object_id = ps.object_id and i.index_id = ps.index_id |
- | + | | |
- | -- Declare the cursor for the list of indexes to be processed. | + | -- Declare the cursor for the list of indexes to be processed. |
- | DECLARE curIndexes CURSOR FOR SELECT * FROM @work_to_do | + | DECLARE curIndexes CURSOR FOR SELECT * FROM @work_to_do |
- | + | | |
- | -- Open the cursor. | + | -- Open the cursor. |
- | OPEN curIndexes | + | OPEN curIndexes |
- | + | | |
- | -- Loop through the indexes | + | -- Loop through the indexes |
- | WHILE (1=1) | + | WHILE (1=1) |
- | BEGIN | + | BEGIN |
- | FETCH NEXT FROM curIndexes | + | FETCH NEXT FROM curIndexes |
- | INTO @objectid, @indexid, @density, @fragmentation, | + | INTO @objectid, @indexid, @density, @fragmentation, |
- | IF @@FETCH_STATUS < 0 BREAK; | + | IF @@FETCH_STATUS < 0 BREAK; |
- | + | | |
- | SELECT | + | SELECT |
- | @objectname = QUOTENAME(o.name) | + | @objectname = QUOTENAME(o.name) |
- | , @schemaname = QUOTENAME(s.name) | + | , @schemaname = QUOTENAME(s.name) |
- | FROM | + | FROM |
- | sys.objects AS o | + | sys.objects AS o |
- | INNER JOIN sys.schemas as s ON s.schema_id = o.schema_id | + | INNER JOIN sys.schemas as s ON s.schema_id = o.schema_id |
- | WHERE | + | WHERE |
- | o.object_id = @objectid; | + | o.object_id = @objectid; |
- | + | | |
- | SELECT | + | SELECT |
- | @indexname = QUOTENAME(name) | + | @indexname = QUOTENAME(name) |
- | , @fillfactorset = CASE fill_factor WHEN 0 THEN 0 ELSE 1 END | + | , @fillfactorset = CASE fill_factor WHEN 0 THEN 0 ELSE 1 END |
- | FROM | + | FROM |
- | sys.indexes | + | sys.indexes |
- | WHERE | + | WHERE |
- | object_id = @objectid AND index_id = @indexid; | + | object_id = @objectid AND index_id = @indexid; |
- | + | | |
- | IF ((@density BETWEEN 75.0 AND 85.0) AND @fillfactorset = 1) OR (@fragmentation < 30.0) | + | IF ((@density BETWEEN 75.0 AND 85.0) AND @fillfactorset = 1) OR (@fragmentation < 30.0) |
- | SET @command = N' | + | SET @command = N' |
- | ELSE IF @numrows >= 5000 AND @fillfactorset = 0 | + | ELSE IF @numrows >= 5000 AND @fillfactorset = 0 |
- | SET @command = N' | + | SET @command = N' |
- | ELSE | + | ELSE |
- | SET @command = N' | + | SET @command = N' |
- | PRINT convert(nvarchar, | + | PRINT convert(nvarchar, |
- | EXEC (@command); | + | EXEC (@command); |
- | PRINT convert(nvarchar, | + | PRINT convert(nvarchar, |
- | END | + | END |
- | + | | |
- | -- Close and deallocate the cursor. | + | -- Close and deallocate the cursor. |
- | CLOSE curIndexes; | + | CLOSE curIndexes; |
- | DEALLOCATE curIndexes; | + | DEALLOCATE curIndexes; |
- | + | | |
- | + | | |
- | IF EXISTS (SELECT * FROM @work_to_do) | + | IF EXISTS (SELECT * FROM @work_to_do) |
- | BEGIN | + | BEGIN |
- | PRINT ' | + | PRINT ' |
- | SELECT @numpages = @numpages - sum(ps.used_page_count) | + | SELECT @numpages = @numpages - sum(ps.used_page_count) |
- | FROM | + | FROM |
- | @work_to_do AS fi | + | @work_to_do AS fi |
- | INNER JOIN sys.indexes AS i ON fi.objectid = i.object_id and fi.indexid = i.index_id | + | INNER JOIN sys.indexes AS i ON fi.objectid = i.object_id and fi.indexid = i.index_id |
- | INNER JOIN sys.dm_db_partition_stats AS ps on i.object_id = ps.object_id and i.index_id = ps.index_id | + | INNER JOIN sys.dm_db_partition_stats AS ps on i.object_id = ps.object_id and i.index_id = ps.index_id |
- | + | | |
- | PRINT ' | + | PRINT ' |
- | END | + | END |
- | GO | + | GO |
- | + | | |
- | + | | |
- | --Update all statistics | + | --Update all statistics |
- | PRINT ' | + | PRINT ' |
- | EXEC sp_updatestats | + | EXEC sp_updatestats |
- | PRINT 'Done updating statistics.' | + | PRINT 'Done updating statistics.' |
- | GO | + | GO |
sql/reidexsusdb.1628772797.txt.gz · Poslední úprava: 12.08.2021 12:53 (upraveno mimo DokuWiki)