You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.7 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[Proc_IndexRebuild] Script Date: 7/4/2019 11:35:48 AM ******/
  4. DROP PROCEDURE [dbo].[Proc_IndexRebuild]
  5. GO
  6. /****** Object: StoredProcedure [dbo].[Proc_IndexRebuild] Script Date: 7/4/2019 11:35:48 AM ******/
  7. SET ANSI_NULLS ON
  8. GO
  9. SET QUOTED_IDENTIFIER ON
  10. GO
  11. CREATE PROC [dbo].[Proc_IndexRebuild]
  12. @dbname varchar(200)=null
  13. AS
  14. BEGIN
  15. -- Ensure a USE <databasename> statement has been executed first.
  16. SET NOCOUNT ON;
  17. DECLARE @objectid int;
  18. DECLARE @indexid int;
  19. DECLARE @partitioncount bigint;
  20. DECLARE @schemaname nvarchar(130);
  21. DECLARE @objectname nvarchar(130);
  22. DECLARE @indexname nvarchar(130);
  23. DECLARE @partitionnum bigint;
  24. DECLARE @partitions bigint;
  25. DECLARE @frag float;
  26. DECLARE @command nvarchar(4000);
  27. -- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
  28. -- and convert object and index IDs to names.
  29. SELECT
  30. object_id AS objectid,
  31. index_id AS indexid,
  32. partition_number AS partitionnum,
  33. avg_fragmentation_in_percent AS frag
  34. INTO #work_to_do
  35. FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED')
  36. WHERE avg_fragmentation_in_percent > 10.0
  37. AND index_id > 0;
  38. --select * from #work_to_do
  39. -- Declare the cursor for the list of partitions to be processed.
  40. DECLARE partitions CURSOR FOR SELECT * FROM #work_to_do;
  41. -- Open the cursor.
  42. OPEN partitions;
  43. -- Loop through the partitions.
  44. WHILE (1=1)
  45. BEGIN;
  46. FETCH NEXT
  47. FROM partitions
  48. INTO @objectid, @indexid, @partitionnum, @frag;
  49. IF @@FETCH_STATUS < 0 BREAK;
  50. SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name)
  51. FROM sys.objects AS o
  52. JOIN sys.schemas as s ON s.schema_id = o.schema_id
  53. WHERE o.object_id = @objectid;
  54. SELECT @indexname = QUOTENAME(name)
  55. FROM sys.indexes
  56. WHERE object_id = @objectid AND index_id = @indexid;
  57. SELECT @partitioncount = count (*)
  58. FROM sys.partitions
  59. WHERE object_id = @objectid AND index_id = @indexid;
  60. -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
  61. IF @frag < 30.0
  62. SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE';
  63. IF @frag >= 30.0
  64. SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REBUILD WITH (MAXDOP=1)';
  65. IF @partitioncount > 1
  66. SET @command = @command + N' PARTITION=' + CAST(@partitionnum AS nvarchar(10));
  67. EXEC (@command);
  68. PRINT N'Executed: ' + @command;
  69. END;
  70. -- Close and deallocate the cursor.
  71. CLOSE partitions;
  72. DEALLOCATE partitions;
  73. -- Drop the temporary table.
  74. DROP TABLE #work_to_do;
  75. END
  76. GO