Sqlserver,資料庫檔案大小和剩餘空間,所有表名和行數及空間佔用量
在MS Sql Server中可以能過以下的方法查詢出磁碟空間的使用情況及各資料庫資料檔案及日誌檔案的大小及使用利用率:
1、查詢各個磁碟分割槽的剩餘空間:
Exec master.dbo.xp_fixeddrives
2、查詢資料庫的資料檔案及日誌檔案的相關資訊(包括檔案組、當前檔案大小、檔案最大值、檔案增長設定、檔案邏輯名、檔案路徑等)
select * from [資料庫名].[dbo].[sysfiles]
轉換檔案大小單位為MB:
select name, convert(float,size) * (8192.0/1024.0)/1024. from [資料庫名].dbo.sysfiles
3、查詢當前資料庫的磁碟使用情況:
Exec sp_spaceused
4、查詢資料庫伺服器各資料庫日誌檔案的大小及利用率
DBCC SQLPERF(LOGSPACE)
//查詢所有表名
select name from sysobjects where xtype='u' --modify_date指表結構最後更新日期,並非資料最後更新日期
SELECT
name, object_id, principal_id, schema_id, parent_object_id, type,
type_desc, create_date, modify_date, is_ms_shipped, is_published,
is_schema_published, lob_data_space_id, filestream_data_space_id,
max_column_id_used, lock_on_bulk_load, uses_ansi_nulls, is_replicated,
has_replication_filter, is_merge_published, is_sync_tran_subscribed,
has_unchecked_assembly_data, text_in_row_limit,
large_value_types_out_of_row
FROM sys.tablesORDER BY modify_date DESC
//查詢資料庫中所有的表名及行數
SELECT a.name, b.rows
FROM sys.sysobjects AS a INNER JOIN
sys.sysindexes AS b ON a.id = b.id
WHERE (b.indid IN (0, 1)) AND (a.type = 'u')
ORDER BY a.name, b.rows DESC
//查詢所有的標明及空間佔用量\行數
SELECT
OBJECT_NAME(id) AS tablename, 8 * reserved / 1024 AS reserved, RTRIM(8 *
dpages) + 'kb' AS used, 8 * (reserved - dpages) / 1024 AS unused,
8 * dpages / 1024 - rows / 1024 * minlen / 1024 AS free
FROM sys.sysindexes
WHERE (indid = 1)
ORDER BY tablename, reserved DESC
//查詢資料庫中的所有資料庫名
SELECT Name FROM Master..SysDatabases ORDER BY Name
//查詢某個資料庫中所有的表名
SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
//獲取一個數據庫中的所有表的名稱、一個表中所有欄位的名稱
SELECT (case when a.colorder=1 then d.name else null end) 表名,
a.colorder 欄位序號,a.name 欄位名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 標識,
(case when (SELECT count(*) FROM sysobjects
WHERE (name in (SELECT name FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
AND (xtype = 'PK'))>0 then '√' else '' end) 主鍵,b.name 型別,a.length 佔用位元組數,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as 長度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小數位數,(case when a.isnullable=1 then '√'else '' end) 允許空,
isnull(e.text,'') 預設值,isnull(g.[value], ' ') AS [說明]
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
left join sys.extended_properties f on d.id=f.class and f.minor_id=0
where b.name is not null
--WHERE d.name='要查詢的表' --如果只查詢指定表,加上此條件
order by a.id,a.colorder