Home Database Mysql Tutorial mysql在线无性能影响删除7G大表_MySQL

mysql在线无性能影响删除7G大表_MySQL

Jun 01, 2016 pm 01:35 PM
mysql online how database server

bitsCN.com

mysql在线无性能影响删除7G大表

 

如何在mysql数据库里删除7G(或更大)大表,使其又不影响服务器的io,导致性能下降影响业务。先不说其是mysql表,就是普通文件,如果直接rm删除,也会使服务器的io性能急剧下降;换个思路如果用化整为零的方式,分多次大大文件一点一点删除,就可以避免因删除文件占用太多服务器io资源

例子: www.bitsCN.com  

版本:

mysql> select version();

+------------+

| version()  |

+------------+

| 5.1.67-log |

+------------+

1 row in set (0.05 sec)

数据表大小:  www.bitsCN.com  

mysql> select count(1) from user4;

+----------+

| count(1) |

+----------+

| 36700160 |

+----------+

1 row in set (1 min 35.66 sec)

[root@racdb2 test]# ll user_bak*

-rw-rw---- 1 mysql dba      10466 Mar  1 13:50 user4_bak.frm

-rw-rw---- 2 mysql dba 7734296576 Mar  1 14:28 user4_bak.ibd

[root@racdb2 test]#

创建一个中间表,来见减少对业务影响

mysql> create table user4_tmp engine=innodb select * from user4 where 1=2;

Query OK, 0 rows affected (0.18 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| a              |

| b              |

| user           |

| user1          |

| user2          |

| user3          |

| user4          |

| user4_tmp      |

| utf8           |

+----------------+

9 rows in set (0.01 sec)

 

把原始表user4重命名

mysql> rename table user4 to user4_bak;

Query OK, 0 rows affected (0.03 sec)

把中间表重命名为原始表user4,如果需要数据,可以导入部分数据

mysql> rename table user4_tmp to user4;

Query OK, 0 rows affected (0.01 sec)

通过文件的硬链接方式删除文件

[root@racdb2 test]# ln user4_bak.ibd user4_bak.ibd.hdlk

[root@racdb2 test]# ll user_bak*

-rw-rw---- 1 mysql dba      10466 Mar  1 13:50 user4_bak.frm

-rw-rw---- 2 mysql dba 7734296576 Mar  1 14:28 user4_bak.ibd

-rw-rw---- 2 mysql dba 7734296576 Mar  1 14:28 user4_bak.ibd.hdlk

[root@racdb2 test]#

注意:

硬连接的作用是允许一个文件拥有多个有效路径名,即文件的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及文件的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。

发现删除7G的文件巨快

mysql> drop table user4_bak;

Query OK, 0 rows affected (0.60 sec)

这个时候在mysql数据库里已经删除了表user4_bak,但系统的存储空间还没有释放,如下所示:

[root@racdb2 test]# ll user_bak*

-rw-rw---- 2 mysql dba 7734296576 Mar  1 14:28 user4_bak.ibd.hdlk

 

只有我们把文件user4_bak.ibd.hdlk删除,磁盘空间才会被释放,那如何尽量少占用系统资源,最小化影响业务来释放这个空间呢?前面已经分析通过化整为零的方式,通过coreutils工具集中的truncate对大文件进行shrink来逐渐释放空间.

 

脚本如下:

[root@racdb2 test]# more /home/mysql/rm_bigfile.sh

#!/bin/bash

#author:skate

#time:2013/02/28

#function:rm huge file

TRUNCATE=/usr/local/bin/truncate

for i in `seq 7384 -100 10 `; #从7384开始每次递减100 ,输出结果见下面

do 

  sleep 1

  echo "$TRUNCATE -s ${i}M /tmp/user4_bak.ibd.hdlk "

  $TRUNCATE -s ${i}M /mysqldata/data/test/user_bak.ibd.hdlk

done

 

执行脚本,然后同时开另一个session,用iostat查看系统io的压力

[root@racdb2 test]# sh /home/mysql/rm_bigfile.sh

发现每1s删除100M的文件,服务器基本没有压力

[root@racdb2 coreutils-8.9]# iostat -mx 2 | grep "sda2"

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00    10.45  0.00  1.00     0.00     0.04    92.00     0.00    0.50   0.50   0.05

sda2              0.00     2.99  0.00  9.95     0.00     0.05    10.40     0.03    3.20   0.25   0.25

sda2              0.00     9.50  0.00  1.00     0.00     0.04    84.00     0.00    1.50   1.50   0.15

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     9.50  0.00  1.00     0.00     0.04    84.00     0.00    0.50   0.50   0.05

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00    14.50  0.00  1.00     0.00     0.06   124.00     0.00    0.50   0.50   0.05

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     9.00  0.00  1.00     0.00     0.04    80.00     0.00    0.50   0.50   0.05

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     9.50  0.00  1.00     0.00     0.04    84.00     0.00    0.50   0.50   0.05

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     9.00  0.00  1.00     0.00     0.04    80.00     0.00    0.50   0.50   0.05

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00    10.55  0.50 10.05     0.00     0.08    16.00     0.04    3.95   1.43   1.51

sda2              0.00     0.00  2.01  0.00     0.01     0.00     8.00     0.02    8.00   8.00   1.61

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     7.54  0.00  1.01     0.00     0.03    68.00     0.00    0.50   0.50   0.05

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     9.50  0.00  1.00     0.00     0.04    84.00     0.00    0.50   0.50   0.05

sda2              0.00     2.99  0.00  1.00     0.00     0.02    32.00     0.00    0.50   0.50   0.05

sda2              0.00     0.00  0.00  0.00     0.00     0.00     0.00     0.00    0.00   0.00   0.00

sda2              0.00     3.02  0.50  1.01     0.00     0.02    24.00     0.02   11.33  11.33   1.71

coreutils的安装

[root@racdb2 test]#wget http://ftp.gnu.org/gnu/coreutils/coreutils-8.9.tar.gz

[root@racdb2 test]#tar -zxvf coreutils-8.9.tar.gz 

[root@racdb2 test]#cd coreutils-8.9

[root@racdb2 test]#./configure

[root@racdb2 test]#make && make install

总结:

1.使用方法:硬链接和化整为零

2.做事之前先思考方法,不要急于动手

3.多角度思考问题,本来是在线问题,在线解决起来,束缚较多,那就把在线变离线;一次删除影响大,那就变多次删除

 

---end----

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

See all articles