Skip to content

Commit a2563fb

Browse files
authored
Create spReport_INDEX_Analyse_Cost_Benefit.sql
1 parent b273801 commit a2563fb

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
SET ANSI_NULLS ON
3+
GO
4+
SET QUOTED_IDENTIFIER ON
5+
GO
6+
7+
CREATE OR ALTER PROCEDURE [dbo].[spReport_INDEX_Analyse_Cost_Benefit]
8+
@argDBName VARCHAR(128), @argTable_Name VARCHAR(128) = NULL
9+
AS
10+
BEGIN
11+
12+
DECLARE @lcMsg VARCHAR(100)
13+
14+
IF @argDBName IS NULL BEGIN
15+
SET @lcMsg = 'Database Name should not be empty'
16+
RAISERROR(@lcMsg, 16, 1)
17+
RETURN
18+
END
19+
20+
DECLARE @lcSQL NVARCHAR(MAX), @lcTable_name_cond VARCHAR(500)
21+
22+
SELECT @lcTable_name_cond = ''
23+
IF @argTable_Name IS NOT NULL
24+
SELECT @lcTable_name_cond = ' AND object_name(s.object_id) LIKE ''' + @argTable_Name + ''''
25+
26+
SELECT @lcSQL = 'USE ' + @argDBName + '; ' +
27+
'SELECT object_name(s.object_id) AS [Table_Name],
28+
i.name AS [Index_Name],
29+
(S.user_seeks + S.user_scans + S.user_lookups) - (S.user_updates + 1) AS [Benefit-Cost],
30+
CONVERT(DECIMAL(5, 2), ((S.user_seeks + S.user_scans + S.user_lookups) / (S.user_seeks + S.user_scans + S.user_lookups + S.user_updates + 1.0)) * 100) AS [Benefit-Cost(%)],
31+
S.user_seeks + S.user_scans + S.user_lookups AS [user reads],
32+
S.user_updates AS [user writes],
33+
S.user_seeks, S.user_scans, S.user_lookups,
34+
S.system_seeks + S.system_scans + S.system_lookups AS [system reads] ,
35+
S.system_updates AS [system writes],
36+
P.row_count, P.used_page_count AS Total_Used_Page_Count, CONVERT(DECIMAL(15,2), P.used_page_count/128.00) AS Total_used_MB
37+
FROM sys.dm_db_index_usage_stats s WITH (NOLOCK)
38+
JOIN sys.indexes i WITH (NOLOCK) on i.object_id = s.object_id and i.index_id = s.index_id
39+
JOIN sys.dm_db_partition_stats P WITH (NOLOCK) ON P.object_id = S.object_id and P.index_id = S.index_id
40+
WHERE objectproperty(s.object_id, ''IsUserTable'') = 1
41+
AND S.database_id = ' + CONVERT(VARCHAR(10), DB_ID(@argDBName) ) +
42+
@lcTable_name_cond +
43+
' ORDER BY [Benefit-Cost] DESC'
44+
45+
46+
EXEC (@lcSQL)
47+
--PRINT (@lcSQL)
48+
49+
END
50+
51+

0 commit comments

Comments
 (0)