Read more: http://www.blogsmonetize.com/2010/10/how-to-use-syntax-highlighter-3083-in.html#ixzz1DHzvEgBA

Thursday, April 14, 2011

Index size on a table

It is often a requirement to understand the size of indexes on a table.
Here is a script shared by one of my friend at my workspace... looks very useful!!!


DECLARE @OBJECT_NAME VARCHAR(255) = 'trn_defect_tracking_history';
DECLARE @temp TABLE
(
indexID BIGINT,
objectId BIGINT,
index_name NVARCHAR(MAX),
used_page_count BIGINT,
pages BIGINT
)
--Insert into temp table
INSERT INTO @temp
SELECT P.index_id,
P.OBJECT_ID,
I.name,
SUM(used_page_count),
SUM(
CASE
WHEN (p.index_id < 2) THEN (
in_row_data_page_count + lob_used_page_count +
row_overflow_used_page_count
)
ELSE lob_used_page_count + row_overflow_used_page_count
END
)
FROM sys.dm_db_partition_stats P
INNER JOIN sys.indexes I
ON I.index_id = P.index_id
AND I.OBJECT_ID = P.OBJECT_ID
WHERE p.OBJECT_ID = OBJECT_ID(@OBJECT_NAME)
GROUP BY
P.index_id,
I.Name,
P.OBJECT_ID;
SELECT index_name INDEX_NAME,
LTRIM(
STR(
(
CASE
WHEN used_page_count > pages THEN (used_page_count - pages)
ELSE 0
END
) * 8,
15,
0
) + ' KB'
) INDEX_SIZE
FROM @temp T
GO

No comments: