SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


List all indexes with keys, description and size


List all indexes with keys, description and size

Author
Message
Eugene Bereznuk
Eugene Bereznuk
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 156
Comments posted to this topic are about the item List all indexes with keys, description and size
microslave
microslave
Mr or Mrs. 500
Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)

Group: General Forum Members
Points: 502 Visits: 450
While trying to find more about sizes of specific indexes, unlike the sum of used space as returned with sp_spaceused, I changed the queries used by sp_spaceused a bit to return individual index sizes.
At a first glans the sum of the returned sizes seems to fit the sum returned by sp_spaceused itself, so this query could be adapted to filter a specific index name (joined with sys.indexes) etc.
Maybe someone else is trying to find out about specific index sizes too, so you might have a look (still trying to restructure this query a bit, too make it smoother, this is just a first shot Wink ):

SELECT
reserved_page_count,
used_page_count,
CASE
WHEN (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 pages,
CASE
WHEN (index_id < 2) THEN row_count
ELSE 0
END rows,
LTRIM (STR ((CASE WHEN used_page_count >
(CASE
WHEN (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)
THEN (used_page_count -
(CASE
WHEN (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) )
ELSE 0 END) * 8, 15, 0) + ' KB') indexsize
FROM sys.dm_db_partition_stats
WHERE object_id = 546100986 -- filter for object_id of a table here
AND index_id in (1,2,5)
microslave
microslave
Mr or Mrs. 500
Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)

Group: General Forum Members
Points: 502 Visits: 450
After putting together the previous query, I came along this undocumented sp:

sp_MSIndexSpace tablename

which also returns the sizes for each single index. Just in case anyone's looking for that... Smile
Willie-876180
Willie-876180
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 8
After hours of looking around on the net, I found this script, copied it and immediately ran it. Let me just add that I am by now means a DBA or anything similar.

When the results came back, I copied it to Excel and summed it. It came up with a total of 19Gb worth of indexes. I thought that this couldn't be as te database is 24Gb in total. I ran the sp_spaceused against a specific table and compared the two. The results were:

Index Name Size (sp_spaceused) Size (your script)
----------------------------------------------------------------------------
PK_CustID 7872KB 2277

So this leaves me unsure as to what the size actually is. Is it 7.69Mb or 2.22Gb. Could you clarify this for me, please?

Thanks
microslave
microslave
Mr or Mrs. 500
Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)

Group: General Forum Members
Points: 502 Visits: 450
Hello Willie,

you're right, the result from sp_spaceused might differ from the query, since the query itself doesn't add xml indexes or fulltext indexes for the given table, in contrast to sp_spaceused. I didn't need these so I left them away.

Anyway, I modified the query so you can query for a tablename and only one index in this table if desired. If I find some time, I'll add the query for fulltext and xml index space too.

BTW, the original query wasn't put together by me, but it's also a part of the sp_spaceused sys storeproc (sp_helptext 'sp_spaceused'), I just put it together for a quick view while performance tuning a database.

Regards

/*** Show index sizes for each index of a table ***/

SELECT o.name TableName, i.name IndexName, reserved_page_count, used_page_count,
CASE
WHEN (s.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 pages,
row_count,
LTRIM (STR ((CASE WHEN used_page_count >
(CASE
WHEN (s.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)
THEN (used_page_count -
(CASE
WHEN (s.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))
ELSE 0 END) * 8, 15, 0) + ' KB') IndexSize
FROM sys.dm_db_partition_stats s
INNER JOIN sys.objects o ON o.object_id = s.object_id
INNER JOIN sys.indexes i ON i.object_id = o.object_id and s.index_id = i.index_id
WHERE o.name = 'TableName'
--AND i.name = 'IndexName' -- use this to query size of one index in the table
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search