日期:2014-05-17 浏览次数:20600 次
--111------------------------------------------------------------
--查看数据库所申请的空间以及空间在数据和索引的分布
--1.修改SP_SPACEUSED过程--必须以SP_开头.建立在MASTER底下
--EXEC SP_DBSPACEUSED 'MASTER','FALSE'
ALTER PROCEDURE SP_DBSPACEUSED
@DBNAME SYSNAME ,
@UPDATEUSAGE VARCHAR(5) = FALSE
AS
DECLARE @SQL NVARCHAR(4000)
SET @SQL=
'USE ['+@DBNAME+'];
DECLARE @ID INT
DECLARE @TYPE CHARACTER(2)
DECLARE @PAGES INT
DECLARE @DBNAME SYSNAME
DECLARE @DBSIZE DEC(15,0)
DECLARE @LOGSIZE DEC(15)
DECLARE @BYTESPERPAGE DEC(15,0)
DECLARE @PAGESPERMB DEC(15,0)
DECLARE @UPDATEUSAGE_TEMP VARCHAR(5)
CREATE TABLE #SPT_SPACE
(
ROWS INT NULL,
RESERVED DEC(15) NULL,
DATA DEC(15) NULL,
INDEXP DEC(15) NULL,
UNUSED DEC(15) NULL
)
IF '''+@UPDATEUSAGE+''' IS NOT NULL
BEGIN
SELECT @UPDATEUSAGE_TEMP=LOWER('''+@UPDATEUSAGE+''')
IF @UPDATEUSAGE_TEMP NOT IN (''TRUE'',''FALSE'')
BEGIN
RAISERROR(15143,-1,-1,@UPDATEUSAGE_TEMP)
END
END
IF @UPDATEUSAGE_TEMP = ''TRUE''
BEGIN
DBCC UPDATEUSAGE(0) WITH NO_INFOMSGS
END
SET NOCOUNT ON
BEGIN
SELECT @DBSIZE = SUM(CONVERT(DEC(15),SIZE)) FROM DBO.SYSFILES WHERE (STATUS & 64 = 0)
SELECT @LOGSIZE = SUM(CONVERT(DEC(15),SIZE)) FROM DBO.SYSFILES WHERE (STATUS & 64 <> 0)
SELECT @BYTESPERPAGE = LOW FROM MASTER.DBO.SPT_VALUES WHERE NUMBER = 1 AND TYPE = ''E''
SELECT @PAGESPERMB = 1048576 / @BYTESPERPAGE
INSERT INTO #SPT_SPACE (RESERVED) SELECT SUM(CONVERT(DEC(15),RESERVED)) FROM SYSINDEXES WHERE INDID IN (0, 1, 255)
SELECT @PAGES = SUM(CONVERT(DEC(15),DPAGES)) FROM SYSINDEXES WHERE INDID < 2
SELECT @PAGES = @PAGES + ISNULL(SUM(CONVERT(DEC(15),USED)), 0) FROM SYSINDEXES WHERE INDID = 255
UPDATE #SPT_SPACE SET DATA = @PAGES
UPDATE #SPT_SPACE SET INDEXP = (SELECT SUM(CONVERT(DEC(15),USED)) FROM SYSINDEXES WHERE INDID IN (0, 1, 255))- DATA
UPDATE #SPT_SPACE SET UNUSED = RESERVED-(SELECT SUM(CONVERT(DEC(15),USED)) FROM SYSINDEXES WHERE INDID IN (0, 1, 255))
END
SELECT
DATABASE_NAME = DB_NAME(),
DATABASE_SIZE =LTRIM(STR((@DBSIZE + @LOGSIZE) / @PAGESPERMB,15,2) + '' MB''),
''UNALLOCATED SPACE'' =LTRIM(STR((@DBSIZE -(SELECT SUM(CONVERT(DEC(15),RESERVED))FROM SYSINDEXES WHERE INDID IN (0, 1, 255))) / @PAGESPERMB,15,2)+ '' MB''),
RESERVED = LTRIM(STR(RESERVED * D.LOW / 1024.,15,0) +'' '' + ''KB''),
DATA = LTRIM(STR(DATA * D.LOW / 1024.,15,0) +'' '' + ''KB''),
INDEX_SIZE = LTRIM(STR(INDEXP * D.LOW / 1024.,15,0) +'' '' + ''KB''),
UNUSED = LTRIM(STR(UNUSED * D.LOW / 1024.,15,0) +