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

JavaScript Basics Variables

Table of contents 1. Variable Overview 1.1 Storag...

Zabbix configures DingTalk's alarm function with pictures

Implementation ideas: First of all, the alarm inf...

How to use Nginx to handle cross-domain Vue development environment

1. Demand The local test domain name is the same ...

Teach you step by step to configure MySQL remote access

Preface When using the MySQL database, sometimes ...

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...

Introduction to MySQL Connection Control Plugin

Table of contents 1. Introduction to the connecti...

In-depth understanding of MySQL long transactions

Preface: This article mainly introduces the conte...

Detailed explanation of Vue3.0 + TypeScript + Vite first experience

Table of contents Project Creation Project Struct...

How to automatically back up the mysql database regularly

We all know that data is priceless. If we don’t b...

Detailed explanation of using echarts map in angular

Table of contents Initialization of echart app-ba...

The connection between JavaScript and TypeScript

Table of contents 1. What is JavaScript? 2. What ...

MySQL trigger trigger add, delete, modify and query operation example

This article uses examples to describe the add, d...

How to display small icons in the browser title bar of HTML webpage

Just like this effect, the method is also very si...