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] GROUP BY [d].[server Name]
) )
SELECT [f].[Server Name] AS [instance], SELECT [f].[Server Name] AS [instance],
cast([f].[filteredInstanceMBUsed] AS NUMERIC(25, 2)) AS [Filtered tables size in MB], 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([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([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] CAST([d].[filteredInstanceMBCompressionEstimate] AS NUMERIC(25, 2)) AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f] FROM [cteFilteredSize] [f]
INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name] INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name]
INNER JOIN [cteCurrentSize] [c] ON [c].[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], SELECT [f].[Server Name],
[f].[dbName] AS [db], [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], CAST([f].[filteredInstanceMBUsed] AS NUMERIC(25, 2)) 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], CAST([uf].[unfilteredInstanceMBUsed] AS NUMERIC(25, 2)) 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], CAST([c].[currentMbUsed] AS NUMERIC(25, 2)) 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] CAST([d].[filteredInstanceMBCompressionEstimate] AS NUMERIC(25, 2)) AS [Estimated compressed db size in MB]
FROM [cteFilteredSize] [f] FROM [cteFilteredSize] [f]
INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name] AND [uf].[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 [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], ORDER BY [f].[Server Name],
[f].[dbName] [f].[dbName]
; ;
--per customer --per customer
WITH [cteFilteredSize] AS ( WITH [cteFilteredSize] AS (
SELECT [d].[Server Name], 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 'cvi%' OR [d].[Server Name] LIKE 'swcv%' THEN 'cvi'
WHEN [d].[Server Name] LIKE 'sun%' OR [d].[Server Name] LIKE 'swsu%' THEN 'sun' WHEN [d].[Server Name] LIKE 'sun%' OR [d].[Server Name] LIKE 'swsu%' THEN 'sun'
ELSE 'central' ELSE 'central'
END AS [customer], END AS [customer],
CAST(SUM([f].[filteredInstanceMBUsed]) AS NUMERIC(25, 2)) AS [Filtered tables size in MB], COUNT(DISTINCT [f].[Server Name]) AS [cnt],
CAST(SUM([uf].[unfilteredInstanceMBUsed]) AS NUMERIC(25, 2)) AS [Unfiltered tables size in MB], CAST(SUM([f].[filteredInstanceMBUsed]) AS NUMERIC(25, 2)) AS [Filtered tables size in MB],
CAST(SUM([c].[currentMbUsed]) AS NUMERIC(25, 2)) AS [Current db size in MB], CAST(SUM([uf].[unfilteredInstanceMBUsed]) AS NUMERIC(25, 2)) AS [Unfiltered tables size in MB],
CAST(SUM([d].[filteredInstanceMBCompressionEstimate]) AS NUMERIC(25, 2)) AS [Estimated compressed db 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] FROM [cteFilteredSize] [f]
INNER JOIN [cteUnfilteredSize] [uf] ON [uf].[Server Name] = [f].[Server Name] AND uf.[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 [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 [cteCompressedSize] [d] ON [d].[Server Name] = [f].[Server Name] AND [d].[dbName] = [f].[dbName]
GROUP BY CASE GROUP BY CASE
WHEN [d].[Server Name] LIKE 'ama%' OR [d].[Server Name] LIKE 'swam%' THEN 'ama' 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 '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]; DROP TABLE [#toUpdate];
END END
if not exists( IF NOT EXISTS(
select * SELECT *
from [INFORMATION_SCHEMA].[COLUMNS] c FROM [INFORMATION_SCHEMA].[COLUMNS] [c]
WHERE c.table_name='octpdba-931-detail' WHERE [c].[table_name] = 'octpdba-931-detail'
and c.table_schema='dbo' AND [c].[table_schema] = 'dbo'
and c.column_name='toIgnore' AND [c].[column_name] = 'toIgnore'
) )
BEGIN BEGIN
EXEC('alter table dbo.[octpdba-931-detail] add toIgnore bit CONSTRAINT df_toIgnore DEFAULT 0'); EXEC ('alter table dbo.[octpdba-931-detail] add toIgnore bit CONSTRAINT df_toIgnore DEFAULT 0');
print 'Added filter bit column'; PRINT 'Added filter bit column';
END END
/*Reset flag toIgnore*/ /*Reset flag toIgnore*/
UPDATE [dbo].[octpdba-931-detail] 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*/ /*Search for the group that has the smallest number of pharmacies*/
DECLARE @cntLimit INT; DECLARE @cntLimit INT
;
WITH [cteInstances] AS ( WITH [cteInstances] AS (
SELECT [g].[Server Name], SELECT [g].[Server Name],
CASE CASE
@@ -31,63 +33,73 @@ WITH [cteInstances] AS (
WHEN [g].[Server Name] LIKE 'sun%' THEN 'sun' WHEN [g].[Server Name] LIKE 'sun%' THEN 'sun'
WHEN [g].[Server Name] LIKE 'swsun%' THEN 'sun' WHEN [g].[Server Name] LIKE 'swsun%' THEN 'sun'
WHEN [g].[Server Name] LIKE 'swprd%' THEN 'central' WHEN [g].[Server Name] LIKE 'swprd%' THEN 'central'
ELSE 'unknown' END AS [customer] ELSE 'unknown'
FROM [dbo].[octpdba-931-global] [g] END AS [customer]
GROUP BY [g].[Server Name] FROM [dbo].[octpdba-931-global] [g]
GROUP BY [g].[Server Name]
), ),
[cteCnt] AS ( [cteCnt] AS (
SELECT [cteInstances].[customer], COUNT(1) AS [cnt] SELECT [cteInstances].[customer],
FROM [cteInstances] COUNT(1) AS [cnt]
GROUP BY [cteInstances].[customer] FROM [cteInstances]
GROUP BY [cteInstances].[customer]
) )
SELECT @cntLimit = MIN([cnt]) SELECT @cntLimit = MIN([cnt])
FROM [cteCnt] FROM [cteCnt]
WHERE [cteCnt].[customer] IN ('sun', 'cvi', 'ama') 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)*/ /*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], SELECT [d].[schemaName],
[d].[TableName], [d].[TableName],
[d].[dbName], [d].[dbName],
COUNT(1) AS [cnt] COUNT(1) AS [cnt]
INTO [#toUpdate] INTO [#toUpdate]
FROM [dbo].[octpdba-931-detail] [d] FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[serverName] != 'swprddb01.centralinfra.net\APSSQL' WHERE [d].[serverName] != 'swprddb01.centralinfra.net\APSSQL'
AND [d].[TableName] NOT LIKE 'TT%' AND [d].[TableName] NOT LIKE 'TT%'
AND [d].[TableName] NOT LIKE 'LD[_]%' AND [d].[TableName] NOT LIKE 'LD[_]%'
GROUP BY [d].[schemaName], GROUP BY [d].[schemaName],
[d].[TableName], [d].[TableName],
[d].[dbName] [d].[dbName]
HAVING COUNT(1) < @cntLimit HAVING COUNT(1) < @cntLimit
ORDER BY [cnt] DESC ORDER BY [cnt] DESC
/*All tables in the activePos* databases are considered to be taken in account*/ /*All tables in the activePos* databases are considered to be taken in account*/
UPDATE [d] UPDATE [d]
SET [d].[toIgnore] = 0 SET [d].[toIgnore] = 0
FROM [dbo].[octpdba-931-detail] [d] 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] 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%' WHERE [d].[dbName] NOT LIKE 'activepos%'
AND [d].[toIgnore] = 1; AND [d].[toIgnore] = 1
;
/*All tables in the DEL schema are ignored*/ /*All tables in the DEL schema are ignored*/
UPDATE [d] UPDATE [d]
SET [d].[toIgnore] = 1 SET [d].[toIgnore] = 1
FROM [dbo].[octpdba-931-detail] [d] FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[schemaName] = 'del' WHERE [d].[schemaName] = 'del'
AND [d].[toIgnore] = 0; AND [d].[toIgnore] = 0
;
SELECT [d].[schemaName], SELECT [d].[schemaName],
[d].[TableName], [d].[TableName],
[d].[dbName], [d].[dbName],
COUNT(1) AS [cnt] COUNT(1) AS [cnt]
FROM [dbo].[octpdba-931-detail] [d] FROM [dbo].[octpdba-931-detail] [d]
WHERE [d].[toIgnore] = 01 WHERE [d].[toIgnore] = 01
GROUP BY [d].[schemaName], GROUP BY [d].[schemaName],
[d].[TableName], [d].[TableName],
[d].[dbName] [d].[dbName]
--HAVING COUNT(1) < 300 --HAVING COUNT(1) < 300
ORDER BY [cnt], ORDER BY [cnt],
[d].[dbName], [d].[dbName],
[d].[schemaName], [d].[schemaName],
[d].[TableName] DESC [d].[TableName] DESC