2015-06-05 24 views
4

當向SQL Server添加.MDF.NDF)或.LDF文件時,我們可以選擇設置其初始大小,自動增長,和增量(百分比或絕對)。SQL Server - 是否可以找到在MDF或LDF文件中實際使用的大小

數據庫運行一段時間後,是否有可能找到數據使用了多少實際大小?例如,如果文件的實際大小爲5M,但只有2M用於存儲數據,則該文件在需要增長之前仍可以獲取3M數據。

我需要一種方法來找出文件總的當前大小(5M)中的「2M」(使用大小)。

回答

1

如果您使用自己的特定初始大小參數創建了數據庫,則不需要知道該數據庫,除非您已經編寫了數據庫創建腳本。

否則,通常默認初始大小被認爲與model數據庫相同。所以,如果它的默認,然後你可以用model數據庫初始大小一般爲3MB

0

有人糾正我,如果我錯了,但對於數據文件,我相信一個*未壓縮的備份應該大致對應的實際數據量查詢在文件中。例如,如果數據庫是2GB,但備份是1GB,則您有1GB的數據。至於你自己的日誌。

2

sys.database_files中(的Transact-SQL)
包含每一個數據庫文件中的一行存儲在數據庫本身。這是每個數據庫視圖。 ...

size  | int | Current size of the file, in 8-KB pages. 
     |  | For a database snapshot, size reflects the maximum space that the snapshot can ever use for the file. 
---------+-----+------------------------------------------------ 
max_size | int | Maximum file size, in 8-KB pages. 
     |  | Databases that are upgraded with an unlimited log file size will report -1 for the maximum size of the log file. 

FILEPROPERTY(的Transact-SQL) 返回指定文件名和屬性名時,指定的文件名的屬性值。 ...

SpaceUsed | Amount of space that is used by the specified file. | Number of pages allocated in the file 

使用它們像這樣:

SELECT 
    name, 
    size, 
    FILEPROPERTY(name, 'SpaceUsed') AS SpaceUsed, 
    size - FILEPROPERTY(name, 'SpaceUsed') As UnusedSize 
FROM 
    sys.database_files 
2

經過一番研究,我發現FILEPROPERTY功能。

SELECT FILEPROPERTY(name, 'SpaceUsed') spaceUsed, * 
FROM sysfiles 

它似乎給了我在文件的當前大小內使用了多少。例如,如果文件的當前大小爲5M,則FILEPROPERTY()可能會給我2M,這意味着文件在需要增長之前仍然可以獲取3M數據。

如果有人能與我確認,我會將其標記爲答案。

0

如何找到SQL日誌文件(.LDF)的大小

DECLARE @command varchar(1000) 
SELECT @command = 'USE ? select [Name], physical_name [Path], 
CAST(size AS BIGINT)*8192 [TotalBytes], 
CAST(FILEPROPERTY(name,''SpaceUsed'') AS BIGINT)*8192 [UsedBytes], 
(case when max_size<0 then -1 else CAST(max_size AS BIGINT)*8192 end) [MaxBytes] 
from sys.database_files' 
Declare @dtTable table 
(
[Name] varchar(max), 
physical_name varchar(max), 
[TotalBytes] varchar(max), 
[UsedBytes] varchar(max), 
[MaxBytes] varchar(max) 
) 
insert into @dtTable EXEC sp_MSforeachdb @command 
select * from @dtTable where physical_name like '%.ldf' 
select * from @dtTable where physical_name like '%.mdf' 
相關問題