How to find the size of a table in SQL?
-
14It depends on the type of DBMS. Which one are you interested in ? – codaddict Aug 31 '10 at 05:56
10 Answers
SQL Server:-
sp_spaceused 'TableName'
Or in management studio: Right Click on table -> Properties -> Storage
MySQL:-
SELECT table_schema, table_name, data_length, index_length FROM information_schema.tables
Sybase:-
sp_spaceused 'TableName'
-
1Note, for MySQL `data_length` is the size of the table in bytes, not the number of rows. See: https://dev.mysql.com/doc/refman/8.0/en/tables-table.html – Ceasar Bautista Jul 26 '19 at 22:04
Combining the answers from ratty's and Haim's posts (including comments) I've come up with this, which for SQL Server seems to be the most elegant so far:
-- DROP TABLE #tmpTableSizes
CREATE TABLE #tmpTableSizes
(
tableName varchar(100),
numberofRows varchar(100),
reservedSize varchar(50),
dataSize varchar(50),
indexSize varchar(50),
unusedSize varchar(50)
)
insert #tmpTableSizes
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"
select * from #tmpTableSizes
order by cast(LEFT(reservedSize, LEN(reservedSize) - 4) as int) desc
This gives you a list of all your tables in order of reserved size, ordered from largest to smallest.
- 6,770
- 6
- 40
- 63
-
I was going to post this very script - let's hope he's using SQL Server. – SqlRyan Sep 15 '10 at 17:18
-
2Excellent script - except, it should read LEN(...) - 3, not minus 4. The values come back like this: "3746520 KB" so it's just the last 3 characters which need chopping off. But... cool to see how a posting from 7 years ago is as useful today as it was back in 2010 !! – Mike Gledhill Aug 14 '17 at 09:17
-
make it a variable table and you won't have to drop it. "declare @tmpTableSizes TABLE " – ARLibertarian May 30 '19 at 15:37
A query (modification of https://stackoverflow.com/a/7892349/1737819) to find a custom name table size in GB. You might try this, replace 'YourTableName' with the name of your table.
SELECT
t.NAME AS TableName,
p.rows AS RowCounts,
CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 / 1024 AS TotalSpaceGB,
SUM(a.used_pages) * 8 / 1024 / 1024 AS UsedSpaceGB ,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 / 1024 AS UnusedSpaceGB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME = 'YourTable'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
UsedSpaceGB DESC, t.Name
- 5,588
- 1
- 41
- 60
-
I'd add `CONVERT(DECIMAL,SUM(a.total_pages))` so it would display info for tables smaller than GB – Bogdan Mart Mar 11 '17 at 15:48
-
Something here is wrong. The query results in a size reported that is more than double what SSMS table properties show. – Vaelek Jun 22 '21 at 16:14
-
This produces incorrect results for tables containing Filtered indices. A filtered index contains a different row-count causing the group-by to produce multiple rows for the same table. I suggest the definition for column RowCounts should be MAX(p.rows) as RowCounts, and p.Rows removed from the GROUP BY ... p.Rows. – ripvlan Apr 08 '22 at 17:57
SQL Server provides a built-in stored procedure that you can run to easily show the size of a table, including the size of the indexes… which might surprise you.
Syntax:
sp_spaceused 'Tablename'
see in :
http://www.howtogeek.com/howto/database/determine-size-of-a-table-in-sql-server/
- 14,933
- 15
- 89
- 118
- 120,002
- 45
- 212
- 219
Do you by size mean the number of records in the table, by any chance? In that case:
SELECT COUNT(*) FROM your_table_name
- 2,773
- 1
- 16
- 20
I know that in SQL 2012 (may work in other versions) you can do the following:
- Right click on the database name in the Object Explorer.
- Select Reports > Standard Reports > Disk Usage by Top Tables.
That will give you a list of the top 1000 tables and then you can order it by data size etc.
- 582
- 6
- 7
And in PostgreSQL:
SELECT pg_size_pretty(pg_relation_size('tablename'));
- 106,435
- 24
- 133
- 132
-
btw, to see results from deletions with this query, you probably need to vacuum. Here's a query to see all public tables at once: `SELECT table_name, pg_size_pretty(pg_relation_size(table_names.table_name)) AS size from (select table_name from information_schema.tables where table_schema = 'public') AS table_names ORDER BY pg_relation_size(table_names.table_name) DESC;` – Noumenon Apr 25 '17 at 16:50
SQL Server, nicely formatted table for all tables in KB/MB:
SELECT
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB,
CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
t.Name
- 5,032
- 26
- 61
- 104
You may refer the answer by Marc_s in another thread, Very useful.
Here's a simple query, if you are just trying to find the largest tables.
-- Find largest table partitions
SELECT top 20 obj.name, LTRIM (STR ( sz.in_row_data_page_count * 8, 15, 0) + ' KB') as Size, * FROM sys.dm_db_partition_stats sz
inner join sys.objects obj on obj.object_id = sz.object_id
order by sz.in_row_data_page_count desc
- 21
- 1
-
2Welcome to Stack Overflow! Although this code may help to solve the problem, it doesn't explain _why_ and/or _how_ it answers the question. Providing this additional context would significantly improve its long-term educational value. Please [edit] your answer to add explanation, including what limitations and assumptions apply. I know it's a poor question - that's even more reason to be clear in the reply. – Toby Speight Oct 03 '16 at 15:59