How to Find Large Tables in MS SQL Database
If your database appears to be larger than you expect it to be, this article will show you how to display a list of your MS SQL Database tables in size order, and list the associate size of the table.
These steps require you to have Microsoft SQL Server Management Studio installed on your computer.
SQL Server Management Studio
- Open and log in to Microsoft SQL Server Management Studio
- Right click your database
- Hover over Reports
- Hover over Standard Reports
- Select Disk Usage by Top Tables
SQL Query
- Open and log in to Microsoft SQL Server Management Studio
- Click the New Query button
- Copy the following script into the New Query page replacing [DatabaseName] with the name of your database
USE [DatabaseName]
GO
CREATE
TABLE
#
temp
(
table_name sysname ,
row_count
INT
,
reserved_size
VARCHAR
(50),
data_size
VARCHAR
(50),
index_size
VARCHAR
(50),
unused_size
VARCHAR
(50))
SET
NOCOUNT
ON
INSERT
#
temp
EXEC
sp_msforeachtable
'sp_spaceused '
'?'
''
SELECT
a.table_name,
a.row_count,
COUNT
(*)
AS
col_count,
a.data_size
FROM
#
temp
a
INNER
JOIN
information_schema.columns b
ON
a.table_name
collate
database_default
= b.table_name
collate
database_default
GROUP
BY
a.table_name, a.row_count, a.data_size
ORDER
BY
CAST
(
REPLACE
(a.data_size,
' KB'
,
''
)
AS
integer
)
DESC
DROP
TABLE
#
temp
- Click the Execute button
Article ID: 227, Created: April 6, 2012 at 10:57 AM, Modified: January 7, 2017 at 9:51 AM