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.
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
CREATE VIEW vw_SpaceUsed AS SELECT 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 FROM sysindexes WHERE OBJECTPROPERTY(id,N'IsMSShipped') = 0 GROUP BY OBJECT_NAME(id)
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.