本主題涵蓋如何監視 SQL Server SQL Server 交易記錄大小、壓縮交易記錄、加入或加大交易記錄檔、最佳化 tempdb 交易記錄成長率,以及控制交易記錄檔的成長。 This topic covers how to monitor SQL Server SQL Server transaction log size, shrink the transaction log, add to or enlarge a transaction log file, optimize the tempdb transaction log growth rate, and control the growth of a transaction log file.
監視記錄空間的使用 Monitor log space use
使用 DBCC SQLPERF (LOGSPACE) 來監視記錄空間的使用。
Monitor log space use by using DBCC SQLPERF (LOGSPACE). 這個命令會傳回目前使用之記錄空間量的相關資訊,並指出交易記錄需要截斷的時機。 This command returns information about the amount of log space currently used, and indicates when the transaction log needs truncation. 如需詳細資訊,請參閱 DBCC SQLPERF Transact-SQL。 For more information, see DBCC SQLPERF Transact-SQL.
如需目前的記錄檔大小、大小上限及檔案的自動成長選項等詳細資訊,您也可以在 sys.database_files 中使用該記錄檔的 size、max_size 和 growth 資料行。 For information about the current log file size, its maximum size, and the autogrow option for the file, you can also use the size, max_size, and growth columns for that log file in sys.database_files. 如需詳細資訊,請參閱 sys.database_files (Transact-SQL)。 For more information, see sys.database_files (Transact-SQL).
重要! Important! 請避免讓記錄磁碟多載! Avoid overloading the log disk!
壓縮記錄檔大小 Shrink log file size
若要減少實體記錄檔的實體大小,則必須壓縮記錄檔。 To reduce the physical size of a physical log file, you must shrink the log file. 如果您知道交易記錄檔包含未使用的空間,則這十分有用。 This is useful when you know that a transaction log file contains unused space. 只有當資料庫已上線,而且至少有一個虛擬記錄檔可用時,您才能壓縮記錄檔。 You can shrink a log file only while the database is online, and at least one virtual log file is free. 在某些情況下,壓縮記錄可能要等到下一個記錄截斷之後才能進行。
In some cases, shrinking the log may not be possible until after the next log truncation.
注意
像長時間執行的交易之類的因素,使虛擬記錄檔保持作用中一段很長的時間,可能限制記錄檔壓縮,甚至完全阻止記錄檔壓縮。 Factors, such as a long-running transaction, that keep virtual log files active for an extended period can restrict log shrinkage or even prevent the log from shrinking at all. 如需延遲記錄截斷可能因素的相關資訊,請參閱 交易記錄 (SQL Server)。 For information about factors that can delay log truncation, see The Transaction Log (SQL Server).
壓縮記錄檔會移除一或多個不保留任何邏輯記錄的虛擬記錄檔 (即「非使用中虛擬記錄檔」)。
Shrinking a log file removes one or more virtual log files that hold no part of the logical log (that is, inactive virtual log files). 當交易記錄檔壓縮之後,就會從記錄檔的結尾移除非使用中的虛擬記錄檔,將記錄縮減至大約目標大小。 When a transaction log file is shrunk, inactive virtual log files are removed from the end of the log file to reduce the log to approximately the target size.
壓縮記錄檔 (但不壓縮資料庫檔案) Shrink a log file (without shrinking database files)
注意
您可以將記錄檔設定為自動壓縮。 You can set log files to shrink automatically. 不過,我們建議您不要進行自動壓縮,而且 autoshrink 資料庫屬性預設為 FALSE。 However, we recommend against automatic shrinking, and the autoshrink database property is set to FALSE by default. 如果 autoshrink 設定為 TRUE,只有當超過 25% 的空間未使用時,自動壓縮才會減少檔案的大小。 If autoshrink is set to TRUE, automatic shrinking reduces the size of a file only when more than 25 percent of its space is unused. 此時,檔案會壓縮成只有 25% 的檔案是未使用空間的大小,或檔案的原始大小,以較大者為準。 The file is shrunk either to the size at which only 25 percent of the file is unused space or to the original size of the file, whichever is larger. 如需變更 autoshrink 屬性設定的相關資訊,請參閱
檢視或變更資料庫的屬性—使用 [選項] 頁面的 Auto Shrink 屬性—或 ALTER DATABASE SET 選項 (Transact-SQL)—使用 AUTO_SHRINK 選項。 For information about changing the setting of the autoshrink property, see View or Change the Properties of a Database—use the Auto Shrink property on the Options page—or
ALTER DATABASE SET Options (Transact-SQL)—use the AUTO_SHRINK option.
加入或加大記錄檔 Add or enlarge a log file
您可以加大現有的記錄檔 (如果磁碟空間允許的話),或是將記錄檔加入資料庫 (通常是在不同的磁碟上),來取得空間。 You can gain space by enlarging the existing log file (if disk space permits) or by adding a log file to the database, typically on a different disk.
若要對資料庫新增一個記錄檔,請使用 ALTER DATABASE 陳述式的 ADD LOG FILE 子句。 To add a log file to the database, use the ADD LOG FILE clause of the ALTER DATABASE statement. 新增記錄檔可讓記錄檔增大。 Adding a log file allows the log to grow.
若要加大記錄檔,可以使用 ALTER DATABASE 陳述式的 MODIFY FILE 子句,並指定 SIZE 與 MAXSIZE 語法。 To enlarge the log file, use the MODIFY FILE clause of the ALTER DATABASE statement, specifying the SIZE and MAXSIZE syntax. 如需詳細資訊,請參閱 ALTER DATABASE (Transact-SQL)。 For more information, see ALTER DATABASE (Transact-SQL).
最佳化 tempdb 交易記錄的大小 Optimize tempdb transaction log size
重新啟動伺服器執行個體時,就會將 tempdb 資料庫的交易記錄大小重新調整為自動成長之前的原始大小。
Restarting a server instance resizes the transaction log of the tempdb database to its original, pre-autogrow size. 這樣會降低 tempdb 交易記錄的效能。 This can reduce the performance of the tempdb transaction log. 您可以在啟動或重新啟動伺服器執行個體後,增加 tempdb 交易記錄的大小,藉以避免這項負擔。 You can avoid this overhead by increasing the size of the tempdb transaction log after starting or restarting the server instance. 如需詳細資訊,請參閱 tempdb Database。 For more information, see tempdb Database.
控制交易記錄檔的成長 Control transaction log file growth
請使用 ALTER DATABASE (Transact-SQL) 陳述式來管理交易記錄檔的成長。 Use the ALTER DATABASE (Transact-SQL) statement to manage the growth of a transaction log file. 請注意下列事項: Note the following:
- 若要變更目前的檔案大小 (單位為 KB、MB、GB 和 TB),請使用 SIZE 選項。 To change the current file size in KB, MB, GB, and TB units, use the SIZE option.
- 若要變更成長的增量,請使用 FILEGROWTH 選項。
To change the growth increment, use the FILEGROWTH option. 0 的值表示將自動成長設為關閉,而且不允許任何其他空間。 A value of 0 indicates that automatic growth is set to off and no additional space is permitted. 記錄檔的少量自動成長增量可能會降低效能。 A small autogrowth increment on a log file can reduce performance. 記錄檔的檔案成長量應夠大,才不用經常進行擴充。 The file growth increment on a log file should be sufficiently large to avoid frequent expansion. 通常適當的預設成長量為 10%。 The default growth increment of 10 percent is generally suitable.
如需對記錄檔變更檔案成長屬性的相關資訊,請參閱 ALTER DATABASE (Transact-SQL)。
For information on changing the file-growth property on a log file, see ALTER DATABASE (Transact-SQL).
- 若要控制記錄檔大小的最大值 (單位為 KB、MB、GB 和 TB) 或是將成長設定為 UNLIMITED,請使用 MAXSIZE 選項。 To control the maximum the size of a log file in KB, MB, GB, and TB units or to set growth to UNLIMITED, use the MAXSIZE option.
另請參閱 See also
BACKUP (Transact-SQL) BACKUP (Transact-SQL)
寫滿交易記錄疑難排解 (SQL Server 錯誤 9002) Troubleshoot a Full Transaction Log (SQL Server Error 9002)