adapted logic

This commit is contained in:
2024-12-27 13:58:56 +01:00
parent 9052475370
commit 594d20080d
4 changed files with 365 additions and 34 deletions

View File

@@ -0,0 +1,173 @@
/*
24.12.2024, TSC
*/
USE [Arizona]
;
SET XACT_ABORT ON
;
SET NOCOUNT ON
;
--#region temp table
IF OBJECT_ID('tempdb..#temptable') IS NOT NULL
BEGIN
DROP TABLE [#temptable];
END
CREATE TABLE [#temptable]
(
[db_name] VARCHAR(255) CONSTRAINT [df_db_name] DEFAULT DB_NAME(),
[object_name] NVARCHAR(128),
[schema_name] NVARCHAR(128),
[index_id] INT,
[partition_number] INT,
[size_with_current_compression_setting(KB)] BIGINT,
[size_with_requested_compression_setting(KB)] BIGINT,
[sample_size_with_current_compression_setting(KB)] BIGINT,
[sample_size_with_requested_compression_setting(KB)] BIGINT
)
;
--#endregion temp table
--#region create table in hciTools
IF NOT EXISTS (
SELECT *
FROM [HCITools].[sys].[tables] [t]
JOIN [HCITools].[sys].[schemas] [s] ON [s].[schema_id] = [t].[schema_id]
WHERE [s].[name] = 'tmp'
AND [t].[name] = 'compression_estimate'
)
BEGIN
CREATE TABLE [HCITools].[tmp].[compression_estimate]
(
[rowId] BIGINT IDENTITY NOT NULL,
[database_name] VARCHAR(255) NOT NULL,
[schema_name] VARCHAR(50) NOT NULL,
[table_name] VARCHAR(255) NOT NULL,
[compute_date] DATETIME NOT NULL,
[current_size_KB] BIGINT NOT NULL,
[estimated_compressed_size_in_kb] BIGINT NULL,
CONSTRAINT [pk_compression_estimate] PRIMARY KEY ([rowId])
);
--DROP TABLE hciTools.tmp.compression_estimate
--TRUNCATE TABLE hciTools.tmp.compression_estimate
PRINT 'table compression_estimate created';
END;
GO
--#endregion create table in hciTools
/* declare variables */
DECLARE @dbName VARCHAR(255)
;
DECLARE [csrDb] CURSOR FAST_FORWARD READ_ONLY FOR
SELECT [Name]
FROM [sys].[databases]
WHERE [Name] LIKE 'activePos%'
OR [Name] IN ('arizona', 'arizonaCust', 'arizonaLd')
ORDER BY [NAME] DESC
;
OPEN [csrDb]
;
FETCH NEXT FROM [csrDb] INTO @dbName
;
WHILE @@FETCH_STATUS = 0 BEGIN
PRINT '---------------------------------------';
PRINT '---- Switching to ' + @dbName;
PRINT '---------------------------------------';
EXECUTE ('
USE '+@dbName+'
DECLARE @schema_name VARCHAR(100), @table_name VARCHAR(500), @iterCnt INT = 0, @expectedIter INT = 0, @tstamp VARCHAR(20);
SELECT @expectedIter = COUNT(1)
FROM sys.[tables] t;
TRUNCATE TABLE [#temptable];
DECLARE csrCompressionEstimate CURSOR FAST_FORWARD READ_ONLY FOR
SELECT --TOP 10
SCHEMA_NAME(t.[schema_id]) AS [schema_name]
, t.[name] AS [table_name]
FROM sys.tables t
OPEN csrCompressionEstimate
FETCH NEXT FROM csrCompressionEstimate INTO @schema_name, @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO [#temptable]
(
[object_name],
[schema_name],
[index_id],
[partition_number],
[size_with_current_compression_setting(KB)],
[size_with_requested_compression_setting(KB)],
[sample_size_with_current_compression_setting(KB)],
[sample_size_with_requested_compression_setting(KB)]
)
EXEC sys.sp_estimate_data_compression_savings @schema_name, @table_name, NULL, NULL, ''PAGE'';
SET @iterCnt = @iterCnt + 1;
IF @iterCnt%3 = 0
BEGIN
SET @tstamp = CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)
RAISERROR(''%s - Iterration %i / %i'', 0, 0, @tstamp, @iterCnt, @expectedIter) WITH NOWAIT;
END
FETCH NEXT FROM csrCompressionEstimate INTO @schema_name, @table_name
END
CLOSE csrCompressionEstimate
DEALLOCATE csrCompressionEstimate
MERGE [HCITools].[tmp].[compression_estimate] AS trg
USING (
SELECT
[db_name]
,[schema_name]
,[object_name]
, SUM([size_with_current_compression_setting(KB)]) AS [size_with_current_compression_setting(KB)]
, SUM([size_with_requested_compression_setting(KB)]) AS [size_with_requested_compression_setting(KB)]
FROM #temptable
GROUP BY [object_name], [schema_name], [db_name]
) AS src ON src.[schema_name] = trg.[schema_name] AND src.[object_name] = trg.[table_name] AND DB_NAME() = trg.[database_name]
WHEN MATCHED
THEN UPDATE
SET [trg].[compute_date] = CURRENT_TIMESTAMP
, [trg].[current_size_KB] = src.[size_with_current_compression_setting(KB)]
, [trg].[estimated_compressed_size_in_kb] = src.[size_with_requested_compression_setting(KB)]
WHEN NOT MATCHED
THEN INSERT([database_name], [schema_name], [table_name], [compute_date], [current_size_KB], [estimated_compressed_size_in_kb])
VALUES(src.[db_name], src.[schema_name], src.[object_name], CURRENT_TIMESTAMP, src.[size_with_current_compression_setting(KB)], src.[size_with_requested_compression_setting(KB)])
;
');
FETCH NEXT FROM [csrDb] INTO @dbName;
END;
CLOSE [csrDb]
;
DEALLOCATE [csrDb]
;
SELECT [e].[database_name],
[e].[schema_name],
[e].[table_name],
REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, [e].[current_size_KB]), 1), ',', ''''), '.00', '') AS [current_size_KB],
REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, [e].[estimated_compressed_size_in_kb]), 1), ',', ''''), '.00', '') AS [current_size_KB],
CASE
WHEN [current_size_KB] = 0 THEN 0
ELSE CAST((([current_size_KB] - [estimated_compressed_size_in_kb]) * 100.0 / [current_size_KB]) AS NUMERIC(25, 2))
END AS [percentage_difference]
FROM [HCITools].[tmp].[compression_estimate] [e]
WHERE [e].[current_size_KB] > 0
AND [e].[estimated_compressed_size_in_kb] > 0
ORDER BY [percentage_difference] DESC
;

View File

@@ -1,24 +1,146 @@
with cteFilteredSize as (
SELECT d.[Server Name]
,sum(d.[UsedSpaceMB]) as filteredInstanceMBUsed
--per instance
WITH [cteFilteredSize] AS (
SELECT [d].[Server Name],
SUM([d].[UsedSpaceMB]) AS [filteredInstanceMBUsed]
FROM [dbo].[octpdba-931-detail] [d]
WHERE d.[toIgnore]=0
GROUP BY d.[server Name]
)
,cteUnfilteredSize as (
SELECT d.[Server Name]
,sum(d.[UsedSpaceMB]) as unfilteredInstanceMBUsed
WHERE [d].[toIgnore] = 0
GROUP BY [d].[server Name]
),
[cteUnfilteredSize] AS (
SELECT [d].[Server Name],
SUM([d].[UsedSpaceMB]) AS [unfilteredInstanceMBUsed]
FROM [dbo].[octpdba-931-detail] [d]
GROUP BY d.[server Name]
)
,cteCurrentSize as (
select g.[Server Name], sum(g.[SpaceUsedMB]) currentMbUsed
from dbo.[octpdba-931-global] g
GROUP BY g.[Server Name]
GROUP BY [d].[server Name]
),
[cteCurrentSize] AS (
SELECT [g].[Server Name],
SUM([g].[SpaceUsedMB]) [currentMbUsed]
FROM [dbo].[octpdba-931-global] [g]
GROUP BY [g].[Server Name]
),
[cteCompressedSize] AS (
SELECT [d].[Server Name],
SUM([d].[compressEstimateSpaceMB]) AS [filteredInstanceMBCompressionEstimate]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 0
GROUP BY [d].[server Name]
)
SELECT f.[Server Name], f.[filteredInstanceMBUsed], uf.[unfilteredInstanceMBUsed], c.[currentMbUsed]
FROM [cteFilteredSize] f
INNER JOIN [cteUnfilteredSize] uf on uf.[Server Name] = f.[Server Name]
INNER JOIN [cteCurrentSize] c on [c].[Server Name] = f.[Server Name]
ORDER BY f.[Server Name]
SELECT [f].[Server Name] AS [instance],
cast([f].[filteredInstanceMBUsed] AS NUMERIC(25, 2)) AS [Filtered tables size in MB],
cast([uf].[unfilteredInstanceMBUsed] AS NUMERIC(25, 2)) AS [Unfiltered tables size in MB],
CAST([c].[currentMbUsed] AS NUMERIC(25, 2)) AS [Current db size in MB],
CAST([d].[filteredInstanceMBCompressionEstimate] AS NUMERIC(25, 2)) AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f]
INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name]
INNER JOIN [cteCurrentSize] [c] ON [c].[Server Name] = [f].[Server Name]
INNER JOIN [cteCompressedSize] [d] ON [d].[Server Name] = [f].[Server Name]
ORDER BY [f].[Server Name]
;
--per instance and db
WITH [cteFilteredSize] AS (
SELECT [d].[Server Name],
[d].[dbName],
SUM([d].[UsedSpaceMB]) AS [filteredInstanceMBUsed]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 0
GROUP BY [d].[server Name],
[d].[dbName]
),
[cteUnfilteredSize] AS (
SELECT [d].[Server Name],
[d].[dbName],
SUM([d].[UsedSpaceMB]) AS [unfilteredInstanceMBUsed]
FROM [dbo].[octpdba-931-detail] [d]
GROUP BY [d].[server Name],
[d].[dbName]
),
[cteCurrentSize] AS (
SELECT [g].[Server Name],
[g].[dbname],
SUM([g].[SpaceUsedMB]) [currentMbUsed]
FROM [dbo].[octpdba-931-global] [g]
GROUP BY [g].[Server Name],
[g].[dbname]
),
[cteCompressedSize] AS (
SELECT [d].[Server Name],
[d].[dbName],
SUM([d].[compressEstimateSpaceMB]) AS [filteredInstanceMBCompressionEstimate]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 0
GROUP BY [d].[server Name],
[d].[dbName]
)
SELECT [f].[Server Name],
[f].[dbName] AS [db],
REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([f].[filteredInstanceMBUsed] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '') AS [Filtered tables size in MB],
REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([uf].[unfilteredInstanceMBUsed] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '') AS [Unfiltered tables size in MB],
REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([c].[currentMbUsed] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '') AS [Current db size in MB],
REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, CAST([d].[filteredInstanceMBCompressionEstimate] AS NUMERIC(25, 2))), 1), ',', ''''), '.00', '') AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f]
INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name] AND [uf].[dbName] = [f].[dbName]
INNER JOIN [cteCurrentSize] [c] ON [c].[Server Name] = [f].[Server Name] AND [c].[dbname] = [f].[dbName]
INNER JOIN [cteCompressedSize] [d] ON [d].[Server Name] = [f].[Server Name] AND [d].[dbName] = [f].[dbName]
ORDER BY [f].[Server Name],
[f].[dbName]
;
--per customer
WITH [cteFilteredSize] AS (
SELECT [d].[Server Name],
[d].[dbName],
SUM([d].[UsedSpaceMB]) AS [filteredInstanceMBUsed]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 0
GROUP BY [d].[server Name],
[d].[dbName]
),
[cteUnfilteredSize] AS (
SELECT [d].[Server Name],
[d].[dbName],
SUM([d].[UsedSpaceMB]) AS [unfilteredInstanceMBUsed]
FROM [dbo].[octpdba-931-detail] [d]
GROUP BY [d].[server Name],
[d].[dbName]
),
[cteCurrentSize] AS (
SELECT [g].[Server Name],
[g].[dbname],
SUM([g].[SpaceUsedMB]) [currentMbUsed]
FROM [dbo].[octpdba-931-global] [g]
GROUP BY [g].[Server Name],
[g].[dbname]
),
[cteCompressedSize] AS (
SELECT [d].[Server Name],
[d].[dbName],
SUM([d].[compressEstimateSpaceMB]) AS [filteredInstanceMBCompressionEstimate]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 0
GROUP BY [d].[server Name],
[d].[dbName]
)
SELECT CASE
WHEN [d].[Server Name] LIKE 'ama%' OR [d].[Server Name] LIKE 'swam%' THEN 'ama'
WHEN [d].[Server Name] LIKE 'cvi%' OR [d].[Server Name] LIKE 'swcv%' THEN 'cvi'
WHEN [d].[Server Name] LIKE 'sun%' OR [d].[Server Name] LIKE 'swsu%' THEN 'sun'
ELSE 'central'
END AS [customer],
CAST(SUM([f].[filteredInstanceMBUsed]) AS NUMERIC(25, 2)) AS [Filtered tables size in MB],
CAST(SUM([uf].[unfilteredInstanceMBUsed]) AS NUMERIC(25, 2)) AS [Unfiltered tables size in MB],
CAST(SUM([c].[currentMbUsed]) AS NUMERIC(25, 2)) AS [Current db size in MB],
CAST(SUM([d].[filteredInstanceMBCompressionEstimate]) AS NUMERIC(25, 2)) AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f]
INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name] AND uf.[dbName] = f.[dbName]
INNER JOIN [cteCurrentSize] [c] ON [c].[Server Name] = [f].[Server Name] AND c.[dbName] = f.[dbName]
INNER JOIN [cteCompressedSize] [d] ON [d].[Server Name] = [f].[Server Name] AND d.[dbName] = f.[dbName]
GROUP BY CASE
WHEN [d].[Server Name] LIKE 'ama%' OR [d].[Server Name] LIKE 'swam%' THEN 'ama'
WHEN [d].[Server Name] LIKE 'cvi%' OR [d].[Server Name] LIKE 'swcv%' THEN 'cvi'
WHEN [d].[Server Name] LIKE 'sun%' OR [d].[Server Name] LIKE 'swsu%' THEN 'sun'
ELSE 'central'
END
;

View File

@@ -3,6 +3,18 @@ IF OBJECT_ID('tempdb..#toUpdate') IS NOT NULL
DROP TABLE [#toUpdate];
END
if not exists(
select *
from [INFORMATION_SCHEMA].[COLUMNS] c
WHERE c.table_name='octpdba-931-detail'
and c.table_schema='dbo'
and c.column_name='toIgnore'
)
BEGIN
EXEC('alter table dbo.[octpdba-931-detail] add toIgnore bit CONSTRAINT df_toIgnore DEFAULT 0');
print 'Added filter bit column';
END
/*Reset flag toIgnore*/
UPDATE [dbo].[octpdba-931-detail]
SET [octpdba-931-detail].[toIgnore]=0;

View File

@@ -40,8 +40,24 @@ IF OBJECT_ID('tempdb..#dbs')IS NOT NULL BEGIN
DROP TABLE #dbs;
END
CREATE TABLE #tblSize ( [serverName] nvarchar(128), [dbName] nvarchar(128), [schemaName] nvarchar(128), [TableName] nvarchar(128), [RowCounts] bigint, [TotalSpaceMB] decimal(26,6), [UsedSpaceMB] decimal(26,6), [DataSpaceMB] decimal(26,6) )
CREATE TABLE #dbs ( [database_name] NVARCHAR(128) NOT NULL, [log_size_mb] DECIMAL(18,2) NOT NULL, [row_size_mb] DECIMAL(18,2) NOT NULL, [total_size_mb] DECIMAL(18,2) NOT NULL );
CREATE TABLE #tblSize (
[serverName] NVARCHAR(128)
, [dbName] NVARCHAR(128)
, [schemaName] NVARCHAR(128)
, [TableName] NVARCHAR(128)
, [RowCounts] BIGINT
, [TotalSpaceMB] NUMERIC(26,6)
, [UsedSpaceMB] NUMERIC(26,6)
, [DataSpaceMB] NUMERIC(26,6)
, compressCurrentSpaceMB NUMERIC(26,6)
, compressEstimateSpaceMB NUMERIC(26,6)
)
CREATE TABLE #dbs (
[database_name] NVARCHAR(128) NOT NULL
, [log_size_mb] NUMERIC(18,2) NOT NULL
, [row_size_mb] NUMERIC(18,2) NOT NULL
, [total_size_mb] NUMERIC(18,2) NOT NULL
);
INSERT INTO [#dbs] ([database_name], [log_size_mb], [row_size_mb], [total_size_mb])
SELECT
@@ -111,7 +127,9 @@ INSERT INTO #tblSize(
[RowCounts] ,
[TotalSpaceMB] ,
[UsedSpaceMB] ,
[DataSpaceMB]
[DataSpaceMB],
[compressCurrentSpaceMB],
[compressEstimateSpaceMB]
)
SELECT @@SERVERNAME as serverName,
DB_NAME() as dbName,
@@ -120,7 +138,9 @@ SELECT @@SERVERNAME as serverName,
MAX([p].[rows]) AS RowCounts,
(SUM([a].[total_pages]) * 8) / 1024.0 AS TotalSpaceMB,
(SUM([a].[used_pages]) * 8) / 1024.0 AS UsedSpaceMB,
(SUM([a].[data_pages]) * 8) / 1024.0 AS DataSpaceMB
(SUM([a].[data_pages]) * 8) / 1024.0 AS DataSpaceMB,
MAX(ce.[current_size_KB])/1024 AS compressCurrentSpaceMB, --MAX() because you can have more than 1 index on the table
MAX(ce.[estimated_compressed_size_in_kb])/1024 AS compressEstimateSpaceMB
FROM sys.tables t
INNER JOIN sys.indexes i
ON t.object_id = i.object_id
@@ -129,6 +149,10 @@ SELECT @@SERVERNAME as serverName,
AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a
ON [p].[partition_id] = [a].[container_id]
LEFT JOIN [HCITools].[tmp].[compression_estimate] ce
ON ce.[database_name] = db_name()
AND ce.[schema_name] = SCHEMA_NAME(t.schema_id)
AND ce.[table_name] = t.[name]
WHERE i.object_id > 255
--AND i.index_id IN ( 0, 1 )
GROUP BY t.name, t.schema_id