How to quickly clean up billions of data in MySQL database

How to quickly clean up billions of data in MySQL database

Today I received a disk alarm exception. The 50G disk was blown up. The analysis and solution process is as follows:

1. Enter the Linux server and check the disk space occupied by each database in the mysql folder

Did you see that olderdb alone takes up 25G?

2. Use SQLyog to log in to the MySQL database and check the space occupied by each table in the database

SELECT CONCAT(table_schema,'.',table_name) AS 'aaa',  
  table_rows AS 'Number of Rows',  
  CONCAT(ROUND(data_length/(1024*1024*1024),6),' G') AS 'Data Size',  
  CONCAT(ROUND(index_length/(1024*1024*1024),6),' G') AS 'Index Size' ,  
  CONCAT(ROUND((data_length+index_length)/(1024*1024*1024),6),' G') AS'Total' 
FROM information_schema.TABLES  
WHERE table_schema LIKE 'olderdb';

3. Query the primary key index

SHOW INDEX FROM orbit

Adopt a strategy

Prerequisite: Currently 80% of the data needs to be deleted

① delete statement

We know that the deletion speed of the delete statement is proportional to the number of indexes. The number of indexes in this table is already very large, and the amount of data is very large. If a conventional delete statement is used to delete, it will definitely take several days.

Deleting with the Delete statement will not release disk space, so an alarm will definitely appear, so this method is not advisable.

② Drop table

Create a new table with the same structure and name it "cc", insert the data to be saved into this table, and then drop the old table.

The SQL statement is as follows:

Create a new table based on the old table

CREATE TABLE cc LIKE orbit ;

Insert data (millions of data must be inserted in batches, 300,000-400,000 at a time is the best, after all, MySQL's data processing capacity is limited)

Query by date and insert (about 300,000 data points will be generated every day, so date insertion is used)

INSERT INTO cc SELECT * FROM orbit WHERE xttime > '2018-04-16 00:00:00' AND xttime<='2018-04-17 00:00:00';

The results are as follows:

It can be seen that it took less than 5 minutes to process more than 500,000 pieces of data, which is relatively fast.

After cleaning, the data table space is released

Then drop the old table

DROP TABLE orbit

It only took about 3 seconds.

Rename the new table "cc"

ALTER TABLE cc RENAME TO orbit

Summarize

The above is my introduction to how to quickly clean up billions of data in MySQL database. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • A brief discussion on the optimization of MySQL paging for billions of data
  • How to use partitioning to optimize MySQL data processing for billions of data

<<:  Detailed explanation of Vue development website SEO optimization method

>>:  How to add java startup command to tomcat service

Recommend

How to deploy zabbix_agent in docker

zabbix_agent deployment: Recommendation: zabbix_a...

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

How to use CocosCreator to create a shooting game

Analyze the production steps: 1. Prepare resource...

Steps to run ASP.NET Core in Docker container

There are too much knowledge to learn recently, a...

Quickly master how to get started with Vuex state management in Vue3.0

Vuex is a state management pattern developed spec...

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

Summary of commonly used multi-table modification statements in Mysql and Oracle

I saw this question in the SQL training question ...

Use of MySQL SHOW STATUS statement

To do MySQL performance adjustment and service st...

Complete steps for using Echarts and sub-packaging in WeChat Mini Program

Preface Although the holiday is over, it shows up...

Keepalived+Nginx+Tomcat sample code to implement high-availability Web cluster

Keepalived+Nginx+Tomcat to achieve high availabil...

Graphical introduction to the difference between := and = in MySQL

The difference between := and = = Only when setti...

mysql 5.7.11 winx64 initial password change

Download the compressed version of MySQL-5.7.11-w...

Detailed explanation of this reference in React

Table of contents cause: go through: 1. Construct...

Three ways to jump to a page by clicking a button tag in HTML

Method 1: Using the onclick event <input type=...