dz̸ΪʲôMySQLdeleteɾ

 ʱ䣺20210104 09:56:52   ߣ  
ƪҪdz̸ΪʲôMySQLdeleteɾݣͨʾܵķdzϸԴҵѧϰ߹һIJοѧϰֵҪСһѧϰѧϰ

ǰ

Ҹмϵͳҵ洢MySQLеҵʱڵҵ񷽲£͵ϮûӦѺܶܿ죬ܿ찡ڼҴˣû͵¸JoinıSQLúԵӦýӿڵresponse timeҲ䳤ˣӰû顣

ºҵҵ񷽣Ǹ˵Ҫ£æҵǸžʹգߵʱҶ˵´βҪˣβ֭ú÷˼

黹ǵýʱҷԭ֣Щܿ죬ӦSQLɨ˺ܶЧݣSQLͨȷ֮ЩһЩˮ¼־ݣֻҪ13£ʱҪԱʵһ㶼뵽insert + deleteķʽȥ

ƪһInnoDB洢ռֲdeleteܵӰ죬ԼŻ鷽Ϊʲôdeleteɾݡ

InnoDB洢ܹ

ͼԿInnoDB洢ṹҪ֣߼洢ṹ洢ṹ

߼ɱռtablespace >  segmentinode > Extent >ҳPageɣInnodb߼λsegmentռСλextentÿsegmentӱռFREE_PAGEз32page32pageʱᰴԭչǰС1extentչ1extentռС32MBʱÿչһextentռ32MBÿչ4extent

Ҫϵͳûļ־ļɣļҪ洢MySQLֵݺûݣ־ļ¼data pageı¼MySQL CrashʱĻָ

Innodbռ

InnoDB洢ռ䣺ϵͳռ䣬ûռ䣬Undoռ䡣

**ϵͳռ䣺**Ҫ洢MySQLڲֵݣinformation_schemaµݡ

**ûռ䣺**innodb_file_per_table=1ʱݱϵͳռ洢table_name.ibdļУṹϢ洢table_name.frmļС

**Undoռ䣺**洢UndoϢһ¶flashbackundoϢ

MySQL 8.0ʼûԶռ䣬﷨£

CREATE TABLESPACE tablespace_name
  ADD DATAFILE 'file_name'        #ļ
  USE LOGFILE GROUP logfile_group    #Զ־ļ飬һÿ2logfile
  [EXTENT_SIZE [=] extent_size]     #С
  [INITIAL_SIZE [=] initial_size]    #ʼС 
  [AUTOEXTEND_SIZE [=] autoextend_size] #Զߴ
  [MAX_SIZE [=] max_size]        #ļsize32G
  [NODEGROUP [=] nodegroup_id]      #ڵ
  [WAIT]
  [COMMENT [=] comment_text]
  ENGINE [=] engine_name

ĺôǿݵȷ룬ֱHDDSSD洢ʵݵĸЧʣܽԼɱ500GӲ̣vg߼lvĿ¼mountӦlv軮ֵĿ¼ֱ/hot_data /cold_data

ͿԽĵҵû洢ڸSSDϣһЩ־ˮ洢ͨHDDϣҪIJ£

#ݱռ
create tablespace tbs_data_hot add datafile '/hot_data/tbs_data_hot01.dbf' max_size 20G;
#ҵ洢ݱռ
create table booking(id bigint not null primary key auto_increment,  ) tablespace tbs_data_hot;
#ݱռ
create tablespace tbs_data_cold add datafile '/hot_data/tbs_data_cold01.dbf' max_size 20G;
#־ˮı洢ݱռ
create table payment_log(id bigint not null primary key auto_increment,  ) tablespace tbs_data_cold;
#ƶһռ
alter table payment_log tablespace tbs_data_hot;

Inndob洢ֲ

ձ鿴ռ仯

mysql> create table user(id bigint not null primary key auto_increment, 
  -> name varchar(20) not null default '' comment '', 
  -> age tinyint not null default 0 comment 'age', 
  -> gender char(1) not null default 'M' comment 'Ա',
  -> phone varchar(16) not null default '' comment 'ֻ',
  -> create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'ʱ',
  -> update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '޸ʱ'
  -> ) engine = InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'ûϢ';
Query OK, 0 rows affected (0.26 sec)
# ls -lh user1.ibd 
-rw-r----- 1 mysql mysql 96K Nov 6 12:48 user.ibd

òinnodb_file_per_table=1ʱʱԶһsegmentͬʱһextent32data page洢ݣĿձĬϴС96KBextentʹ֮64ҳһЩСundo segmentڿʼʱٵĿռ䣬ʡĿ

# python2 py_innodb_page_info.py -v /data2/mysql/test/user.ibd
page offset 00000000, page type <File Space Header>
page offset 00000001, page type <Insert Buffer Bitmap>
page offset 00000002, page type <File Segment inode>
page offset 00000003, page type <B-tree Node>, page level <0000>
page offset 00000000, page type <Freshly Allocated Page>
page offset 00000000, page type <Freshly Allocated Page>
Total number of page: 6:   #ܹҳ
Freshly Allocated Page: 2   #õҳ
Insert Buffer Bitmap: 1    #뻺ҳ
File Space Header: 1     #ļռͷ
B-tree Node: 1        #ҳ
File Segment inode: 1     #ļinondeibdata1.ibdϻжinode

ݺĿռ仯

mysql> DELIMITER $$
mysql> CREATE PROCEDURE insert_user_data(num INTEGER) 
  -> BEGIN
  ->   DECLARE v_i int unsigned DEFAULT 0;
  -> set autocommit= 0;
  -> WHILE v_i < num DO
  ->  insert into user(`name`, age, gender, phone) values (CONCAT('lyn',v_i), mod(v_i,120), 'M', CONCAT('152',ROUND(RAND(1)*100000000)));
  -> SET v_i = v_i+1;
  -> END WHILE;
  -> commit;
  -> END $$
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;

#10w
mysql> call insert_user_data(100000);
Query OK, 0 rows affected (6.69 sec)
# ls -lh user.ibd
-rw-r----- 1 mysql mysql 14M Nov 6 10:58 /data2/mysql/test/user.ibd

# python2 py_innodb_page_info.py -v /data2/mysql/test/user.ibd
page offset 00000000, page type <File Space Header>
page offset 00000001, page type <Insert Buffer Bitmap>
page offset 00000002, page type <File Segment inode>
page offset 00000003, page type <B-tree Node>, page level <0001>  #һҶӽڵ㣬ĸ߶ȴ1Ϊ2.
........................................................
page offset 00000000, page type <Freshly Allocated Page>
Total number of page: 896:
Freshly Allocated Page: 493
Insert Buffer Bitmap: 1
File Space Header: 1
B-tree Node: 400
File Segment inode: 1

deleteݺĿռ仯

mysql> select min(id),max(id),count(*) from user;
+---------+---------+----------+
| min(id) | max(id) | count(*) |
+---------+---------+----------+
|    1 | 100000 |  100000 |
+---------+---------+----------+
1 row in set (0.05 sec)
#ɾ50000ݣϿռӦô14MB䳤7MBҡ
mysql> delete from user limit 50000;
Query OK, 50000 rows affected (0.25 sec)

 
#ļСȻ14MBûС
# ls -lh /data2/mysql/test/user1.ibd 
-rw-r----- 1 mysql mysql 14M Nov 6 13:22 /data2/mysql/test/user.ibd

#ҳûбա
# python2 py_innodb_page_info.py -v /data2/mysql/test/user.ibd
page offset 00000000, page type <File Space Header>
page offset 00000001, page type <Insert Buffer Bitmap>
page offset 00000002, page type <File Segment inode>
page offset 00000003, page type <B-tree Node>, page level <0001>
........................................................
page offset 00000000, page type <Freshly Allocated Page>
Total number of page: 896:
Freshly Allocated Page: 493
Insert Buffer Bitmap: 1
File Space Header: 1
B-tree Node: 400
File Segment inode: 1
#MySQLڲDZɾ
mysql> use information_schema;

Database changed
mysql> SELECT A.SPACE AS TBL_SPACEID, A.TABLE_ID, A.NAME AS TABLE_NAME, FILE_FORMAT, ROW_FORMAT, SPACE_TYPE, B.INDEX_ID , B.NAME AS INDEX_NAME, PAGE_NO, B.TYPE AS INDEX_TYPE FROM INNODB_SYS_TABLES A LEFT JOIN INNODB_SYS_INDEXES B ON A.TABLE_ID =B.TABLE_ID WHERE A.NAME = 'test/user1';
+-------------+----------+------------+-------------+------------+------------+----------+------------+---------+------------+
| TBL_SPACEID | TABLE_ID | TABLE_NAME | FILE_FORMAT | ROW_FORMAT | SPACE_TYPE | INDEX_ID | INDEX_NAME | PAGE_NO | INDEX_TYPE |
+-------------+----------+------------+-------------+------------+------------+----------+------------+---------+------------+
|    1283 |   1207 | test/user | Barracuda  | Dynamic  | Single   |   2236 | PRIMARY  |    3 |     3 |
+-------------+----------+------------+-------------+------------+------------+----------+------------+---------+------------+
1 row in set (0.01 sec)

PAGE_NO = 3 ʶB-treeroot page3ҳINDEX_TYPE = 3Ǿۼ INDEX_TYPEȡֵ£
0 = nonunique secondary index; 
1 = automatically generated clustered index (GEN_CLUST_INDEX); 
2 = unique nonclustered index; 
3 = clustered index; 
32 = full-text index;
#ռٺй۲

MySQLڲɾռ䣬ɾdelflag:N޸Ϊdelflag:Ycommit֮ᱻpurgeɾһinsertļ¼delete֮Ŀռ䲻ᱻãļ¼Сڵdeleteļ¼ջᱻãݿ֪ͨõinnblock߽з

InnodbеƬ

ƬIJ

֪ݴ洢ļϵͳϵģDz100%÷ռ䣬ɾݻҳһЩնд루ۼӣᵼҳѣҳѵҳÿռ50%ԱɾĻӦĶֵɾģҲᵼṹеҳһЩ"ն"ȻЩնпܻᱻظãվᵼ²ռδʹãҲƬ

ͬʱΪ100%InnodbҲpageҳ1/16ĿռΪԤʹãAn innodb_fill_factor setting of 100 leaves 1/16 of the space in clustered index pages free for future index growthֹupdate

mysql> select table_schema,
  ->    table_name,ENGINE,
  ->    round(DATA_LENGTH/1024/1024+ INDEX_LENGTH/1024/1024) total_mb,TABLE_ROWS,
  ->    round(DATA_LENGTH/1024/1024) data_mb, round(INDEX_LENGTH/1024/1024) index_mb, round(DATA_FREE/1024/1024) free_mb, round(DATA_FREE/DATA_LENGTH*100,2) free_ratio
  -> from information_schema.TABLES where TABLE_SCHEMA= 'test'
  -> and TABLE_NAME= 'user';
+--------------+------------+--------+----------+------------+---------+----------+---------+------------+
| table_schema | table_name | ENGINE | total_mb | TABLE_ROWS | data_mb | index_mb | free_mb | free_ratio |
+--------------+------------+--------+----------+------------+---------+----------+---------+------------+
| test     | user   | InnoDB |    4 |   50000 |    4 |    0 |    6 |   149.42 |
+--------------+------------+--------+----------+------------+---------+----------+---------+------------+
1 row in set (0.00 sec)

data_freeǷδʹõֽ˵ȫƬռ䡣

ƬĻ

InnoDBıͨƬͷſռ䣬IOȽϺʱҲDMLУͬʱҪռöĴ̿ռ䣬RDS˵ܻᵼ´̿ռ˲䱬ʵ˲䱻Ӧ޷DMLԽֹϻȥִС

#ִInnoDBƬ
mysql> alter table user engine=InnoDB;
Query OK, 0 rows affected (9.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

##ִ֮ļС14MB͵10M
# ls -lh /data2/mysql/test/user1.ibd 
-rw-r----- 1 mysql mysql 10M Nov 6 16:18 /data2/mysql/test/user.ibd

mysql> select table_schema,    table_name,ENGINE,    round(DATA_LENGTH/1024/1024+ INDEX_LENGTH/1024/1024) total_mb,TABLE_ROWS,    round(DATA_LENGTH/1024/1024) data_mb, round(INDEX_LENGTH/1024/1024) index_mb, round(DATA_FREE/1024/1024) free_mb, round(DATA_FREE/DATA_LENGTH*100,2) free_ratio from information_schema.TABLES where TABLE_SCHEMA= 'test' and TABLE_NAME= 'user';
+--------------+------------+--------+----------+------------+---------+----------+---------+------------+
| table_schema | table_name | ENGINE | total_mb | TABLE_ROWS | data_mb | index_mb | free_mb | free_ratio |
+--------------+------------+--------+----------+------------+---------+----------+---------+------------+
| test     | user   | InnoDB |    5 |   50000 |    5 |    0 |    2 |   44.29 |
+--------------+------------+--------+----------+------------+---------+----------+---------+------------+
1 row in set (0.00 sec)

deleteSQLӰ

δɾǰSQLִ

#100W
mysql> call insert_user_data(1000000);
Query OK, 0 rows affected (35.99 sec)

#
mysql> alter table user add index idx_name(name), add index idx_phone(phone);
Query OK, 0 rows affected (6.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

#ͳϢ
mysql> show index from user;
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user |     0 | PRIMARY  |      1 | id     | A     |   996757 |   NULL | NULL  |   | BTREE   |     |        |
| user |     1 | idx_name |      1 | name    | A     |   996757 |   NULL | NULL  |   | BTREE   |     |        |
| user |     1 | idx_phone |      1 | phone    | A     |      2 |   NULL | NULL  |   | BTREE   |     |        |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

#״̬
mysql> flush status;
Query OK, 0 rows affected (0.00 sec)

#ִSQL
mysql> select id, age ,phone from user where name like 'lyn12%';
+--------+-----+-------------+
| id   | age | phone    |
+--------+-----+-------------+
|  124 |  3 | 15240540354 |
|  1231 | 30 | 15240540354 |
| 12301 | 60 | 15240540354 |
.............................
| 129998 | 37 | 15240540354 |
| 129999 | 38 | 15240540354 |
| 130000 | 39 | 15240540354 |
+--------+-----+-------------+
11111 rows in set (0.03 sec)

mysql> explain select id, age ,phone from user where name like 'lyn12%';
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------+
| id | select_type | table | type | possible_keys | key   | key_len | ref | rows | Extra         |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------+
| 1 | SIMPLE   | user | range | idx_name   | idx_name | 82   | NULL | 22226 | Using index condition |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

#鿴״̬ر
mysql> select * from information_schema.session_status where variable_name in('Last_query_cost','Handler_read_next','Innodb_pages_read','Innodb_data_reads','Innodb_pages_read');
+-------------------+----------------+
| VARIABLE_NAME   | VARIABLE_VALUE |
+-------------------+----------------+
| HANDLER_READ_NEXT | 11111     |  #
| INNODB_DATA_READS | 7868409    |  #
| INNODB_PAGES_READ | 7855239    |  #߼
| LAST_QUERY_COST  | 10.499000   |  #SQLijɱCOSTҪIO_COSTCPU_COST
+-------------------+----------------+
4 rows in set (0.00 sec)

ɾSQLִ

#ɾ50w
mysql> delete from user limit 500000;
Query OK, 500000 rows affected (3.70 sec)

#ͳϢ
mysql> analyze table user;
+-----------+---------+----------+----------+
| Table   | Op   | Msg_type | Msg_text |
+-----------+---------+----------+----------+
| test.user | analyze | status  | OK    |
+-----------+---------+----------+----------+
1 row in set (0.01 sec)

#״̬
mysql> flush status;
Query OK, 0 rows affected (0.01 sec)

mysql> select id, age ,phone from user where name like 'lyn12%';
Empty set (0.05 sec)

mysql> explain select id, age ,phone from user where name like 'lyn12%';
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------+
| id | select_type | table | type | possible_keys | key   | key_len | ref | rows | Extra         |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------+
| 1 | SIMPLE   | user | range | idx_name   | idx_name | 82   | NULL | 22226 | Using index condition |
+----+-------------+-------+-------+---------------+----------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)

mysql> select * from information_schema.session_status where variable_name in('Last_query_cost','Handler_read_next','Innodb_pages_read','Innodb_data_reads','Innodb_pages_read');
+-------------------+----------------+
| VARIABLE_NAME   | VARIABLE_VALUE |
+-------------------+----------------+
| HANDLER_READ_NEXT | 0       |
| INNODB_DATA_READS | 7868409    |
| INNODB_PAGES_READ | 7855239    |
| LAST_QUERY_COST  | 10.499000   |
+-------------------+----------------+
4 rows in set (0.00 sec)

ͳƷ

COST ߼ ɨ ִʱ
ʼ100W 10.499000 7868409 7855239 22226 11111 30ms
100Wɾ50W 10.499000 7868409 7855239 22226 0 50ms

Ҳ˵ͨĴҪͨdeleteԱDzʵģκʱҪdeleteȥɾݣӦʹŵıɾ

deleteŻ

ҵ˺Ȩ

һϵͳ˵ҪҵصȥϵͳÿϵͳԿһserviceAPPкܶ񣬺ĵķûuser-servicesearch-serviceƷproduct-serviceλ÷location-service۸price-serviceȡÿӦһݿ⣬Ϊݿⴴ˺ţͬʱֻDMLȨûdeleteȨޣͬʱֹʡ

#ûݿⲢȨ
create database mt_user charset utf8mb4;
grant USAGE, SELECT, INSERT, UPDATE ON mt_user.* to 'w_user'@'%' identified by 't$W*g@gaHTGi123456';
flush privileges;

deleteΪɾ

MySQLݿ⽨ģ淶4ֶΣÿеģͬʱcreate_timeҪĺô

  • һЩѯҵ񳡾һĬϵʱΣ7һ£ͨcreate_timeȥˣɨ졣
  • һЩĵҵҪT +1ķʽȡݲֿУÿ00:30ȡǰһݣͨcreate_time˵ġ
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`is_deleted` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Ƿ߼ɾ0δɾ1ɾ',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'ʱ',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '޸ʱ'

#ɾǣҵӿڵdeleteͿתΪupdate
update user set is_deleted = 1 where user_id = 1213;

#ѯʱҪis_deleted
select id, age ,phone from user where is_deleted = 0 and name like 'lyn12%';

ݹ鵵ʽ

ͨݹ鵵

#1. 鵵һԭ_bak
CREATE TABLE `ota_order_bak` (
 `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '',
 `order_id` varchar(255) DEFAULT NULL COMMENT 'id',
 `ota_id` varchar(255) DEFAULT NULL COMMENT 'ota',
 `check_in_date` varchar(255) DEFAULT NULL COMMENT 'ס',
 `check_out_date` varchar(255) DEFAULT NULL COMMENT '',
 `hotel_id` varchar(255) DEFAULT NULL COMMENT 'ƵID',
 `guest_name` varchar(255) DEFAULT NULL COMMENT '˿',
 `purcharse_time` timestamp NULL DEFAULT NULL COMMENT 'ʱ',
 `create_time` datetime DEFAULT NULL,
 `update_time` datetime DEFAULT NULL,
 `create_user` varchar(255) DEFAULT NULL,
 `update_user` varchar(255) DEFAULT NULL,
 `status` int(4) DEFAULT '1' COMMENT '״̬  1   0 ɾ',
 `hotel_name` varchar(255) DEFAULT NULL,
 `price` decimal(10,0) DEFAULT NULL,
 `remark` longtext,
 PRIMARY KEY (`id`),
 KEY `IDX_order_id` (`order_id`) USING BTREE,
 KEY `hotel_name` (`hotel_name`) USING BTREE,
 KEY `ota_id` (`ota_id`) USING BTREE,
 KEY `IDX_purcharse_time` (`purcharse_time`) USING BTREE,
 KEY `IDX_create_time` (`create_time`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY RANGE (to_days(create_time)) ( 
PARTITION p201808 VALUES LESS THAN (to_days('2018-09-01')), 
PARTITION p201809 VALUES LESS THAN (to_days('2018-10-01')), 
PARTITION p201810 VALUES LESS THAN (to_days('2018-11-01')), 
PARTITION p201811 VALUES LESS THAN (to_days('2018-12-01')), 
PARTITION p201812 VALUES LESS THAN (to_days('2019-01-01')), 
PARTITION p201901 VALUES LESS THAN (to_days('2019-02-01')), 
PARTITION p201902 VALUES LESS THAN (to_days('2019-03-01')), 
PARTITION p201903 VALUES LESS THAN (to_days('2019-04-01')), 
PARTITION p201904 VALUES LESS THAN (to_days('2019-05-01')), 
PARTITION p201905 VALUES LESS THAN (to_days('2019-06-01')), 
PARTITION p201906 VALUES LESS THAN (to_days('2019-07-01')), 
PARTITION p201907 VALUES LESS THAN (to_days('2019-08-01')), 
PARTITION p201908 VALUES LESS THAN (to_days('2019-09-01')), 
PARTITION p201909 VALUES LESS THAN (to_days('2019-10-01')), 
PARTITION p201910 VALUES LESS THAN (to_days('2019-11-01')), 
PARTITION p201911 VALUES LESS THAN (to_days('2019-12-01')), 
PARTITION p201912 VALUES LESS THAN (to_days('2020-01-01')));

#2. ԭЧݣҪͬѧȷݱΧ
create table tbl_p201808 as select * from ota_order where create_time between '2018-08-01 00:00:00' and '2018-08-31 23:59:59';

#3. 鵵
alter table ota_order_bak exchange partition p201808 with table tbl_p201808;

#4. ɾԭѾ淶
delete from ota_order where create_time between '2018-08-01 00:00:00' and '2018-08-31 23:59:59' limit 3000;

ŻĹ鵵ʽ

#1. м
CREATE TABLE `ota_order_2020` (........) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY RANGE (to_days(create_time)) ( 
PARTITION p201808 VALUES LESS THAN (to_days('2018-09-01')), 
PARTITION p201809 VALUES LESS THAN (to_days('2018-10-01')), 
PARTITION p201810 VALUES LESS THAN (to_days('2018-11-01')), 
PARTITION p201811 VALUES LESS THAN (to_days('2018-12-01')), 
PARTITION p201812 VALUES LESS THAN (to_days('2019-01-01')), 
PARTITION p201901 VALUES LESS THAN (to_days('2019-02-01')), 
PARTITION p201902 VALUES LESS THAN (to_days('2019-03-01')), 
PARTITION p201903 VALUES LESS THAN (to_days('2019-04-01')), 
PARTITION p201904 VALUES LESS THAN (to_days('2019-05-01')), 
PARTITION p201905 VALUES LESS THAN (to_days('2019-06-01')), 
PARTITION p201906 VALUES LESS THAN (to_days('2019-07-01')), 
PARTITION p201907 VALUES LESS THAN (to_days('2019-08-01')), 
PARTITION p201908 VALUES LESS THAN (to_days('2019-09-01')), 
PARTITION p201909 VALUES LESS THAN (to_days('2019-10-01')), 
PARTITION p201910 VALUES LESS THAN (to_days('2019-11-01')), 
PARTITION p201911 VALUES LESS THAN (to_days('2019-12-01')), 
PARTITION p201912 VALUES LESS THAN (to_days('2020-01-01')));

#2. ԭЧݣ100WҿҵͷֱӲ룬Ƚϴ󣬽dataXԿƵʺʹС֮ǰGoװdataXʵԶjsonļԶСȥִС
insert into ota_order_2020 select * from ota_order where create_time between '2020-08-01 00:00:00' and '2020-08-31 23:59:59';

#3. 
alter table ota_order rename to ota_order_bak; 
alter table ota_order_2020 rename to ota_order;
#4. 
insert into ota_order select * from ota_order_bak a where not exists (select 1 from ota_order b where a.id = b.id);
#5. ota_order_bakɷȽϴ󲻽ֱӸ죬ȴ÷ͨdataXѵȥɡ

#6. Ĺ鵵
#мձ
create table ota_order_mid like ota_order;
#ԭЧݷͨ
alter table ota_order exchange partition p201808 with table ota_order_mid; 
##ͨݵ鵵Ӧ
alter table ota_order_bak exchange partition p201808 with table ota_order_mid;

ԭ͹鵵ǰµķֻҪһмͨҵͷηȿɾЧݣܻտգûпռƬӰϵSQLִмƻ

ܽ

ͨInnoDB洢ռֲdeleteܵӰԿdeleteɾȲͷŴ̿ռ䣬һƬƵѣӰSQLִмƻȶԣ

ͬʱƬʱôCPU̿ռ䣬ӰDML

ҵ棬Ӧ߼ɾɾΪʵݹ鵵󣬿òMySQLʵ֣DDLûƬ

һȽϺõķClickhouseڵݱʹClickhouse洢TTLʵЧԶ

ƪdz̸ΪʲôMySQLdeleteɾݵ¾ͽܵ,MySQLdeleteɾű֮ǰ»ϣԺֽ֧ű֮ң