一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

關(guān)于查看MSSQL 數(shù)據(jù)庫(kù) 用戶每個(gè)表 占用的空間大小

最近做項(xiàng)目需要查看數(shù)據(jù)用戶表的大小,包括記錄條數(shù)和占用的磁盤(pán)空間數(shù)目。在網(wǎng)上找了很久其中查看MSSQL數(shù)據(jù)庫(kù)每個(gè)表占用的空間大小 相對(duì)還可以。
不過(guò)它的2、3中方法返回的數(shù)據(jù)比較多,有些是我們不關(guān)心的數(shù)據(jù),我在AdventureWorks2012數(shù)據(jù)上做的測(cè)試。其中第二種方法代碼如下:
復(fù)制代碼 代碼如下:
View Code
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
create table tablespaceinfo --創(chuàng)建結(jié)果存儲(chǔ)
(nameinfo varchar(50) ,
rowsinfo int , reserved varchar(20) ,
datainfo varchar(20) ,
index_size varchar(20) ,
unused varchar(20) )
delete from tablespaceinfo --清空數(shù)據(jù)表
declare @tablename varchar(255) --表名稱
declare @cmdsql varchar(500)
DECLARE Info_cursor CURSOR FOR
select o.name
from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1
and o.name not like N'#%%' order by o.name
OPEN Info_cursor
FETCH NEXT FROM Info_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
execute sp_executesql
N'insert into tablespaceinfo exec sp_spaceused @tbname',
N'@tbname varchar(255)',
@tbname = @tablename
FETCH NEXT FROM Info_cursor
INTO @tablename
END
CLOSE Info_cursor
DEALLOCATE Info_cursor
GO
--itlearner注:顯示數(shù)據(jù)庫(kù)信息
sp_spaceused @updateusage = 'TRUE'
--itlearner注:顯示表信息
select *
from tablespaceinfo
order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc

運(yùn)行效果如圖:
 
很顯然這個(gè)返回結(jié)果是錯(cuò)誤的。但是它提供了一種思路,修改后的SQL語(yǔ)句如下:
復(fù)制代碼 代碼如下:
View Code
IF NOT EXISTS ( SELECT  *
                FROM    sys.tables
                WHERE   name = 'tablespaceinfo' )
    BEGIN
        CREATE TABLE tablespaceinfo --創(chuàng)建結(jié)果存儲(chǔ)
            (
              Table_Name VARCHAR(50) ,
              Rows_Count INT ,
              reserved INT ,
              datainfo INT ,
              index_size INT ,
              unused INT
            )
    END
DELETE  FROM tablespaceinfo
 --清空數(shù)據(jù)表
CREATE TABLE #temp --創(chuàng)建結(jié)果存儲(chǔ)
    (
      nameinfo VARCHAR(50) ,
      rowsinfo INT ,
      reserved VARCHAR(20) ,
      datainfo VARCHAR(20) ,
      index_size VARCHAR(20) ,
      unused VARCHAR(20)
    )
DECLARE @tablename VARCHAR(255)
 --表名稱
DECLARE @cmdsql NVARCHAR(500)
DECLARE Info_cursor CURSOR
FOR
    SELECT  '[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']' AS Table_Name
    FROM    [INFORMATION_SCHEMA].[TABLES]
    WHERE   TABLE_TYPE = 'BASE TABLE'
            AND TABLE_NAME <> 'tablespaceinfo'
OPEN Info_cursor
FETCH NEXT FROM Info_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @cmdsql = 'insert into #temp exec sp_spaceused ''' + @tablename
            + ''''
        EXECUTE sp_executesql @cmdsql
        FETCH NEXT FROM Info_cursor
INTO @tablename
    END
CLOSE Info_cursor
DEALLOCATE Info_cursor
GO
--itlearner注:顯示數(shù)據(jù)庫(kù)信息
--sp_spaceused @updateusage = 'TRUE'
--itlearner注:顯示表信息
UPDATE  #temp
SET     reserved = REPLACE(reserved, 'KB', '') ,
        datainfo = REPLACE(datainfo, 'KB', '') ,
        index_size = REPLACE(index_size, 'KB', '') ,
        unused = REPLACE(unused, 'KB', '')
INSERT  INTO dbo.tablespaceinfo
        SELECT  nameinfo ,
                CAST(rowsinfo AS INT) ,
                CAST(reserved AS INT) ,
                CAST(datainfo AS INT) ,
                CAST(index_size AS INT) ,
                CAST(unused AS INT)
        FROM    #temp
DROP TABLE #temp
SELECT  Table_Name ,
        Rows_Count ,
        CASE WHEN reserved > 1024
             THEN CAST(reserved / 1024 AS VARCHAR(10)) + 'Mb'
             ELSE CAST(reserved AS VARCHAR(10)) + 'KB'
        END AS Data_And_Index_Reserved ,
        CASE WHEN datainfo > 1024
             THEN CAST(datainfo / 1024 AS VARCHAR(10)) + 'Mb'
             ELSE CAST(datainfo AS VARCHAR(10)) + 'KB'
        END AS Used ,
        CASE WHEN Index_size > 1024
             THEN CAST(index_size / 1024 AS VARCHAR(10)) + 'Mb'
             ELSE CAST(index_size AS VARCHAR(10)) + 'KB'
        END AS index_size ,
        CASE WHEN unused > 1024 THEN CAST(unused / 1024 AS VARCHAR(10)) + 'Mb'
             ELSE CAST(unused AS VARCHAR(10)) + 'KB'
        END AS unused
FROM    dbo.tablespaceinfo
ORDER BY reserved DESC

運(yùn)行結(jié)果如圖:

同時(shí)他的第三種方法返回的數(shù)據(jù)太多,很多是我們不怎么關(guān)心的,原SQL語(yǔ)句如下:
復(fù)制代碼 代碼如下:
View Code

SELECT  OBJECT_NAME(id) tablename ,
 * reserved / 1024 reserved ,
        RTRIM(8 * dpages / 1024) + 'Mb' used ,
 * ( reserved - dpages ) / 1024 unused ,
 * dpages / 1024 - rows / 1024 * minlen / 1024 free ,
        rows
FROM    sysindexes
WHERE   indid = 1
ORDER BY reserved DESC

運(yùn)行結(jié)果如圖:

這里面包含一些索引信息,其實(shí)我們只關(guān)心表占用磁盤(pán)信息,修改后的SQL語(yǔ)句如下:
復(fù)制代碼 代碼如下:
View Code
SELECT  OBJECT_NAME(id) tablename ,
        CASE WHEN reserved * 8 > 1024 THEN RTRIM(8 * reserved / 1024) + 'MB'
             ELSE RTRIM(reserved * 8) + 'KB'
        END DataReserve ,
        CASE WHEN dpages * 8 > 1024 THEN RTRIM(8 * dpages / 1024) + 'MB'
             ELSE RTRIM(dpages * 8) + 'KB'
        END Used ,
        CASE WHEN 8 * ( reserved - dpages ) > 1024
             THEN RTRIM(8 * ( reserved - dpages ) / 1024) + 'MB'
             ELSE RTRIM(8 * ( reserved - dpages )) + 'KB'
        END unused ,
        CASE WHEN ( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 ) > 1024
             THEN RTRIM(( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 )
                        / 1024) + 'MB'
             ELSE RTRIM(( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 ))
                  + 'KB'
        END FREE ,
        rows AS Rows_Count
FROM    sys.sysindexes
WHERE   indid = 1
        AND status = 2066 -- status='18'
ORDER BY reserved DESC

運(yùn)行結(jié)果如下:

有不對(duì)的地方歡迎大家拍磚!

php技術(shù)關(guān)于查看MSSQL 數(shù)據(jù)庫(kù) 用戶每個(gè)表 占用的空間大小,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 日韩一区二区三区免费视频 | 国产免费小视频在线观看 | 天堂成人在线视频 | 亚洲国产一区二区三区亚瑟 | 99福利在线 | 国产一区三区二区中文在线 | 日韩亚洲综合精品国产 | 日韩在线视频不卡 | 久久精品中文字幕极品 | 国产精品视频成人 | 国产我不卡 | 国产精品原创巨作av | 久青草国产手机视频免费观看 | 婷婷四色 | 五月婷婷综合网 | 亚综合 | 黄网观看| 91久久婷婷国产综合精品青草 | 美女视频黄的全免费视频网站 | 亚洲成人一区二区 | baoyu168成人免费视频 | 九月丁香婷婷 | 91热久久免费频精品99欧美 | 成人小视频在线观看 | 草草视频免费看 | 五月婷久久 | 色婷婷激情五月综合 | 玖玖国产 | 亚洲国产成人久久综合野外 | 曰本还a大片免费无播放器 曰本视频网络www色 | 一区二区在线视频观看 | 91精品国产综合久久久久久 | 日韩午夜精品 | 欧美性第一页 | 色网在线播放 | 91精选视频| 天天综合天天做天天综合 | 成人亲子乱子伦视频 | 国产超91| 亚洲精品国产手机 | 色成年激情久久综合 |