全部產品
Search
文件中心

PolarDB:Faster TRUNCATE/DROP TABLE

更新時間:Sep 12, 2025

PolarDB通過最佳化記憶體緩衝區(Buffer Pool)中資料頁的清理機制,解決了社區版MySQL中TRUNCATE/DROP TABLE等DDL操作執行緩慢的問題,避免了因全量掃描帶來的效能瓶頸。在大記憶體或高負載下,這一最佳化能顯著縮短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(秒)

提升率

64核512 GB

374

0.01

5.2

99.81%

0.11

9.48

98.84%

32核256 GB

192

0.02

2.45

99.18%

0.1

2.65

96.23%

16核128 GB

96

0.01

1.73

99.42%

0.12

1.86

93.55%

8核64 GB

42

0.01

0.73

98.63%

0.12

0.79

84.81%

4核32 GB

24

0.02

0.45

95.56%

0.13

0.53

75.47%

4核16 GB

12

0.03

0.23

86.96%

0.12

0.35

65.71%

從上表可以看出,開啟Faster TRUNCATE/DROP TABLE功能後,能顯著提升TRUNCATE TABLE操作的執行效率。

聯絡我們

若您對DDL操作有任何疑問,請聯絡我們