Last active
January 30, 2024 16:10
-
-
Save ghotz/773e228d2f79e0a92194 to your computer and use it in GitHub Desktop.
Group index optimize total size from logged data by OLA Maintenance Procedures https://ola.hallengren.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Group index optimize total size from logged data by https://ola.hallengren.com | |
-- | |
WITH cte AS | |
( | |
SELECT | |
DatabaseName | |
, StartTime | |
, ExtendedInfo.value('(/ExtendedInfo/PageCount)[1]', 'bigint') AS pages | |
FROM CommandLog | |
WHERE CommandType = 'ALTER_INDEX' -- AND DatabaseName = 'dbname' | |
) | |
SELECT YEAR(StartTime) AS [Year] | |
, MONTH(StartTime) AS [Month] | |
, DAY(StartTime) AS [Day] | |
, DatabaseName | |
, SUM(pages * 8192 / 1024.0 / 1024.0) AS SizeMB | |
FROM cte | |
GROUP BY | |
YEAR(StartTime), MONTH(StartTime), DAY(StartTime), DatabaseName |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH cte AS | |
( | |
SELECT | |
DatabaseName | |
, StartTime | |
, EndTime | |
, CommandType | |
, ExtendedInfo.value('(/ExtendedInfo/PageCount)[1]', 'bigint') AS pages | |
FROM CommandLog | |
WHERE CommandType IN ('ALTER_INDEX', 'UPDATE_STATISTICS') -- AND DatabaseName = 'dbname' | |
) | |
SELECT YEAR(StartTime) AS [Year] | |
, MONTH(StartTime) AS [Month] | |
, DAY(StartTime) AS [Day] | |
, SUM( | |
CASE WHEN CommandType = 'UPDATE_STATISTICS' THEN 1 ELSE 0 END | |
) AS StatisticsUpdated | |
, SUM( | |
CASE WHEN CommandType = 'ALTER_INDEX' THEN 1 ELSE 0 END | |
) AS IndexesMaintained | |
, SUM(DATEDIFF(minute, StartTime, EndTime)) AS MinElapsed | |
, DatabaseName | |
, CAST(SUM(ISNULL(pages, 0) * 8192 / 1024.0 / 1024.0) AS decimal(15, 2)) AS SizeMB | |
FROM cte | |
GROUP BY | |
YEAR(StartTime), MONTH(StartTime), DAY(StartTime), DatabaseName | |
ORDER BY | |
1, 2, 3, 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH cte AS | |
( | |
SELECT | |
DatabaseName | |
, StartTime | |
, EndTime | |
, CommandType | |
, CASE | |
WHEN CommandType = 'ALTER_INDEX' AND Command LIKE '%REBUILD%' THEN 'REBUILD' | |
WHEN CommandType = 'ALTER_INDEX' AND Command LIKE '%REORG%' THEN 'REORG' | |
END AS AlterIndexType | |
, ExtendedInfo.value('(/ExtendedInfo/PageCount)[1]', 'bigint') AS pages | |
FROM CommandLog | |
WHERE CommandType IN ('ALTER_INDEX', 'UPDATE_STATISTICS') -- AND DatabaseName = 'dbname' | |
) | |
SELECT YEAR(StartTime) AS [Year] | |
, MONTH(StartTime) AS [Month] | |
, DAY(StartTime) AS [Day] | |
, DatabaseName | |
, SUM( | |
CASE WHEN CommandType = 'UPDATE_STATISTICS' THEN 1 ELSE 0 END | |
) AS StatisticsUpdated | |
, SUM( | |
CASE WHEN CommandType = 'ALTER_INDEX' THEN 1 ELSE 0 END | |
) AS IndexesMaintained | |
, SUM(DATEDIFF(minute, StartTime, EndTime)) AS MinElapsed | |
, CAST(SUM( | |
CASE WHEN AlterIndexType = 'REBUILD' | |
THEN ISNULL(pages, 0) * 8192 / 1024.0 / 1024.0 | |
ELSE 0 | |
END | |
) AS decimal(15, 2)) AS RebuildSizeMB | |
, CAST(SUM( | |
CASE WHEN AlterIndexType = 'REORG' | |
THEN ISNULL(pages, 0) * 8192 / 1024.0 / 1024.0 | |
ELSE 0 | |
END | |
) AS decimal(15, 2)) AS ReorgSizeMB | |
FROM cte | |
GROUP BY | |
YEAR(StartTime), MONTH(StartTime), DAY(StartTime), DatabaseName | |
ORDER BY | |
1, 2, 3, 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment