Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ to delete large tables from the database

Shulou Source: shulou.com Published: 2022-06-02 09:21:41 09月22日 Update

This article introduces the relevant knowledge of "how to use C++ to delete large tables in the database". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Background: the IO pressure of the database is very great. If the large table is deleted online, it will cause great jitter to IO and business. The common practice as a DBA is to establish a hard connection between .frm and .ibd, and then on drop table tablename;, but the table file still exists on this machine. If you delete the table file directly with rm, it will cause the until of IO to reach 100%.

Question: so how to avoid IO reaching 100% when deleting files?

Solution: gradually erase files through ftruncate. Below, I provide my own slowrm to delete files. Test the io usage between 10-20%, and delete at a speed of more than 100G per hour (here is only a rough version, you can modify it yourself)

# include

# include

# include

# include

# include

# include

/ / determine whether the input is legal

Int fileInputLegal (const char * pfile=nullptr)

Int main (int argc,char * argv []) {

/ / use stat to get the file size

/ / call open to open the file and get the file descriptor

/ / call ftrucate to reduce the size of the file until 0

Int fd =-1

/ / this parameter has been adjusted

Int deleteBytes = 1024700

/ / the purpose is to get the length of the file

Struct stat buf

Off_t fileSize = 0

If (argc! = 2) {

Std::cout

Tags: Files data databases size caching content parameters methods more knowledge inappropriate legal practical large rough frequent counterproductive successful learning next business Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Shulou Tech Info Xiaomi Shulou Information