Created
April 15, 2013 20:09
-
-
Save justinlewis/5390930 to your computer and use it in GitHub Desktop.
A query to list all the tables in a SQL Server database with their size in KB and the size of their Indexes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Measures tables size (in kilobytes) | |
declare @t table ( | |
name nvarchar(100), [rows] int, [reserved] nvarchar(100), [data] nvarchar(100), [index_size] nvarchar(100), [unused] nvarchar(100) | |
) | |
declare @name nvarchar(100) | |
declare tt cursor for | |
Select name from sys.tables | |
open tt | |
fetch next from tt into @name | |
while @@FETCH_STATUS = 0 | |
begin | |
insert into @t | |
exec sp_spaceused @name | |
fetch next from tt into @name | |
end | |
close tt | |
deallocate tt | |
select name as table_name, [rows] as rows_count, data + [index] as total_size, data as data_size, [index] as index_size | |
from (select name, | |
[rows], | |
cast (LEFT(data, LEN(data)-3) as int) data, | |
cast (LEFT(index_size, LEN(index_size)-3) as int) [index] | |
from @t | |
) x | |
order by 3 desc, 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment