mysql batch delete large amounts of data

mysql batch delete large amounts of data

mysql batch delete large amounts of data

Assume that there is a table (syslogs) with 10 million records. You need to delete all the records with statusid=1 without stopping the business. There are about 6 million records. If you directly execute DELETE FROM syslogs WHERE statusid=1, you will find that the deletion fails because of the lock wait timeout exceed error.

Because this statement involves too many records, we delete them in batches using the LIMIT parameter, for example, deleting every 10,000 records.

MySQL can be completed with the following statement:

 DELETE FROM syslogs WHERE status=1 ORDER BY statusid LIMIT 10000;

Then you can successfully delete these records by executing it multiple times.

Note:

When performing large-scale deletions, be sure to use limit. Because if you don't use limit, deleting a large amount of data is likely to cause deadlock.

If the where clause of delete is not on the index, you can first find the primary key and then delete the database based on the primary key.

It is best to add limit 1 when updating and deleting to prevent accidental operations.

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Code for batch deleting data tables with the same prefix in Mysql in PHP

<<:  Vue2.x - Example of using anti-shake and throttling

>>:  How to set Tomcat as an automatically started service? The quickest way

Recommend

Analysis of the Docker deployment Consul configuration process

Execute Command docker run -d --name consul -p 85...

A detailed summary of HTML tag nesting rules suitable for beginners

I have been relearning HTML recently, which can be...

Using react-beautiful-dnd to implement drag and drop between lists

Table of contents Why choose react-beautiful-dnd ...

15-minute parallel artifact GNU Parallel Getting Started Guide

GNU Parallel is a shell tool for executing comput...

CSS3 uses scale() and rotate() to achieve zooming and rotation

1. scale() method Zoom refers to "reducing&q...

How to install rabbitmq-server using yum on centos

Socat needs to be installed before installing rab...

Tips for implementing multiple borders in CSS

1. Multiple borders[1] Background: box-shadow, ou...

Detailed explanation of Vue routing router

Table of contents Using routing plugins in a modu...

Nginx solves cross-domain issues and embeds third-party pages

Table of contents Preface difficulty Cross-domain...

Vue calculated property implementation transcript

This article shares the Vue calculation property ...

How to remotely connect to the cloud server database using Navicat

It is very convenient to connect to a remote serv...

Some "pitfalls" of MySQL database upgrade

For commercial databases, database upgrade is a h...

Detailed explanation of the basic use of Apache POI

Table of contents Basic Introduction Getting Star...

JS achieves five-star praise effect

Use JS to implement object-oriented methods to ac...