back to top

Determining the size of SQL database tables and indexes

In SQL 2000, you used to be able to view table details for a SQL database and see the size of each table based on data size used and index sized used.  This information was available right from the SQL enterprise manager.  The only problem with that was the tables were sorted by table name, not size so it was a little time consuming to find what the biggest tables were.

Enterprise Manager

SQL 2005 dropped this view from the SQL Management Studio, so there is no graphical way of seeing the table size usage.

There is however a way of getting this information and it is fairly straight forward. 

First off there is a SQL stored procedure called sp_SpaceUsed.  This procedure allows you to see the space usage for a single table.  See this posting.  This procedure only allows you to query one table at a time.  We can use another stored procedure however in conjunction with the first.  There is an undocumented stored procedure called sp_MSForEachTable.  This procedure acts like a cursor allowing you to loop through all the tables in a database and perform some kind of action on them.  IN this case, it allows us to run the sp_SpaceUsed procedure on every table.  This undocumented procedure simply takes the command to be run against every table and then you substitute the specific table name with a “?”, like so:

EXECUTE sp_MSforeachtable 'EXECUTE sp_spaceused [?]'

Now using these procedures in conjunction with each other produces several results sets, one result set for each table.  This could be improved by taking the various results and populating them into a temporary table.  Then you can examine the contents of this table to get the results of all table information returned in one result set.

Here is a query that does just that.  The query can be modified by editing the @Sort variable so that the results are returned in various sorting directions.

 

SET NOCOUNT ON
/*
Sorting options
Set the @Sort value to one of the following
0 = Alphabetically by the name of the table
1 = By row count
2 = By table size
3 = By unused space
4 = By index size
5 = By data size
*/

DECLARE @Sort int

–*****************************************************************************
SELECT @Sort = 2   –Change this value to control sort option.  See notes above
–*****************************************************************************

–Creates temp Table
CREATE TABLE #SizeTable
 (    Table_Name varchar(50),
    Row_Count int,
    Table_Size varchar(50),
    Data_Space_Used varchar(50),
    Index_Space_Used varchar(50),
    Unused_Space varchar(50)
 )
–Create Stored Procedure String
 –Populate Temporary Table
 INSERT INTO #SizeTable EXEC(‘sp_msforeachtable ”sp_spaceused “?””’)

CREATE TABLE #ResultTable
 (    Table_Name varchar(50),
    Row_Count int,
    Table_Size int,
    Data_Space_Used int,
    Index_Space_Used int,
    Unused_Space int
 )
insert into #ResultTable select Table_Name, replace(row_count,’kb’,”),
replace(Table_Size,’kb’,”),replace(Data_Space_Used,’kb’,”),replace(Index_Space_Used,’kb’,”),
replace(Unused_Space,’kb’,”) from #SizeTable

–Determine sorting method
  If @Sort =0   SELECT * FROM #ResultTable ORDER BY Table_Name
  If @Sort =1   SELECT * FROM #ResultTable ORDER BY Row_Count Desc
  If @Sort =2   SELECT * FROM #ResultTable ORDER BY Table_Size Desc
  If @Sort =3   SELECT * FROM #ResultTable ORDER BY Unused_Space Desc
  If @Sort =4   SELECT * FROM #ResultTable ORDER BY Index_Space_Used Desc      
  If @Sort =5   SELECT * FROM #ResultTable ORDER BY Data_Space_Used Desc          
 
–Delete Temporay Tables
DROP TABLE #ResultTable
DROP TABLE #SizeTable

 

This query should work on SQL 2000, 2005 and 2008 instances.

Contact Customer FX for your CRM project
We let our expertise speak for itself - let us know how our all-star team can help on your CRM project
About the Author
Kris Halsrud
Kris Halsrud
Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Infor CRM Sync for Exchange: Action Required by August 31, 2026

Using Infor CRM Sync for Exchange with Microsoft 365? Your Exchange administrator must update tenant settings by August 31, 2026 to prevent sync disruption.

[Webinar] What’s Coming Next for Infor CRM SLX and Why v10 Matters

See what’s coming next for Infor CRM SLX, including new marketing workflows, automation improvements, and reasons to consider upgrading to v10.

Disable Caching in Infor CRM SLX Web Client

In the rare case caching is causing issues in the Infor CRM SLX web client, it is possible to disable specific types of caching by making edits using Application Architect, and then deploying.

Exploring AI Options for Infor CRM v10

I've started taking a closer look at the AI capabilities built into Infor CRM v10. Over the next several posts, I'll share what I learn. From the built-in features and supported AI providers to implementation considerations, costs, and practical use cases.

Infor CRM SLX 10.0: Delegate Ownership Changes for “Everyone” Records

Learn how the new EveryoneOwnerAssignment secured action in Infor CRM SLX 10.0 allows designated users to change ownership of Everyone-owned records without requiring Administrator access.

Related Articles

Infor CRM Sync for Exchange: Action Required by August 31, 2026

Using Infor CRM Sync for Exchange with Microsoft 365? Your Exchange administrator must update tenant settings by August 31, 2026 to prevent sync disruption.

[Webinar] What’s Coming Next for Infor CRM SLX and Why v10 Matters

See what’s coming next for Infor CRM SLX, including new marketing workflows, automation improvements, and reasons to consider upgrading to v10.

Disable Caching in Infor CRM SLX Web Client

In the rare case caching is causing issues in the Infor CRM SLX web client, it is possible to disable specific types of caching by making edits using Application Architect, and then deploying.

Exploring AI Options for Infor CRM v10

I've started taking a closer look at the AI capabilities built into Infor CRM v10. Over the next several posts, I'll share what I learn. From the built-in features and supported AI providers to implementation considerations, costs, and practical use cases.

Related Videos