June 22, 2016 at 9:20 am
Hi Folks,
Anyone has a script/documentation to monitor/alert table growth by percentage or number of rows.
Many thanks.
June 23, 2016 at 6:30 am
See if this will help you -
CREATE TABLE [dbo].[TableSizeGrowth](
[id] [int] IDENTITY(1,1) NOT NULL,
[table_schema] [nvarchar](256) NULL,
[table_name] [nvarchar](256) NULL,
[table_rows] [int] NULL,
[reserved_space] [int] NULL,
[data_space] [int] NULL,
[index_space] [int] NULL,
[unused_space] [int] NULL,
[date] [datetime] NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[TableSizeGrowth] ADD CONSTRAINT [DF_TableSizeGrowth_date]
DEFAULT (dateadd(day,(0),datediff(day,(0),getdate()))) FOR [date]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_TableSizeGrowth]
AS
BEGIN
SET NOCOUNT ON
DECLARE
@max-2 INT,
@min-2 INT,
@table_name NVARCHAR(256),
@table_schema NVARCHAR(256),
@sql NVARCHAR(4000)
DECLARE @table TABLE(
id INT IDENTITY(1,1) PRIMARY KEY,
table_name NVARCHAR(256),
table_schema NVARCHAR(256))
IF (SELECT OBJECT_ID('tempdb..#results')) IS NOT NULL
BEGIN
DROP TABLE #results
END
CREATE TABLE #results
(
[table_schema] [nvarchar](256) NULL,
[table_name] [nvarchar](256) NULL,
[table_rows] [int] NULL,
[reserved_space] [nvarchar](55) NULL,
[data_space] [nvarchar](55) NULL,
[index_space] [nvarchar](55) NULL,
[unused_space] [nvarchar](55) NULL
)
INSERT @table(table_schema, table_name)
SELECT
table_schema, table_name
FROM
information_schema.tables
WHERE table_schema + '.' + table_name IN () --insert your table names here
SELECT
@min-2 = 1,
@max-2 = (SELECT MAX(id) FROM @table)
BEGIN
SELECT
@table_name = table_name,
@table_schema = table_schema
FROM
@table
WHERE
id = @min-2
SELECT @sql = 'EXEC sp_spaceused ''[' + @table_schema + '].[' + @table_name + ']'''
INSERT #results(table_name, table_rows, reserved_space, data_space, index_space, unused_space)
EXEC (@sql)
UPDATE #results
SET table_schema = @table_schema
WHERE table_name = @table_name
END
UPDATE #results SET data_space = SUBSTRING(data_space, 1, (LEN(data_space)-3))
UPDATE #results SET reserved_space = SUBSTRING(reserved_space, 1, (LEN(reserved_space)-3))
UPDATE #results SET index_space = SUBSTRING(index_space, 1, (LEN(index_space)-3))
UPDATE #results SET unused_space = SUBSTRING(unused_space, 1, (LEN(unused_space)-3))
INSERT INTO TableSizeGrowth (table_schema, table_name, table_rows, reserved_space, data_space, index_space, unused_space)
SELECT * FROM #results
DROP TABLE #results
END
June 23, 2016 at 3:34 pm
SQL!$@w$0ME (6/22/2016)
Hi Folks,Anyone has a script/documentation to monitor/alert table growth by percentage or number of rows.
Many thanks.
Based on what and when? The condition just before'n'after a given insert or some previous point in time?
--Jeff Moden
Change is inevitable... Change for the better is not.
June 23, 2016 at 10:29 pm
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply