Current location - Health Preservation Learning Network - Healthy weight loss - The transaction log file of SQL Server 2000 database is too large. How to reduce it?
The transaction log file of SQL Server 2000 database is too large. How to reduce it?
When the QL server is replicated, all updates to the database will be recorded in the transaction log file of the database. Unless the database is set to shrink automatically or manually, the transaction log file will grow until the preset log file growth limit is reached or the available disk space is exhausted. If the current database file or log file is too large, you can use the following two commands to shrink it: DBCC SHRINKDATABASE: shrink the size of all data and log files in the specified database DBCC SHRINKFILE: shrink the size of the specified data or log file in the database. These two commands can release the free space in the database and shrink the database or the specified database file to the specified size, but the size of the shrunk data file or log file will not be smaller than the size of the existing valid data in the file. For the specific usage of these two commands, please refer to the corresponding topics in SQL Server 2000 Books Online. In addition, you can also perform database contraction in SQL Server Enterprise Manager, and the above two commands are also called, with similar effects. When using the above command to shrink the log file, it should be noted that the transaction log records that have been written into the database but not truncated will not be shrunk, because the information of these log records has been written into the database file, but it will also be used when using the transaction log backup to restore the database. For databases using the simple recovery model, the transaction log is automatically truncated every time a checkpoint is processed. For the database using the full recovery model or bulk-logged recovery model, the transaction log will be truncated only when the log backup is performed, the information recorded in the transaction log will be written into the transaction log backup file, and the space occupied by them will be marked as available (that is, truncated). Truncating the transaction log will not make the log file smaller, but it can free up some space for writing new log records in the future. To reduce the physical size of the log file, use the DBCC SHRINKDATABASE and DBCC SHRINKFILE commands mentioned above. When executing the BACKUP LOG statement, you can also use the WITH NO_LOG (or WITH TRUNCATE_ONLY, with the same meaning) parameter. At this time, the transaction log is not actually backed up, but the inactive part of the transaction log is truncated (this is the same as the ordinary BACKUP LOG statement). This applies when there is not enough disk space left to back up the transaction log, or the inactive part of the transaction log is not intended to be reserved for database recovery. In order to avoid the phenomenon that the transaction log file grows too fast and consumes all the disk space, one method is to set the database to use simple recovery mode, so that SQL Server can automatically truncate the inactive part of the transaction log at regular intervals and recover the space occupied by it for writing into the transaction log records in the future. However, this will make the database unable to recover to the real-time point through transaction log backup and reduce the reliability of the database, so it should not be used in production databases in general. For the production database, it is recommended to use the full recovery model, and make full backups and transaction log backups of the database regularly. For example, a full backup is performed once a week and a transaction log backup is performed once a day, which can be easily realized through the database maintenance plan wizard in SQL Server Enterprise Manager (generally, it can be set to automatically perform backups at a time when business is not busy every night). By regularly performing transaction log backup of the database, the rapid growth of log files can be avoided and the size can be kept at a relatively stable level. Although database backup files also occupy a lot of disk space, they can be moved to other disks or deleted at any time when they are not needed, and the database can be easily restored when there is a fault or misoperation. Because the size of data file increases with the increase of data in the database, the space occupied by deleted data in the database can be used for newly inserted data; After backing up the transaction log regularly, we can control the size of the log file within a reasonable range. Therefore, it is generally not necessary to shrink the database, and it is not recommended to set the database to automatic shrinking mode. It is recommended to shrink the database only in the following cases: 1, the disk space is less than 2, and the data file is very large, but it only contains a small amount of data (maybe there was a large amount of data before, but it was later deleted). It is expected that the data volume of the database will not be very large in the future. 3. The transaction log file is too large because it has not been backed up for a long time. Another way to reduce the transaction log file size is to execute the CHECKPOINT command in the database first, and then detach the database, rename or delete or move the corresponding database log file (. Ldf file) to another directory, and then execute the stored procedure sp_Attach_single_file_db or reattach it in the enterprise manager. Since the original log file cannot be found, SQL Server will automatically create a log file with a size of only 504K for the database. However, this method must temporarily take the database offline, so it is usually not suitable for use in a production environment. If the transaction log file of the current database is too large and must be shrunk, it is recommended to refer to the following steps: 1. It is recommended (but not necessary) to back up the database first: backup the database database _ name to backup _ device.

2. Back up the transaction log: backuplogdatabase _ name to backup _ device If the records in the current transaction log are not needed for database restoration, or there is not enough space for transaction log backup, simply execute the following command to truncate the transaction log: backuplogdatabase _ name with no _ log.

3. Shrink log file: DBCC shrinks the file (log file name), where the log file name is the logical name of the transaction log file, which can be seen in the Transaction Log page of the database properties of Enterprise Manager (for example, the default logical name of the transaction log file in the Northwind database is Northwind_log). 4. If the log file is still large, you can try to back up the log again by using the NO_LOG and DBCC SHRINKFILE commands. 5. If there is still no obvious effect at this time, please execute DBCC OPENTRAN (database_name) to check whether there are active transactions in the current database that have not been committed for a long time. If necessary, you can disconnect these connections, try to truncate the transaction log and shrink the log file again. 6. After the log file is shrunk, it is recommended to make a complete backup of the database immediately, and make an appropriate database backup plan according to actual needs.