This commit is contained in:
2024-12-27 15:07:59 +01:00
parent 594d20080d
commit c55d583e42
2 changed files with 79 additions and 65 deletions

View File

@@ -26,11 +26,11 @@ WITH [cteFilteredSize] AS (
GROUP BY [d].[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]
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]
@@ -76,11 +76,11 @@ WITH [cteFilteredSize] AS (
)
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]
[f].[dbName] AS [db],
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] AND [uf].[dbName] = [f].[dbName]
INNER JOIN [cteCurrentSize] [c] ON [c].[Server Name] = [f].[Server Name] AND [c].[dbname] = [f].[dbName]
@@ -88,6 +88,7 @@ FROM [cteFilteredSize] [f]
ORDER BY [f].[Server Name],
[f].[dbName]
;
--per customer
WITH [cteFilteredSize] AS (
SELECT [d].[Server Name],
@@ -128,15 +129,16 @@ SELECT CASE
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]
END AS [customer],
COUNT(DISTINCT [f].[Server Name]) AS [cnt],
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]
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'

View File

@@ -3,24 +3,26 @@ 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
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;
SET [octpdba-931-detail].[toIgnore]=0
;
/*Search for the group that has the smallest number of pharmacies*/
DECLARE @cntLimit INT;
DECLARE @cntLimit INT
;
WITH [cteInstances] AS (
SELECT [g].[Server Name],
CASE
@@ -31,63 +33,73 @@ WITH [cteInstances] AS (
WHEN [g].[Server Name] LIKE 'sun%' THEN 'sun'
WHEN [g].[Server Name] LIKE 'swsun%' THEN 'sun'
WHEN [g].[Server Name] LIKE 'swprd%' THEN 'central'
ELSE 'unknown' END AS [customer]
FROM [dbo].[octpdba-931-global] [g]
GROUP BY [g].[Server Name]
ELSE 'unknown'
END AS [customer]
FROM [dbo].[octpdba-931-global] [g]
GROUP BY [g].[Server Name]
),
[cteCnt] AS (
SELECT [cteInstances].[customer], COUNT(1) AS [cnt]
FROM [cteInstances]
GROUP BY [cteInstances].[customer]
SELECT [cteInstances].[customer],
COUNT(1) AS [cnt]
FROM [cteInstances]
GROUP BY [cteInstances].[customer]
)
SELECT @cntLimit = MIN([cnt])
FROM [cteCnt]
WHERE [cteCnt].[customer] IN ('sun', 'cvi', 'ama')
FROM [cteCnt]
WHERE [cteCnt].[customer] IN ('sun', 'cvi', 'ama')
UPDATE [d]
SET [d].[toIgnore] = 1
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[schemaName] NOT IN ('del')
AND [d].[dbName] LIKE 'arizona%'
AND [d].[TableName] LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%'
/*Every table that is present in less than 90 pharmacies is deemed as to be ignored (phcy count: ama=215, sun=91, cvi=92)*/
SELECT [d].[schemaName],
[d].[TableName],
[d].[dbName],
COUNT(1) AS [cnt]
INTO [#toUpdate]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[serverName] != 'swprddb01.centralinfra.net\APSSQL'
AND [d].[TableName] NOT LIKE 'TT%'
AND [d].[TableName] NOT LIKE 'LD[_]%'
GROUP BY [d].[schemaName],
[d].[TableName],
[d].[dbName]
HAVING COUNT(1) < @cntLimit
ORDER BY [cnt] DESC
INTO [#toUpdate]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[serverName] != 'swprddb01.centralinfra.net\APSSQL'
AND [d].[TableName] NOT LIKE 'TT%'
AND [d].[TableName] NOT LIKE 'LD[_]%'
GROUP BY [d].[schemaName],
[d].[TableName],
[d].[dbName]
HAVING COUNT(1) < @cntLimit
ORDER BY [cnt] DESC
/*All tables in the activePos* databases are considered to be taken in account*/
UPDATE [d]
SET [d].[toIgnore] = 0
FROM [dbo].[octpdba-931-detail] [d]
INNER JOIN [#toUpdate] [t] ON [t].[TableName] = [d].[TableName] AND [t].[schemaName] = [d].[schemaName] AND [t].[dbName] = [d].[dbName]
WHERE [d].[dbName] NOT LIKE 'activepos%'
AND [d].[toIgnore] = 1;
FROM [dbo].[octpdba-931-detail] [d]
INNER JOIN [#toUpdate] [t] ON [t].[TableName] = [d].[TableName] AND [t].[schemaName] = [d].[schemaName] AND [t].[dbName] = [d].[dbName]
WHERE [d].[dbName] NOT LIKE 'activepos%'
AND [d].[toIgnore] = 1
;
/*All tables in the DEL schema are ignored*/
UPDATE [d]
SET [d].[toIgnore] = 1
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[schemaName] = 'del'
AND [d].[toIgnore] = 0;
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[schemaName] = 'del'
AND [d].[toIgnore] = 0
;
SELECT [d].[schemaName],
[d].[TableName],
[d].[dbName],
COUNT(1) AS [cnt]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 01
GROUP BY [d].[schemaName],
[d].[TableName],
[d].[dbName]
FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 01
GROUP BY [d].[schemaName],
[d].[TableName],
[d].[dbName]
--HAVING COUNT(1) < 300
ORDER BY [cnt],
[d].[dbName],
[d].[schemaName],
[d].[TableName] DESC
ORDER BY [cnt],
[d].[dbName],
[d].[schemaName],
[d].[TableName] DESC