search
HomeComputer TutorialsComputer KnowledgeMethods and steps for compressing database log files

Methods and steps for compressing database log files

How to compress database log files

There are three specific methods.

method one:

first step:

backup log database_name with no_log

or backup log database_name with truncate_only

-- no_log and truncate_only are synonymous here, you can execute any sentence.

Step 2:

1. Shrink all data and log files of a specific database, execute:

dbcc shrinkdatabase (database_name,[,target_percent])

-- database_name is the name of the database to be shrunk; target_percent is the percentage of remaining free space required in the database file after the database is shrunk.

2. Shrink the data or log files in a specific database at a time, execute

dbcc shrinkfile(file_id,[,target_size])

-- file_id is the identification (ID) number of the file to be shrunk. To obtain the file ID, use the FILE_ID function or search sysfiles in the current database; target_size is the desired file size in megabytes (using expressed as an integer). If not specified, dbcc shrinkfile reduces the file size to the default file size. Both dbcc can have the parameter notruncate or truncateonly. For details, see the online help.

Method Two:

first step:

Back up the entire database first to prepare for emergencies.

Step 2:

After the backup is completed, execute the following statement in Query Analyzer:

exec sp_detach_db yourDBName,true

--Uninstall the registration information of this DB in MSSQL

third step:

Go to the directory where the physical log file is located to delete the log file or move the log file out of the directory

the fourth step:

Execute the following statement in Query Analyzer:

exec sp_attach_single_file_db yourDBName,'

d:\mssql\data\yourDBName_data.mdf '

--Register the DB as a single file. If successful, MSSQL will automatically generate a 500K log file for this DB.

Method 3:

1. Enter the Enterprise Manager and select the database, such as demo

2. All tasks->Separate database

3. Go to the directory where the database file is stored and delete the MuOnline_log.LDF file. Just in case, you can copy it out

4. Enterprise Manager -> Attach database, select muonline. At this time, you will see that the log file item is a cross. It does not matter, continue. At this time, the database will prompt you whether to create a new one if the database has no logs. That’s it for sure.

5. Remember that the user needs to reset it after the database is reattached.

If you don’t want it to get bigger in the future:

Used under SQL2000:

Right-click on the database->Properties->Options->Failure Recovery-Model-Select-Simple Model.

Or use SQL statement:

alter database database name set recovery simple

What to do if the SQL database is too large

--1. Daily database compression

--Compress log and database file size

/*--pay attention

Please follow the steps. If you have not completed the previous steps, please do not do the following steps

Otherwise it may damage your database.

--*/

1. Clear the log

DUMP TRANSACTION library name WITH NO_LOG

2. Truncate transaction log:

BACKUP LOG database name WITH NO_LOG

3. Shrink the database file (if not compressed, the database file will not be reduced

Enterprise Manager--right-click the database you want to compress--All tasks--Shrink database--Shrink files

--Select the log file--In the shrink mode, select shrink to XXM. A minimum number of M allowed to be shrunk will be given here. Enter this number directly and confirm.

--Select the data file--In the shrink mode, select shrink to XXM. There will be a minimum number of M allowed to be shrunk. Enter this number directly and confirm.

You can also use SQL statements to complete

--Shrink database

DBCC SHRINKDATABASE(Customer Data)

--Shrink the specified data file, 1 is the file number, which can be queried through this statement: select * from sysfiles

DBCC SHRINKFILE(1)

4. In order to minimize the log file size (if it is SQL 7.0, this step can only be performed in the query analyzer)

a. Separate database:

Enterprise Manager--Server--Database--Right-click--Detach Database

b. Delete LOG files on my computer

c. Additional database:

Enterprise Manager--Server--Database--Right-click--Attach Database

This method will generate a new LOG, the size is only more than 500K

or use code:

The following example detaches pubs and then appends a file in pubs to the current server.

a.Separation

EXEC sp_detach_db @dbname = 'pubs'

b. Delete log files

c.Add

EXEC sp_attach_single_file_db @dbname = 'pubs',

@physname = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf'

5. In order to automatically shrink in the future, make the following settings:

Enterprise Manager--Server--right-click the database--Properties--Options--Select "Auto Shrink"

--SQL statement setting method:

EXEC sp_dboption 'database name', 'autoshrink', 'TRUE'

6. If you want to prevent the log from growing too large in the future

Enterprise Manager--Server--Right-click Database--Properties--Transaction Log

--Limit file growth to xM (x is the maximum data file size you allow)

--SQL statement setting method:

alter database database name modify file(name=logical file name, maxsize=20)

The above is the detailed content of Methods and steps for compressing database log files. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
How to Fix Attachment Not Showing in Outlook 365?How to Fix Attachment Not Showing in Outlook 365?Apr 19, 2025 am 12:50 AM

Do you have trouble downloading or sending attachments in Outlook 365? Sometimes, Outlook doesn’t show them for some unknown reason, so you are unable to see them. In this post on php.cn Website, we collect some use tips for attachments not showing.

How to Fix V Rising Connection Timed out? Here Are 5 Solutions! - MiniToolHow to Fix V Rising Connection Timed out? Here Are 5 Solutions! - MiniToolApr 19, 2025 am 12:49 AM

When V Rising players try to join a server that is close to or already full, they may encounter the “V Rising connection timed out” issue. If you are one of them, you can refer to this post from php.cn to get solutions. Now, keep on your reading.

How to Turn on/off Real-Time Protection in Windows Security? - MiniToolHow to Turn on/off Real-Time Protection in Windows Security? - MiniToolApr 19, 2025 am 12:48 AM

Windows supplies real-time protection via Windows Security. But this feature may prevent you from doing something it thinks are dangerous. In this situation, you may want to temporarily turn on real-time protection. This php.cn post will show you how

Windows 11 24H2 or Windows 12? Does This Matter?Windows 11 24H2 or Windows 12? Does This Matter?Apr 19, 2025 am 12:47 AM

Microsoft has started working on next year’s Windows updates very early. Recent rumors state that the next update in 2024 might be Windows 11 24H2 rather than Windows 12. Everything is uncertain now. php.cn will now take you to see some related infor

Fix: Unable to Perform Requested Operation – Error 0x80030001 - MiniToolFix: Unable to Perform Requested Operation – Error 0x80030001 - MiniToolApr 19, 2025 am 12:46 AM

The error 0x80030001 often happens when you are attempting to copy files. The error code will be accompanied by a message that tells “unable to perform requested operation”. If you are struggling with this error, you can read this article on php.cn W

How to Download and Install Windows 11 KB5034765How to Download and Install Windows 11 KB5034765Apr 19, 2025 am 12:45 AM

On February 13, 2024, Microsoft released KB5034765 (OS builds 22621.3155 and 22631.3155) for Windows 11 22H2 and Windows 11 23H2. This security update brings you many new improvements and bug fixes. You can learn how to download and install Windows 1

Power Management Tab Missing from Device Manager - Top GuidePower Management Tab Missing from Device Manager - Top GuideApr 19, 2025 am 12:44 AM

Device Manager is widely used when you need to fix some computer issues. You can check the problematic devices and decide to uninstall or update device drivers. Besides, you can also set Power Management settings in Device Manager. However, you may f

2 Ways to Reset Windows Backup to Default in Windows 11/102 Ways to Reset Windows Backup to Default in Windows 11/10Apr 19, 2025 am 12:43 AM

When Backup and Restore (Windows Backup) fails to work, you can choose to reset it to default. How to restore Windows Backup to default in Windows 11/10? php.cn will guide you to easily do this thing in 2 ways and let’s go to see them.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.