mysql8.0 分區表查詢性能調查
測試環境
- vmware CentOS Linux release 7.8.2003 (Core) 64位
- 2處理器,2G
- mysq l8.0.23
創建表
14個分區,每個分區400W數據,共5600W數據。
`id` BIGINT NOT NULL AUTO_INCREMENT,
hiredate DATETIME,
user_name varhcar(64),
PRIMARY KEY (`id`,hiredate)
)
PARTITION BY RANGE (TO_DAYS(hiredate) ) (
PARTITION p1 VALUES LESS THAN ( TO_DAYS('20200101') ),
PARTITION p2 VALUES LESS THAN ( TO_DAYS('20200201') ),
PARTITION p3 VALUES LESS THAN ( TO_DAYS('20200301') ),
PARTITION p4 VALUES LESS THAN ( TO_DAYS('20200401') ),
PARTITION p5 VALUES LESS THAN ( TO_DAYS('20200501') ),
PARTITION p6 VALUES LESS THAN ( TO_DAYS('20200601') ),
PARTITION p7 VALUES LESS THAN ( TO_DAYS('20200701') ),
PARTITION p8 VALUES LESS THAN ( TO_DAYS('20200801') ),
PARTITION p9 VALUES LESS THAN ( TO_DAYS('20200901') ),
PARTITION p10 VALUES LESS THAN ( TO_DAYS('20201001') ),
PARTITION p11 VALUES LESS THAN ( TO_DAYS('20201101') ),
PARTITION p12 VALUES LESS THAN ( TO_DAYS('20201201') ),
PARTITION p13 VALUES LESS THAN ( TO_DAYS('20210101') ),
PARTITION p14 VALUES LESS THAN ( TO_DAYS('20210201') )
);
數據初始化
CREATE DEFINER=`root`@`%` PROCEDURE `test_insert`(IN `hiredate` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE y INT DEFAULT 1;
WHILE y<40000
DO
INSERT INTO bdm_range_datetime(`hiredate`,`user_name`) VALUES
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),
(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1),(hiredate,LAST_INSERT_ID()+1);
SET y=y+1;
END WHILE ;
commit;
END
調用:
call test_insert(20191231);
call test_insert(20200101);
call test_insert(20200201);
call test_insert(20200301);
call test_insert(20200401);
call test_insert(20200501);
call test_insert(20200601);
call test_insert(20200701);
call test_insert(20200801);
call test_insert(20200901);
call test_insert(20201001);
call test_insert(20201101);
call test_insert(20201201);
call test_insert(20210101);
執行情況
1個分區執行時間
select count(*) from bdm_range_datetime where hiredate='2019-12-31' ;
/* Affected rows: 0 已找到記錄: 1 警告: 0 持續時間 1 query: 0.734 sec. */
2個分區執行時間
select count(*) from bdm_range_datetime where hiredate='2019-12-31' or hiredate='2020-01-02' ;
/* Affected rows: 0 已找到記錄: 1 警告: 0 持續時間 1 query: 1.516 sec. */
跨分區數量 | 執行時間(S) |
---|---|
1 | 0.7 |
2 | 1.5 |
3 | 2.7 |
4 | 3.8 |
5 | 5.0 |
6 | 6.4 |
7 | 7.8 |
8 | 9.5 |
9 | 11.3 |
10 | 13.2 |
11 | 15 |
12 | 17.3 |
13 | 19 |
14 | 21.6 |
分片鍵查詢
帶分片鍵
select * from bdm_range_datetime where hiredate='2021-01-04' and user_name='56003838'
/* Affected rows: 0 已找到記錄: 1 警告: 0 持續時間 1 query: 1.218 sec. */
跨7個分區
select * from bdm_range_datetime where (hiredate<'2021-01-08' and hiredate>'2020-08-04') and user_name='56003838';
/* Affected rows: 0 已找到記錄: 1 警告: 0 持續時間 1 query: 9.578 sec. */
不待分片鍵
select * from bdm_range_datetime where user_name='56003838';
/* Affected rows: 0 已找到記錄: 1 警告: 0 持續時間 1 query: 26.500 sec. */
差26倍。