全部產品
Search
文件中心

PolarDB:Faster TRUNCATE/DROP TABLE

更新時間:Jul 06, 2024

在社區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_id

Global

Faster 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)

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操作的執行效率。