2012-05-10 1 views
3

이 절차는 db에 사용 된 공간을 정확하게 표시합니까? 나는 결과를 의심하고있다.테이블에서 사용되는 공간

DECLARE @TableName VARCHAR(100) --For storing values in the cursor 

--Cursor to get the name of all user tables from the sysobjects listing 
DECLARE tableCursor CURSOR 
FOR 
select [name] 
from dbo.sysobjects 
where OBJECTPROPERTY(id, N'IsUserTable') = 1 
FOR READ ONLY 

--A procedure level temp table to store the results 
CREATE TABLE #TempTable 
(
    tableName varchar(100), 
    numberofRows varchar(100), 
    reservedSize varchar(50), 
    dataSize varchar(50), 
    indexSize varchar(50), 
    unusedSize varchar(50) 
) 

--Open the cursor 
OPEN tableCursor 

--Get the first table name from the cursor 
FETCH NEXT FROM tableCursor INTO @TableName 

--Loop until the cursor was not able to fetch 
WHILE (@@Fetch_Status >= 0) 
BEGIN 
    --Dump the results of the sp_spaceused query to the temp table 
    INSERT #TempTable 
     EXEC sp_spaceused @TableName 

    --Get the next table name 
    FETCH NEXT FROM tableCursor INTO @TableName 
END 

--Get rid of the cursor 
CLOSE tableCursor 
DEALLOCATE tableCursor 

--Select all records so we can use the reults 
SELECT * 
FROM #TempTable order BY tablename 

--Final cleanup! 
DROP TABLE #TempTable 

이 게시물의 형식에 대해 죄송합니다. StackO 확실한 버그입니다 - 오늘은 서식 도구 모음이 없습니다.

답변

1

코드는 사용 된 공간을 테이블별로 보여줍니다. 매개 변수없이 sp_spaceused을 실행하면 전체 데이터베이스의 크기에 대한 개요를 볼 수 있습니다. 결과를 의심하게 만드는 원인은 무엇입니까?

+0

저장된 proc이 테이블 이름을 param으로 허용한다는 점에 감사드립니다. proc의 출력은 위에서 나온 출력과 일치합니다. 사용 된 테이블 공간의 합계가 KB를 MB로 변환 한 후 db 크기를 초과했기 때문에 나는 그 점을 의심했습니다. – ChadD

1

당신은, 예를 들어, system's dynamic views/functions

중 하나를 사용하는 것이 좋습니다 클러스터 힙 및 클러스터되지 않은 인덱스에 사용되는 페이지에 훨씬 더 자세한 정보를 반환 할 sys.dm_db_index_physical_stats를 사용하여이 간단한 쿼리를 고려해 볼 수 있습니다

select * from sys.dm_db_index_physical_stats ( 
    DEFAULT 
    , DEFAULT 
    , DEFAULT 
    , DEFAULT 
    , 'DETAILED' 
) 
관련 문제