Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table and Index size in SQL Server

Tags:

sql-server

Can we have a SQL query which will basically help in viewing table and index sizes in SQl Server.

How SQL server maintains memory usage for tables/indexes?

like image 443
Kamal Joshi Avatar asked Nov 25 '08 09:11

Kamal Joshi


People also ask

How do I find the index and table size in SQL Server?

sp_spaceused gives you the size of all the indexes combined. The results are usually slightly different but within 1%. The first query includes primary keys, which is a little confusing for a couple reasons.

How will you check the table size and index size?

select segment_name,TABLESPACE_NAME ,segment_type, bytes/1024/1024/1024 size_gb from dba_segments where segment_name = '&segment_name' or segment_name in (select index_name from dba_indexes where table_name='&tablename' and table_owner='&owner'); Share via: Facebook.

How do I find the size of an index in SQL Server?

As we know, sp_spaceused gives the size of table and index but it gives the sum of size of all indexes on a table.

What is the size of table in SQL Server?

The easiest way to find the size of all the tables in a database is to use the SQL Server Management Studio's (SSMS) standard report called Disk Usage by Table. To access the disk usage table: Login to SSMS.


1 Answers

sp_spaceused gives you the size of all the indexes combined.

If you want the size of each index for a table, use one of these two queries:

SELECT     i.name                  AS IndexName,     SUM(s.used_page_count) * 8   AS IndexSizeKB FROM sys.dm_db_partition_stats  AS s  JOIN sys.indexes                AS i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id WHERE s.[object_id] = object_id('dbo.TableName') GROUP BY i.name ORDER BY i.name  SELECT     i.name              AS IndexName,     SUM(page_count * 8) AS IndexSizeKB FROM sys.dm_db_index_physical_stats(     db_id(), object_id('dbo.TableName'), NULL, NULL, 'DETAILED') AS s JOIN sys.indexes AS i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id GROUP BY i.name ORDER BY i.name 

The results are usually slightly different but within 1%.

like image 147
Rob Garrison Avatar answered Sep 25 '22 04:09

Rob Garrison