Bug #102304 TRUNCATE PARTITION TABLE cause stalls
Submitted: 20 Jan 2021 3:04 Modified: 21 Jan 2021 13:04
Reporter: linfeng chen Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S5 (Performance)
Version:5.7.21 OS:Any
Assigned to: CPU Architecture:Any
Tags: truncate table

[20 Jan 2021 3:04] linfeng chen
Description:
The operation of truncating a partitioned table affects database performance,
TPS went down from over 30,000 to a few thousand

How to repeat:
1:Database configuration
innodb_buffer_pool_size=32G
innodb_buffer_pool_instances=16
innodb_adaptive_hash_index=1

2:create table and insert data(see private comment)

3:run sysbench
/home/sysbench-0.5/sysbench/sysbench --test=/home/sysbench-0.5/sysbench/tests/db/oltp.lua --db-driver=mysql --debug=off --mysql-db=sysbench --mysql-password=123456 --oltp-tables-count=30 --oltp-distinct-ranges=0 --oltp-index-updates=1 --oltp-non-index-updates=1 --oltp-order-ranges=0 --oltp-point-selects=9 --oltp-simple-ranges=0 --oltp-sum-ranges=0 --oltp_delete_inserts=0 --oltp-table-size=5000000 --num-threads=512 --max-requests=0 --max-time=60000 --oltp-auto-inc=off --mysql-engine-trx=yes --oltp-test-mod=complex --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --oltp-user-delay-min=10 --oltp-user-delay-max=100 --report-interval=1 --forced-shutdown=1 run

4:stop sysbench

5:run sysbench
/home/sysbench-0.5/sysbench/sysbench --test=/home/sysbench-0.5/sysbench/tests/db/oltp.lua --db-driver=mysql --debug=off --mysql-db=sysbench --mysql-password=123456 --oltp-tables-count=29 --oltp-distinct-ranges=0 --oltp-index-updates=1 --oltp-non-index-updates=1 --oltp-order-ranges=0 --oltp-point-selects=9 --oltp-simple-ranges=0 --oltp-sum-ranges=0 --oltp_delete_inserts=0 --oltp-table-size=5000000 --num-threads=512 --max-requests=0 --max-time=60000 --oltp-auto-inc=off --mysql-engine-trx=yes --oltp-test-mod=complex --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=root --oltp-user-delay-min=10 --oltp-user-delay-max=100 --report-interval=1 --forced-shutdown=1 run

6:truncate sbtest30;

Suggested fix:
Locks all hash tables before executing the function(buf_LRU_flush_or_remove_pages)

btr_search_s_lock_all();
DEBUG_SYNC_C("simulate_buffer_pool_scan");
buf_LRU_flush_or_remove_pages_nohash(id, BUF_REMOVE_ALL_NO_WRITE, 0);

Lock the hash table affects performance;
Is the hash table locked just to prevent the adaptive hash from being closed?
Can I use a new lock to prevent closing adaptive hashes? This improves performance
.
[20 Jan 2021 13:17] MySQL Verification Team
Hi Mr. chen,

Thank you for your bug report.

However, this is not a bug.

When partitions are truncated many mutex, locks and guards are taken in order to ensure the validity of both tables and queries. Also, there are some excessive writings to logs for such a massive endeavour. 

In short, this is expected behaviour.

Not a bug.
[21 Jan 2021 13:04] linfeng chen
When truncate is executed, the table is locked and the query is stopped.No adaptive hashing will be added to the table.The lock was added in January 2017.There are no bugs before adding.