Faster TRUNCATE/DROP TABLE

Updated at:

PolarDB optimizes the mechanism that cleans up data pages in the buffer pool. This resolves the slow execution of DDL operations such as TRUNCATE TABLE and DROP TABLE in MySQL Community Edition and avoids the performance bottlenecks caused by full scans. On clusters with large memory or under high loads, this optimization significantly reduces DDL execution time and improves system performance and stability.

Limits

Your PolarDB for MySQL cluster must meet one of the following conditions:

  • MySQL 5.7 with revision version 5.7.1.0.19 or later.

  • MySQL 8.0.1 with revision version 8.0.1.1.50 or later.

  • MySQL 8.0.2 with revision version 8.0.2.2.30.1 or later.

Note

To check the version of your cluster, see PolarDB for MySQL Engine version notes.

Usage

You can use the loose_innodb_flush_pages_using_space_id parameter to enable the Faster TRUNCATE/DROP TABLE feature. For instructions, see Set cluster parameters and node parameters. The following table describes the parameter.

Parameter

Level

Description

loose_innodb_flush_pages_using_space_id

Global

Specifies whether to enable the Faster TRUNCATE/DROP TABLE feature. Valid values:

  • ON: enables the Faster TRUNCATE/DROP TABLE feature.

  • OFF: disables the Faster TRUNCATE/DROP TABLE feature.

Performance test

This topic uses MySQL 5.7 to compare how enabling and disabling the Faster TRUNCATE/DROP TABLE feature affects the execution efficiency of TRUNCATE TABLE operations on clusters with different specifications.

Test method

  1. Run the following command to fill the buffer pool of clusters with different specifications. This prevents the buffer pool from affecting the execution efficiency of the TRUNCATE TABLE operation.

    ip=<cluster_endpoint>
    user=<database_account>
    psw=<database_password>
    port= <database_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. Create the tables to be truncated, t1 and t2, and insert 8,192 rows and 2,097,152 rows of data into them respectively.

    # Create the t1 table and insert 8,196 rows of data.
    row_num=13
    ip=<cluster_endpoint>
    user=<database_account>
    psw=<database_password>
    port= <database_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
    # Create the t2 table and insert 2,097,152 rows of data.
    row_num=21
    ip=<cluster_endpoint>
    user=<database_account>
    psw=<database_password>
    port= <database_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. On clusters with different specifications, enable and disable the Faster TRUNCATE/DROP TABLE feature, and run the TRUNCATE TABLE operation on the tables t1 and t2.

Test results

On clusters with different specifications, record the TRUNCATE TABLE execution time for the tables t1 and t2 with the Faster TRUNCATE/DROP TABLE feature enabled and disabled. The following table shows the test results:

Cluster specifications

Buffer pool (GB)

t1

t2

ON (seconds)

OFF (seconds)

Improvement

ON (seconds)

OFF (seconds)

Improvement

64 cores, 512 GB

374

0.01

5.2

99.81%

0.11

9.48

98.84%

32 cores, 256 GB

192

0.02

2.45

99.18%

0.1

2.65

96.23%

16 cores, 128 GB

96

0.01

1.73

99.42%

0.12

1.86

93.55%

8 cores, 64 GB

42

0.01

0.73

98.63%

0.12

0.79

84.81%

4 cores, 32 GB

24

0.02

0.45

95.56%

0.13

0.53

75.47%

4 cores, 16 GB

12

0.03

0.23

86.96%

0.12

0.35

65.71%

The preceding table shows that enabling the Faster TRUNCATE/DROP TABLE feature significantly improves the execution efficiency of TRUNCATE TABLE operations.

Contact us

If you have any questions about DDL operations, you can search for the DingTalk group number and join the group for consultation. You can directly @ the experts in the group with your questions. The group also has a PolarDB for MySQL assistant online 24/7 to answer your questions. DingTalk group number: 15375044501.