SQL SERVER – Find Number of Rows and Disk Space Reserved – Using sp_spaceused Interesting Observation

Previously I posted SQL SERVER – Find Row Count in Table – Find Largest Table in Database – T-SQL. Today we will look into the same issue but with some additional interesting detail.
We can find the row count using another system SP sp_spaceused. This SP gives additional information regarding disk space reserved on database as well. Well, when I ran the SP on AdventureWorks first time, I suspected that database SP is not providing me correct results.

SQL SERVER - Find Number of Rows and Disk Space Reserved - Using sp_spaceused Interesting Observation spaceused

After a bit investigating I found that it may be possible that due to any reason may be the usage on AdventureWorks database might not be updated. I ran same SP with additional param as shown below and it provided me updated information for the same. Once I ran the SP with @updateusage param, it updated the status of the database. When I ran it again next time without param it gave the same results.

SQL SERVER - Find Number of Rows and Disk Space Reserved - Using sp_spaceused Interesting Observation spaceused1

The reason for above behavior is when dellocation of space happened in database (e.g. dropping table or truncating table) it does not release the used space right away. However, when @updateusage param is used database engine scans whole database and update the sapce usage in respective tables.
Let me know if you guys have any interesting observation like this.

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Data Storage, SQL Function, SQL Scripts, SQL Server DBCC, SQL Stored Procedure, SQL Utility
Previous Post
SQL SERVER – Find Row Count in Table – Find Largest Table in Database – T-SQL
Next Post
SQL SERVER – Remove Duplicate Entry from Comma Delimited String – UDF

Related Posts

Leave a Reply