Thanks for this article, it was very helpful in explaining how space is reported in the different columns of the sysindexes table, but I am left wondering why we would want to have so many separate queries, and a temporary table, when a single query something like this would do:
/* --optional, if you want to store the data:if NOT exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SpaceUsedByObject]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)BeginCREATE Table SpaceUsedByObject( [Id] INT Identity(1,1), ObjName sysname, TotalSpaceUsed INT, DataSpaceUsed INT, IndexSpaceUsed INT, RowCnt INT, TodayDate DateTime Default Getdate())EndINSERT INTO SpaceUsedByObject(ObjName, TotalSpaceUsed, DataSpaceUsed, IndexSpaceUsed, RowCnt)*/
SELECT Object_Name(sysindexes.ID) AS ObjName, Sum(Used) AS TotalSpaceUsed, Sum(CASE WHEN IndID = 255 THEN Used ELSE Dpages END) AS DataSpaceUsed, Sum(Used) - Sum(CASE WHEN IndID = 255 THEN Used ELSE Dpages END) AS IndexSpaceUsed, Sum(CASE WHEN IndID = 255 THEN 0 ELSE rowcnt END) AS RowCntFROM sysindexes INNER JOIN sysobjects ON sysindexes.Id=sysobjects.Id and type='u'WHERE Indid IN (0,1,255)GROUP BY sysindexes.IDORDER BY TotalSpaceUsed DESC --Added because that was really what I was interested in
Is there any disadvantage to doing everything in a single query, eg are the CASE statements more expensive than the multiple joins, subqueries and updates?
The version which I use in my production database is similar to what you have mentioned. Just few more columns and change in the table name. When I wrote the article and send for proof reading with case statements I got a feedback of using multiple queries so it will be easier to understand and NOT using a table variable as many new user many not know about it. Even one of my friend mention DO NOT change the way sp_spaceused have it logic (ie with multiple queries) or you will be receive many questions as why are you using case statement.
In fact the queries which are mentioned in the articles will fail if there are objects with same name and different owners but I did not wanted to complicate the article so I left that segment out of the picture
Secondly I feel case statment will have a better performance then multiple queries but did not bench mark it. Thanks for the feedback I will try to cover this kind of ambiguities in future
Thanks for the explanation!
I ran that whole query from
CREATE Table SpaceUsedByObject to ... UPDATE SpaceUsedByObject
I have sql server 2000 version Enterprise Edition and service packs 3a and ms03-031.:
I got error:
Server: Msg 512, Level 16, State 1, Line 42Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.The statement has been terminated.Server: Msg 512, Level 16, State 1, Line 51Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.The statement has been terminated.
when I double click the error , it is pointing to this line in bold:
SET IndexSpaceUsed = TotalSpaceUsed - (SELECT DataSpaceUsed FROM #SpaceUsedByObjectForDpage S1 WHERE S1.ObjName=SpaceUsedByObject.ObjName AND SpaceUsedByObject.IndexSpaceUsed IS NULL ) WHERE IndexSpaceUsed IS NULL
Did you get an error ?
You have the same object name under different owner that is the reason you are receiving the error. You can use the case statement to get your output
Hi, could this be because of What Amit mentioned above?
The sample script will not work is there are multiple objects with the same name but different owners, in the same database.
You are correct again. I guess I should have included this as part of the article. Good I am learning the art of writing
That is a real good solution for calculating table space. However all these days i was using following script:
SET NOCOUNT ONGOIF OBJECT_ID('TEMPDB..#TMP_TBL_FOR_SPACEUSED') IS NOT NULL DROP TABLE #TMP_TBL_FOR_SPACEUSEDGOCREATE TABLE #TMP_TBL_FOR_SPACEUSED (NAME VARCHAR (130),ROWS NUMERIC, RESERVED VARCHAR (20), DATA VARCHAR (20), INDEX_SIZE VARCHAR (20), UNUSED VARCHAR (20))
DECLARE CUR1 CURSOR FOR SELECT NAME FROM SYSOBJECTS WHERE XTYPE = 'U'DECLARE @TAB_NAME AS VARCHAR(256)OPEN CUR1FETCH NEXT FROM CUR1 INTO @TAB_NAMEWHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO #TMP_TBL_FOR_SPACEUSED EXEC SP_SPACEUSED @TAB_NAME FETCH NEXT FROM CUR1 INTO @TAB_NAME ENDCLOSE CUR1DEALLOCATE CUR1SELECT * FROM #TMP_TBL_FOR_SPACEUSED ORDER BY 2 DESCGOIF OBJECT_ID('TEMPDB..#TMP_TBL_FOR_SPACEUSED') IS NOT NULL DROP TABLE #TMP_TBL_FOR_SPACEUSEDGO
May not be a good option, still .........