|
@ -573,8 +573,102 @@ END CATCH; |
|
|
|
|
|
|
|
|
DROP TABLE #temp_hbpo |
|
|
DROP TABLE #temp_hbpo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 所有碎片超过百分比表,索引重建 |
|
|
|
|
|
|
|
|
|
|
|
SET ANSI_NULLS ON |
|
|
GO |
|
|
GO |
|
|
|
|
|
|
|
|
|
|
|
SET QUOTED_IDENTIFIER ON |
|
|
|
|
|
GO |
|
|
|
|
|
|
|
|
|
|
|
CREATE proc [dbo].[p_reBuildIndex] |
|
|
|
|
|
|
|
|
|
|
|
as |
|
|
|
|
|
|
|
|
|
|
|
declare @statement NVARCHAR(1000) |
|
|
|
|
|
|
|
|
|
|
|
declare mycursor cursor for |
|
|
|
|
|
|
|
|
|
|
|
SELECT 'ALTER INDEX [' + ix.name + '] ON [' + s.name + '].[' + t.name + '] REBUILD WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, SORT_IN_TEMPDB = OFF, ONLINE = OFF )' |
|
|
|
|
|
|
|
|
|
|
|
as sqlStr |
|
|
|
|
|
|
|
|
|
|
|
FROM sys.indexes AS ix |
|
|
|
|
|
|
|
|
|
|
|
INNER JOIN sys.tables t |
|
|
|
|
|
|
|
|
|
|
|
ON t.object_id = ix.object_id |
|
|
|
|
|
|
|
|
|
|
|
INNER JOIN sys.schemas s |
|
|
|
|
|
|
|
|
|
|
|
ON t.schema_id = s.schema_id |
|
|
|
|
|
|
|
|
|
|
|
INNER JOIN |
|
|
|
|
|
|
|
|
|
|
|
(SELECT object_id , |
|
|
|
|
|
|
|
|
|
|
|
index_id , |
|
|
|
|
|
|
|
|
|
|
|
avg_fragmentation_in_percent, |
|
|
|
|
|
|
|
|
|
|
|
partition_number |
|
|
|
|
|
|
|
|
|
|
|
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) |
|
|
|
|
|
|
|
|
|
|
|
) ps |
|
|
|
|
|
|
|
|
|
|
|
ON t.object_id = ps.object_id |
|
|
|
|
|
|
|
|
|
|
|
AND ix.index_id = ps.index_id |
|
|
|
|
|
|
|
|
|
|
|
INNER JOIN |
|
|
|
|
|
|
|
|
|
|
|
(SELECT object_id, |
|
|
|
|
|
|
|
|
|
|
|
index_id , |
|
|
|
|
|
|
|
|
|
|
|
COUNT(DISTINCT partition_number) AS partition_count |
|
|
|
|
|
|
|
|
|
|
|
FROM sys.partitions |
|
|
|
|
|
|
|
|
|
|
|
GROUP BY object_id, |
|
|
|
|
|
|
|
|
|
|
|
index_id |
|
|
|
|
|
|
|
|
|
|
|
) pc |
|
|
|
|
|
|
|
|
|
|
|
ON t.object_id = pc.object_id |
|
|
|
|
|
|
|
|
|
|
|
AND ix.index_id = pc.index_id |
|
|
|
|
|
|
|
|
|
|
|
WHERE ps.avg_fragmentation_in_percent > 0 |
|
|
|
|
|
|
|
|
|
|
|
AND ix.name IS NOT NULL |
|
|
|
|
|
|
|
|
|
|
|
open mycursor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fetch next from mycursor into @statement |
|
|
|
|
|
|
|
|
|
|
|
while(@@fetch_status=0) --如果数据集里一直有数据 |
|
|
|
|
|
|
|
|
|
|
|
begin |
|
|
|
|
|
|
|
|
|
|
|
EXEC sp_executesql @statement |
|
|
|
|
|
|
|
|
|
|
|
--select @statement |
|
|
|
|
|
|
|
|
|
|
|
fetch next from mycursor into @statement |
|
|
|
|
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
close mycursor |
|
|
|
|
|
|
|
|
|
|
|
deallocate mycursor |
|
|
|
|
|
|
|
|
|
|
|
GO |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|