在社区MySQL 5.7中,当执行TRUNCATE TABLE和DROP TABLE时,需要扫描整个缓存池(Buffer Pool),将表空间对应的所有数据页从LRU list和FLUSH list中删除。当Buffer Pool比较大时,该操作耗时较长。PolarDB对DDL过程中的Buffer Pool管理机制进行了优化,能够有效提升Buffer Pool的扫描效率,提升TRUNCATE TABLE和DROP TABLE的执行效率。

使用限制

PolarDB MySQL版集群版本需为5.7版本且Revision version为5.7.1.0.21或以上
说明 您可以参见查询版本号确认集群版本。

使用方法

您可以通过loose_innodb_flush_pages_using_space_id参数开启Faster TRUNCATE/DROP TABLE功能,具体操作请参见设置集群参数和节点参数。参数说明如下:
参数级别说明
loose_innodb_flush_pages_using_space_idGlobalFaster TRUNCATE/DROP TABLE功能开关。取值范围如下:
  • ON:开启Faster TRUNCATE/DROP TABLE功能
  • OFF:关闭Faster TRUNCATE/DROP TABLE功能(默认值)

性能测试

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

测试方法
  1. 使用如下命令先将不同规格集群的Buffer Pool打满,避免Buffer Pool影响TRUNCATE TABLE操作的执行效率。
    ip=xxx.xxx.xx.xx
    user='user_name'
    psw='password'
    port=xxx
    MYSQL="mysql --host=$ip -p$psw --port=$port -u$user  -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 t1;"
  2. 创建待TRUNCATE的表t1t2,并分别插入8196行和2097152行数据。
    #创建表t1,并插入8196行数据。
    row_num=13
    ip=xxx.xxx.xx.xx
    user='user_name'
    psw='password'
    port=xxx
    MYSQL="mysql --host=$ip -p$psw --port=$port -u$user  -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=xxx.xxx.xx.xx
    user='user_name'
    psw='password'
    port=xxx
    MYSQL="mysql --host=$ip -p$psw --port=$port -u$user  -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)t1t2
ONOFF提升率ONOFF提升率
64核512 GB3740.015.299.81%0.119.4898.84%
32核256 GB 192 0.022.4599.18%0.12.6596.23%
16核128 GB960.011.7399.42%0.121.8693.55%
8核64 GB420.010.7398.63%0.120.7984.81%
4核32 GB240.020.4595.56% 0.13 0.5375.47%
4核16 GB120.030.2386.96%0.120.3565.71%

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