Faster TRUNCATE/DROP TABLE

PolarDB通过优化内存缓冲区(Buffer Pool)中数据页的清理机制,解决了社区版MySQLTRUNCATE/DROP TABLEDDL操作执行缓慢的问题,避免了因全量扫描带来的性能瓶颈。在大内存或高负载下,这一优化能显著缩短DDL执行时间,提升系统性能与稳定性。

使用限制

您的PolarDB MySQL集群需满足如下条件之一:

  • MySQL5.7版本,且修订版本为5.7.1.0.19及以上。

  • MySQL8.0.1版本,且修订版本为8.0.1.1.50及以上。

  • MySQL8.0.2版本,且修订版本为8.0.2.2.30.1及以上。

说明

您可以参见参考PolarDB MySQL内核版本说明确认集群版本。

使用方法

您可以通过loose_innodb_flush_pages_using_space_id参数开启Faster TRUNCATE/DROP TABLE功能,具体操作请参见设置集群参数和节点参数。参数说明如下:

参数

级别

说明

loose_innodb_flush_pages_using_space_id

Global

Faster TRUNCATE/DROP TABLE功能开关。取值范围如下:

  • ON:开启Faster TRUNCATE/DROP TABLE功能。

  • OFF:关闭Faster TRUNCATE/DROP TABLE功能。

性能测试

本文档使用MySQL 5.7版本,对比了不同规格的集群下,开启和关闭Faster TRUNCATE/DROP TABLE功能对表执行TRUNCATE TABLE操作的执行效率影响情况。

测试方法

  1. 使用如下命令先将不同规格集群的Buffer Pool打满,避免Buffer Pool影响TRUNCATE TABLE操作的执行效率。

    ip=<集群连接地址>
    user=<数据库账号>
    psw=<数据库密码>
    port= <数据库端口>
    MYSQL="mysql -h $ip -P $port -u$user -p$psw -vvv -e"
    $MYSQL "create database test;"
    $MYSQL "use test;create table if not exists t3 (a bigint,b char(250),c char(250),d char(250),e char(250)) charset=latin1;"
    $MYSQL "use test;insert into t3 values(1,repeat('x', 255),repeat('x', 255),repeat('x', 255),repeat('x', 255));"
    for ((i=1; i<=32; i+=1))
    do
       $MYSQL --host=$ip -p$psw --port=$port -u$user  -vvv -e "use test;insert into t3 select * from t3;"
    done
    $MYSQL  "use test; select count(*) from t3;"
  2. 创建待TRUNCATE的表t1t2,并分别插入8192行和2097152行数据。

    #创建表t1,并插入8196行数据。
    row_num=13
    ip=<集群连接地址>
    user=<数据库账号>
    psw=<数据库密码>
    port= <数据库端口>
    MYSQL="mysql -h $ip -P $port -u$user -p$psw -vvv -e"
    $MYSQL "use test;create table if not exists t1 (a bigint,b char(250),c char(250),d char(250),e char(250)) charset=latin1;"
    $MYSQL "use test;insert into t1 values(1,repeat('x', 255),repeat('x', 255),repeat('x', 255),repeat('x', 255));"
    for ((i=1; i<=$row_num; i+=1))
    do
       $MYSQL "use test;insert into t1 select * from t1;"
    done
    #创建表t2,并插入2097152行数据。
    row_num=21
    ip=<集群连接地址>
    user=<数据库账号>
    psw=<数据库密码>
    port= <数据库端口>
    MYSQL="mysql -h $ip -P $port -u$user -p$psw -vvv -e"
    $MYSQL "use test;create table if not exists t2 (a bigint,b char(250),c char(250),d char(250),e char(250)) charset=latin1;"
    $MYSQL "use test;insert into t2 values(1,repeat('x', 255),repeat('x', 255),repeat('x', 255),repeat('x', 255));"
    for ((i=1; i<=$row_num; i+=1))
    do
       $MYSQL "use test;insert into t2 select * from t2;"
    done
  3. 在不同规格的集群中,分别开启和关闭Faster TRUNCATE/DROP TABLE功能,对表t1t2进行TRUNCATE TABLE操作。

测试结果

在不同规格的集群中,分别记录开启和关闭Faster TRUNCATE/DROP TABLE功能后,对表t1t2进行TRUNCATE TABLE操作所需的执行时间,实验结果如下所示:

集群规格

Buffer Pool(GB)

t1

t2

ON(秒)

OFF(秒)

提升率

ON(秒)

OFF(秒)

提升率

64512 GB

374

0.01

5.2

99.81%

0.11

9.48

98.84%

32256 GB

192

0.02

2.45

99.18%

0.1

2.65

96.23%

16128 GB

96

0.01

1.73

99.42%

0.12

1.86

93.55%

864 GB

42

0.01

0.73

98.63%

0.12

0.79

84.81%

432 GB

24

0.02

0.45

95.56%

0.13

0.53

75.47%

416 GB

12

0.03

0.23

86.96%

0.12

0.35

65.71%

从上表可以看出,开启Faster TRUNCATE/DROP TABLE功能后,能显著提升TRUNCATE TABLE操作的执行效率。

联系我们

若您对DDL操作有任何疑问,可通过钉钉搜索群号入群咨询。您可以直接@群内专家,并附上您要咨询的问题;同时群内也有PolarDB MySQL小助手24*7小时在线回答您的问题。钉钉群号:15375044501。