CREATE PROC usp_BenchMark
AS
/*DRIVER PROC TO RUN BENCHMARK PROCESS
=======================================================*/
BEGIN
EXEC usp_StoreNewBenchMarkObjects
EXEC usp_StoreBenchMarkTableDataVolumeHistory
EXEC usp_StoreBenchMarkObjectIndexUsage
EXEC usp_StoreTempDBHistory
EXEC usp_StoreObjectTempDBHistory
EXEC usp_StoreObjectBlockingHistory
EXEC usp_StoreObjectCPUHistory
EXEC usp_StoreObjectIOHistory
END
GO
CREATE PROC usp_StoreNewBenchMarkObjects
AS
/*ADD ANY NEW DATABASE OBJECTS TO BENCHMARK OBJECT TABLES
=======================================================*/
INSERT INTO Benchmark_Object
Select obj.object_id, obj.type, obj.name from sys.objects obj
left outer join Benchmark_Object ben
on obj.object_id = ben.object_Id
where obj.type IN ('U', 'P', 'FN', 'V', 'TR', 'PC', 'AF', 'FS', 'FT', 'TA', 'TR', 'TF', 'X')
and ben.object_id is null
GO
CREATE PROC usp_StoreBenchMarkTableDataVolumeHistory
AS
/*STORE TABLE ROW COUNTS FOR BENCHMARK TABLES
=======================================================*/
DECLARE @MYSQL NVARCHAR(2000)
SET @MYSQL = N'INSERT INTO Benchmark_Table_DataVolumeHistory select Object_Id(''?''), getdate(), count(*) from ?';
EXEC sp_msforeachtable @MYSQL
GO
CREATE PROC usp_StoreBenchMarkObjectIndexUsage
AS
/*STORE INDEX USAGE HISTORY COUNTS FOR BENCHMARK TABLES
=======================================================*/
INSERT into Benchmark_Object_IndexUsageHistory
select dmv.object_id
,sampledatetime=Getdate()
,index_id
, user_seeks
, user_scans
, user_lookups
from sys.dm_db_index_usage_stats dmv
inner join Benchmark_Table bt
on dmv.object_id = bt.object_id
GO
CREATE PROC usp_StoreTempDBHistory
AS
/*STORE TEMPDB USAGE HISTORY AS SERVER LEVEL
=======================================================*/
INSERT INTO Benchmark_TempDBHistory
SELECT getdate(),
SUM(user_object_reserved_page_count) * 8 as user_objects_kb,
SUM(internal_object_reserved_page_count) * 8 as internal_objects_kb,
SUM(version_store_reserved_page_count) * 8 as version_store_kb,
SUM(unallocated_extent_page_count) * 8 as freespace_kb
FROM sys.dm_db_file_Space_Usage
where database_id = 2
GO
CREATE PROC usp_StoreObjectTempDBHistory
AS
/*STORE TEMPDB USAGE HISTORY AT OBJECT LEVEL
=======================================================*/
INSERT INTO Benchmark_Object_TempDBHistory
SELECT
obj.objectid as [Object_Id],
getdate() as SampleDateTime,
sum(tmp.user_objects_alloc_page_count) AS user_objects_alloc_page_count,
sum(tmp.user_objects_dealloc_page_count) AS user_objects_dealloc_page_count,
sum(tmp.internal_objects_alloc_page_count) AS internal_objects_alloc_page_count,
sum(tmp.internal_objects_dealloc_page_count) AS internal_objects_dealloc_page_count
FROM sys.dm_db_task_space_usage AS tmp
LEFT OUTER JOIN sys.dm_exec_requests AS req
ON tmp.session_id = req.session_id
OUTER APPLY sys.dm_exec_sql_text(req.sql_handle) AS obj
WHERE tmp.session_id > 50
AND obj.objectid is not null
Group BY obj.ObjectId
GO
CREATE PROC usp_StoreObjectBlockingHistory
AS
/*STORE OBJECT BLOCKING HISTORY
=======================================================*/
BEGIN
INSERT INTO Benchmark_Table_BlockingHistory
SELECT s.object_id
, SampleDateTime = getdate()
, indexname=i.name, i.index_id
, [block_pct]=cast (100.0 * row_lock_wait_count / (1 + row_lock_count) as numeric(15,2))
, row_lock_wait_ms = row_lock_wait_in_ms
, [avg_row_lock_waits_ms]=cast (1.0 * row_lock_wait_in_ms / (1 + row_lock_wait_count) as numeric(15,2))
from sys.dm_db_index_operational_stats (db_id(), NULL, NULL, NULL) s
,sys.indexes i
where objectproperty(s.object_id,'IsUserTable') = 1
and i.object_id = s.object_id
and i.index_id = s.index_id
And row_lock_wait_count <> 0
END
GO
ALTER PROC usp_StoreObjectCPUHistory
/*STORE CPU USAGE HISTORY
=======================================================*/
AS
BEGIN
INSERT INTO Benchmark_Object_CPUHistory
SELECT
objectid as [Object_Id]
, getdate() As SampleDateTime
, total_cpu_time
, total_execution_count
, number_of_statements
FROM (
SELECT
qs.sql_handle
, sum(qs.total_worker_time) as total_cpu_time
, sum(qs.execution_count) as total_execution_count
, count(*) as number_of_statements
FROM
sys.dm_exec_query_stats qs
GROUP BY qs.sql_handle ) dt
Cross Apply sys.dm_exec_sql_text(dt.sql_handle) eqp
WHERE ObjectId is not null
END
GO
ALTER PROC usp_StoreObjectIOHistory
/*STORE IO USAGE HISTORY
=======================================================*/
AS
BEGIN
INSERT INTO Benchmark_Object_IO_History
Select ObjectId As [Object_ID], GetDate() as SampleDateTime
, (total_logical_reads/Cast(execution_count as Decimal(38,16))) as avg_logical_reads
, (total_logical_writes/Cast(execution_count as Decimal(38,16))) as avg_logical_writes
, (total_physical_reads/Cast(execution_count as Decimal(38,16))) as avg_physical_reads
, Execution_Count
from (
select
Sum(total_logical_reads) as total_logical_reads
, Sum(total_logical_writes) as total_logical_writes
, Sum(total_physical_reads) as total_physical_reads
, Sum(execution_count) as execution_count
, sh.objectid
from sys.dm_exec_query_stats
Cross Apply sys.dm_exec_sql_text(sql_Handle) sh
Where objectId is not null
Group By objectid
) SubQry
END
GO
CREATE PROC usp_CalcBenchmarkTableDataVolumeGrowth(
@START_DATE datetime,
@END_DATE datetime
)
AS
BEGIN
DECLARE @START_DATE datetime,
@END_DATE datetime
SET @START_DATE = getdate()
SET @END_DATE = getdate()
IF @START_DATE = @END_DATE
SET @END_DATE = Convert(varchar(10), DateAdd(day, 1, @END_DATE), 101)
SELECT startdt.object_id, object_name(startdt.object_id) as [Object_Name], startdt.[RowCount] as Starting_Row_Count,
enddt.[RowCount] as Ending_Row_Count,
pct_Growth = case when startdt.[rowcount] = 0 then 0
else (enddt.[rowCount] - startdt.[rowCount])/Convert(Decimal(38, 16), startdt.[rowcount])
end
FROM Benchmark_Table_DataVolumeHistory startdt
INNER JOIN (
Select object_id, min(SampleDateTime) as minSampleDateTime,
max(SampleDateTime) As maxSampleDateTime
FROM Benchmark_Table_DataVolumeHistory
WHERE SampleDateTime >= Convert(varchar(10), Getdate(), 101)
And SampleDateTime < @END_DATE
GROUP BY object_Id
) sub
ON startdt.object_id = sub.object_id
and startdt.SampleDateTime = minSampleDateTime
INNER JOIN BenchMark_Table_DataVolumeHistory enddt
ON enddt.object_id = sub.object_id
and enddt.SampleDateTime = maxSampleDateTime
SELECT UseCaseDesc, startdt.object_id, object_name(startdt.object_id) as [Object_Name], startdt.[RowCount] as Starting_Row_Count,
enddt.[RowCount] as Ending_Row_Count,
pct_Growth = case when startdt.[rowcount] = 0 then 0
else (enddt.[rowCount] - startdt.[rowCount])/Convert(Decimal(38, 16), startdt.[rowcount])
end
FROM Benchmark_Table_DataVolumeHistory startdt
INNER JOIN (
Select uc.UseCaseDesc, hist.object_id, min(SampleDateTime) as minSampleDateTime,
max(SampleDateTime) As maxSampleDateTime
FROM Benchmark_Table_DataVolumeHistory hist
INNER JOIN Benchmark_useCase_Object_assn ua
ON hist.object_id = ua.object_id
inner join benchmark_usecase uc
on ua.usecaseid = uc.usecaseid
WHERE SampleDateTime >= Convert(varchar(10), Getdate(), 101)
And SampleDateTime < @END_DATE
GROUP BY hist.object_Id, usecasedesc
) sub
ON startdt.object_id = sub.object_id
and startdt.SampleDateTime = minSampleDateTime
INNER JOIN BenchMark_Table_DataVolumeHistory enddt
ON enddt.object_id = sub.object_id
and enddt.SampleDateTime = maxSampleDateTime
END