在线上truncate 了一张2000w 的表:
mysql [localhost:5724] {root} (test) > truncate table t1;
Query OK, 0 rows affected (1 min 37.19 sec)
发现全局系统都慢了一下,processlist 中发现truncate操作处于system lock
mysql [localhost:5724] {root} ((none)) > show processlist;
+--------+------+-----------+------+---------+------+-------------+-------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+------+-----------+------+---------+------+-------------+-------------------+
| 104077 | root | localhost | test | Query | 54 | System lock | truncate table t1 |
| 104078 | root | localhost | NULL | Query | 0 | starting | show processlist |
+--------+------+-----------+------+---------+------+-------------+-------------------+
2 rows in set (0.03 sec)
mysql [localhost:5724] {root} ((none)) > show processlist;
+--------+------+-----------+------+---------+------+-------------+-------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+------+-----------+------+---------+------+-------------+-------------------+
| 104077 | root | localhost | test | Query | 60 | System lock | truncate table t1 |
| 104078 | root | localhost | NULL | Query | 0 | starting | show processlist |
+--------+------+-----------+------+---------+------+-------------+-------------------+
2 rows in set (0.00 sec)
mysql [localhost:5724] {root} ((none)) > show processlist;
+--------+------+-----------+------+---------+------+-------------+-------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+------+-----------+------+---------+------+-------------+-------------------+
| 104077 | root | localhost | test | Query | 61 | System lock | truncate table t1 |
| 104078 | root | localhost | NULL | Query | 0 | starting | show processlist |
+--------+------+-----------+------+---------+------+-------------+-------------------+
2 rows in set (0.00 sec)
关于 System lock 官方的解释,
The thread has called mysql_lock_tables()
and the thread state has not been updated since. This is a very general state that can occur for many reasons.
For example, the thread is going to request or is waiting for an internal or external system lock for the table. This can occur when InnoDB
waits for a table-level lock during execution of LOCK TABLES
. If this state is being caused by requests for external locks and you are not using multiple mysqld servers that are accessing the same MyISAM
tables, you can disable external system locks with the --skip-external-locking
option. However, external locking is disabled by default, so it is likely that this option will have no effect. For SHOW PROFILE
, this state means the thread is requesting the lock (not waiting for it).
关于system lock 的解释官方说明也是不够多,核心就是表锁,也就是在drop 操作时申请的表锁
#####
https://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
mysql drop 操作的原理:
Truncate operations drop and re-create the table, It requires the DROP privilege.
mysql 5.7 如果有大bp 情况下,truncate table 会导致性能临时骤降,mysql 5.7 的truncate 操作在业务期间需要极其谨慎啊
On a system with a large InnoDB buffer pool and innodb_adaptive_hash_index enabled,
TRUNCATE TABLE operations may cause a temporary drop in system performance due to an LRU scan that occurs
when removing an InnoDB table's adaptive hash index entries.
The problem was addressed for DROP TABLE in MySQL 5.5.23 (Bug #13704145, Bug #64284)
but remains a known issue for TRUNCATE TABLE (Bug #68184)