数据库的硬盘空间如何使用
admin
2023-05-25 17:41:09
0

SQL Server占用的存储空间,包含数据库file占用的存储空间,数据库对象占用的存储空间。

一,数据库file占用的存储空间

1,使用 sys.master_files 查看数据库中各个file占用的存储空间

select db.name as database_name,
    db.is_auto_shrink_on,
    db.recovery_model_desc,
    mf.file_id,
    mf.type_desc,
    mf.name as logic_file_name,
    mf.size*8/1024/1024 as size_gb,
    mf.physical_name,    --mf.max_size,    mf.growth,
    mf.is_percent_growth,
    mf.state_descfrom sys.databases db 
inner join sys.master_files mf 
    on db.database_id=mf.database_idwhere mf.size*8/1024/1024>1  -- GBorder by size_gb desc

2,使用sp_spaceused 查看当前DB的空间使用量

use DB_Studygoexec sys.sp_spaceused

数据库的硬盘空间如何使用

database_sizedatabase_size includes both data and log files.

数据文件的空间利用信息:

  • unallocated space :Space in the database that has not been reserved for database objects.

  • reserved:Total amount of space allocated by objects in the database.

  • data:Total amount of space used by data.

  • index_sizeTotal amount of space used by indexes.

  • unused Total amount of space reserved for objects in the database, but not yet used.

  • database_size will always be larger than the sum of reserved + unallocated space because it includes the size of log files, but reserved and unallocated_space consider only data pages.

3, 按照extent统计data file的disk space usage

从系统page:GAM 和 SGAM 上读取Extent allocate 信息,计算data file有多少extent allocated 或 unallocated。

计算公式:1Extent=8Pages,1Page=8KB

dbcc showfilestats

数据库的硬盘空间如何使用

4, 统计SQL Server 实例中所有数据库的日志文件的disk space usage

dbcc sqlperf(logspace) 返回的结果总是准确的,语句的执行不会对sql server增加负担

dbcc sqlperf(logspace)

数据库的硬盘空间如何使用

二,查看数据库中,各个table 或index 所占用的disk space

1,查看数据库所有table 或index 所占用的disk space

数据库的硬盘空间如何使用

select 
    t.name,    sum(case when ps.index_id<2 then ps.row_count else 0 end) as row_count,    sum(ps.reserved_page_count)*8/1024/1024 as reserved_gb,    sum(ps.used_page_count)*8/1024 as used_mb,    sum( case when ps.index_id<2
                    then ps.in_row_data_page_count+ps.lob_used_page_count+ps.row_overflow_used_page_count              else 0 end
        )*8/1024 as data_used_mb,    sum(case when ps.index_id>=2 
                then ps.in_row_data_page_count+ps.lob_used_page_count+ps.row_overflow_used_page_count             else 0 end
        )*8/1024 as index_used_mbfrom sys.dm_db_partition_stats psinner join sys.tables t    on ps.object_id=t.object_idgroup by t.object_id, t.nameorder by reserved_gb desc

数据库的硬盘空间如何使用

2,在当前DB中,查看某一个Table object 空间使用信息

exec sp_spaceused 'dbo.dt_study'

数据库的硬盘空间如何使用

rows:Number of rows existing in the table.

reserved:Total amount of reserved space for objname.

data:Total amount of space used by data in objname.

index_size:Total amount of space used by indexes in objname.

unused:Total amount of space reserved for objname but not yet used.

三,使用Standard Reports 查看disk space usage

数据库的硬盘空间如何使用

四,查看服务器各个逻辑盘符剩余的disk space

Exec master.sys.xp_fixeddrives

数据库的硬盘空间如何使用

Appendix:

查看数据库中table,indexed等对象的disk空间使用量,但是返回的结果并不十分精确。

sp_spaceused Displays the number of rows, disk space reserved, and disk space used by a table, indexed view, or Service Broker queue in the current database, or displays the disk space reserved and used by the whole database.

Syntax

sp_spaceused [[ @objname = ] 'objname' ] 
        [,[ @updateusage = ] 'updateusage' ]

相关内容

热门资讯

韩国海军提交核潜艇请示报告,正... 澎湃新闻记者 朱郑勇 实习生 王镱家韩国海军已经向韩国联合参谋本部提交了关于建造核动力潜艇的请示报告...
星元晶算携手清华大学,共筑人形... 深圳和天津2026年5月19日-- 2026年5月19日,星元晶算科技(深圳)有限公司与清华大学天津...
毫米波电缆组件厂家梳理 军工与... 导语:毫米波电缆组件作为高频信号传输的核心部件,在雷达、卫星通信、5G基站等场景中需求持续增长。根据...
618大内存折叠屏手机怎么选?... 随着618购物节临近,不少用户开始考虑升级手中的设备,尤其是对存储空间和性能有更高要求的高端用户。在...
进口不锈钢氢气减压阀十大行业标... 进口不锈钢氢气减压阀最新十大品牌是由多个全球知名权威机构和媒体会定期发布阀门行业相关排名榜单,本排行...
原创 投... 当李彦宏在2026年百度 Create 开发者大会上提出“DAA(日活智能体数)将成为 AI 时代新...
市场监管总局开展居民水电气计量... 记者今天了解到,为持续规范供水供电供气市场秩序,切实维护人民群众切身利益,市场监管总局部署开展居民水...
坚定不移沿着习近平总书记指引的... 5月14日,工人在洛轴集团智能工厂生产线上作业。5月13日,由中信重工牵头研制的国家重点研发计划“移...
2026年郑州市区中招政策发布... 5月20日上午,郑州市教育局召开2026年郑州市中招工作会议。今年,郑州中招考试时间为6月22日-2...
坚定不移沿着习近平总书记指引的... 5月的龙门石窟,伊河漾碧,青山夹峙,游人如织。古阳洞内,四层脚手架间,龙门石窟研究院几名年轻人屏息凝...