共計(jì) 2910 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
這篇文章給大家分享的是有關(guān)數(shù)據(jù)庫(kù)的硬盤(pán)空間如何使用的內(nèi)容。丸趣 TV 小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨丸趣 TV 小編過(guò)來(lái)看看吧。
SQL Server 占用的存儲(chǔ)空間,包含數(shù)據(jù)庫(kù) file 占用的存儲(chǔ)空間,數(shù)據(jù)庫(kù)對(duì)象占用的存儲(chǔ)空間。
一,數(shù)據(jù)庫(kù) file 占用的存儲(chǔ)空間
1,使用 sys.master_files 查看數(shù)據(jù)庫(kù)中各個(gè) file 占用的存儲(chǔ)空間
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 查看當(dāng)前 DB 的空間使用量
use DB_Studygoexec sys.sp_spaceused
database_size:database_size includes both data and log files.
數(shù)據(jù)文件的空間利用信息:
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_size:Total 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 統(tǒng)計(jì) data file 的 disk space usage
從系統(tǒng) page:GAM 和 SGAM 上讀取 Extent allocate 信息,計(jì)算 data file 有多少 extent allocated 或 unallocated。
計(jì)算公式:1Extent=8Pages,1Page=8KB
dbcc showfilestats
4,統(tǒng)計(jì) SQL Server 實(shí)例中所有數(shù)據(jù)庫(kù)的日志文件的 disk space usage
dbcc sqlperf(logspace) 返回的結(jié)果總是準(zhǔn)確的,語(yǔ)句的執(zhí)行不會(huì)對(duì) sql server 增加負(fù)擔(dān)
dbcc sqlperf(logspace)
二,查看數(shù)據(jù)庫(kù)中,各個(gè) table 或 index 所占用的 disk space
1,查看數(shù)據(jù)庫(kù)所有 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,在當(dāng)前 DB 中,查看某一個(gè) 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
四,查看服務(wù)器各個(gè)邏輯盤(pán)符剩余的 disk space
Exec master.sys.xp_fixeddrives
Appendix:
查看數(shù)據(jù)庫(kù)中 table,indexed 等對(duì)象的 disk 空間使用量,但是返回的結(jié)果并不十分精確。
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 ]
感謝各位的閱讀!關(guān)于“數(shù)據(jù)庫(kù)的硬盤(pán)空間如何使用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!