Tablespace usage view

This view allows you to easily see the top 10% by Row Count, Space Used, etc. This is useful for answering the question "what's taking up all the space in my database?" I wrote it when asked to find what table(s) were using all the space in a particular database. Not being familiar with the database I used Enterprise Manager Taskpad-> Tables to find large tables and quickly realized the ability to run a query to find the Top 10 tables by spaced used would be very handy. A quick note on the "Cast(8192 as BigInt)." At first, this seems nonsensical but without it any large table may generate an integer overflow during the multiplication. This tip has been tested specifically on SQL 2000 Developer, Standard, and Enterprise. Should work on all editions and service packs of SQL 2000.

CREATE VIEW vw_SpaceUsed
 OBJECT_NAME(id) AS ObjectName,
 SUM(CASE WHEN indid in (0,1,255) THEN rows ELSE 0 END) AS Rows,
 ((SUM(CASE WHEN indid in (0,1,255) THEN Reserved ELSE 0 END) * Cast(8192 as BigInt)) / 1024) As Reserved,
 ((SUM(CASE WHEN indid in (0,1,255) THEN dpages ELSE 0 END) * Cast(8192 as BigInt)) / 1024) AS Data,
 (((SUM(CASE WHEN indid in (0,1,255) THEN used ELSE 0 END) - SUM(CASE WHEN indid in (0,1,255) THEN dpages ELSE 0 END)) * Cast(8192 as BigInt) ) / 1024) as Index_Size,
 (((SUM(CASE WHEN indid in (0,1,255) THEN Reserved ELSE 0 END) - SUM(CASE WHEN indid in (0,1,255) THEN Used ELSE 0 END))  * Cast(8192 as BigInt)) / 1024) as Unused
 OBJECTPROPERTY(id,N'IsMSShipped') = 0

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.

This was first published in December 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.