SQL Server : delete from large table

vpfxa7rd  于 2023-02-28  发布在  SQL Server
关注(0)|答案(2)|浏览(182)

We have a log table in our production database which is hosted on Azure. The table has grown to about 4.5 million records and now we just want to delete all the records from that log table.

I tried running

Delete from log_table

And I also tried

Delete top 100 from log_table

Delete top 20 from log_table

When I run the queries, database usage jumps to 100% and the query just hangs. I believe this is because of the large number of records in the table. Is there a way we can overcome the issue?

5f0d552i

5f0d552i1#

To delete all rows in a big table, you can use the truncate table command. This command is used to remove all rows from a table, and it is a faster operation than using the DELETE command

Ex:

TRUNCATE TABLE table_name;
0h4hbjxa

0h4hbjxa2#

In an Azure SQL database, you have several options to choose from, for you to control the size of your database and its log file. First of all, let's start with some defenitions:

  • Data space used is the amount of space used to store database data. Generally, space used increases (decreases) on inserts (deletes). In some cases, the space used does not change on inserts or deletes depending on the amount and pattern of data involved in the operation and any fragmentation. For example, deleting one row from every data page does not necessarily decrease the space used.
  • Data space allocated is the amount of formatted file space made available for storing database data. The amount of space allocated grows automatically, but never decreases after deletes. This behavior ensures that future inserts are faster since space does not need to be reformatted.
  • Data space allocated but unused represents the maximum amount of free space that can be reclaimed by shrinking database data files.
  • Data max size is the maximum amount of space that can be used for storing database data. The amount of data space allocated cannot grow beyond the data max size.

In Azure SQL Database, to shrink files you can use either DBCC SHRINKDATABASE or DBCC SHRINKFILE commands:

DBCC SHRINKDATABASE shrinks all data and log files in a database using a single command. The command shrinks one data file at a time, which can take a long time for larger databases. It also shrinks the log file, which is usually unnecessary because Azure SQL Database shrinks log files automatically as needed.

DBCC SHRINKFILE command supports more advanced scenarios:

  • It can target individual files as needed, rather than shrinking all files in the database.
  • Each DBCC SHRINKFILE command can run in parallel with other DBCC SHRINKFILE commands to shrink multiple files at the same time and reduce the total time of shrink, at the expense of higher resource usage and a higher chance of blocking user queries, if they are executing during shrink.
  • If the tail of the file does not contain data, it can reduce allocated file size much faster by specifying the TRUNCATEONLY argument. This does not require data movement within the file.

Now going on to some useful SQL queries:

-- Shrink database data space allocated.
DBCC SHRINKDATABASE (N'database_name');

-- Review file properties, including file_id and name values to reference in shrink commands
SELECT file_id,
       name,
       CAST(FILEPROPERTY(name, 'SpaceUsed') AS bigint) * 8 / 1024. AS space_used_mb,
       CAST(size AS bigint) * 8 / 1024. AS space_allocated_mb,
       CAST(max_size AS bigint) * 8 / 1024. AS max_file_size_mb
FROM sys.database_files
WHERE type_desc IN ('ROWS','LOG');

-- Shrink database data file named 'data_0` by removing all unused at the end of the file, if any.
DBCC SHRINKFILE ('data_0', TRUNCATEONLY);
GO

When it comes to the log file of a database, you can use the following queries:

-- Shrink the database log file (always file_id 2), by removing all unused space at the end of the file, if any.
DBCC SHRINKFILE (2, TRUNCATEONLY);

... and to set the database log file to automatically shrink itself and keep a certain amount of space, you may use:

-- Enable auto-shrink for the current database.
ALTER DATABASE CURRENT SET AUTO_SHRINK ON;

For reference purposes, I didn't get this information myself, but extracted it from this official article in Microsoft Docs

相关问题