mysql分區(qū)表測試

mysql分區(qū)表測試


mysql部署情況

使用docker-compose在10.xx.xx.1機(jī)器進(jìn)行部署, 端口3307為master骡湖,3308為slave實(shí)例, master+slave ,以下的寫入操作在master進(jìn)行,讀取操作在slave中進(jìn)行

表結(jié)構(gòu)

使用范圍進(jìn)行分區(qū)旺订,時(shí)間戳一天一張表.

CREATE TABLE login_user (
id INT,
user_name VARCHAR(20),
create_date TIMESTAMP,
UNIQUE KEY (id,create_date)
)
PARTITION BY RANGE ( UNIX_TIMESTAMP(create_date) ) (
PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-02 00:00:00') ),
PARTITION p2 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-03 00:00:00') ),
PARTITION p3 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-04 00:00:00') ),
PARTITION p4 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-05 00:00:00') ),
PARTITION p5 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-06 00:00:00') ),
PARTITION p6 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-07 00:00:00') ),
PARTITION p7 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-08 00:00:00') ),
PARTITION p8 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-09 00:00:00') ),
PARTITION p9 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-10 00:00:00') ),
PARTITION p10 VALUES LESS THAN (UNIX_TIMESTAMP('2018-10-11 00:00:00') )
);

分區(qū)情況

mysql root@localhost:my_database> select partition_name part,partition_expression expr,partition_description descr,table_rows from information_schema.partitions where table_schema = schema() and table_name='log
in_user';
+------+------------------------------+------------+------------+
| part | expr | descr | table_rows |
+------+------------------------------+------------+------------+
| p1 | UNIX_TIMESTAMP(create_date) | 1538438400 | 0 |
| p2 | UNIX_TIMESTAMP(create_date) | 1538524800 | 0 |
| p3 | UNIX_TIMESTAMP(create_date) | 1538611200 | 0 |
| p4 | UNIX_TIMESTAMP(create_date) | 1538697600 | 0 |
| p5 | UNIX_TIMESTAMP(create_date) | 1538784000 | 0 |
| p6 | UNIX_TIMESTAMP(create_date) | 1538870400 | 0 |
| p7 | UNIX_TIMESTAMP(create_date) | 1538956800 | 0 |
| p8 | UNIX_TIMESTAMP(create_date) | 1539043200 | 0 |
| p9 | UNIX_TIMESTAMP(create_date) | 1539129600 | 0 |
| p10 | UNIX_TIMESTAMP(create_date) | 1539216000 | 0 |
+------+------------------------------+------------+------------+
10 rows in set
Time: 0.015s

文件存儲(chǔ)相關(guān)

多文件存儲(chǔ)在IO層面減少多線程訪問競態(tài)條件.

I have no name!@4b8b48b630f3:/bitnami/mysql/data/my_database$ ls -alh
total 1.2M
drwxr-x--- 2 1001 root 4.0K Nov 7 04:03 .
drwxrwxr-x 6 root root 4.0K Nov 7 02:48 ..
-rw-r----- 1 1001 root 61 Nov 7 02:47 db.opt
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p1.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p10.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p2.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p3.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p4.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p5.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p6.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p7.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:16 login_user#P#p8.ibd
-rw-r----- 1 1001 root 112K Nov 7 04:03 login_user#P#p9.ibd
-rw-r----- 1 1001 root 8.5K Nov 7 04:02 login_user.frm

寫入測試數(shù)據(jù)

insert into login_user(id,user_name,create_date) values (1,'test','2018-10-01 01:01:03');
insert into login_user(id,user_name,create_date) values (2,'test','2018-10-02 01:02:03');
insert into login_user(id,user_name,create_date) values (3,'test','2018-10-03 01:03:03');
insert into login_user(id,user_name,create_date) values (4,'test','2018-10-04 01:04:03');
insert into login_user(id,user_name,create_date) values (5,'test','2018-10-05 01:05:03');
insert into login_user(id,user_name,create_date) values (6,'test','2018-10-06 01:06:03');
insert into login_user(id,user_name,create_date) values (7,'test','2018-10-07 01:07:03');
insert into login_user(id,user_name,create_date) values (8,'test','2018-10-08 01:08:03');
insert into login_user(id,user_name,create_date) values (9,'test','2018-10-08 01:09:03');
insert into login_user(id,user_name,create_date) values (10,'test','2018-10-10 01:10:03');

insert into login_user(id,user_name,create_date) values (11,'test','2018-10-01 02:01:03');
insert into login_user(id,user_name,create_date) values (12,'test','2018-10-02 02:02:03');
insert into login_user(id,user_name,create_date) values (13,'test','2018-10-03 02:03:03');
insert into login_user(id,user_name,create_date) values (14,'test','2018-10-04 02:04:03');
insert into login_user(id,user_name,create_date) values (15,'test','2018-10-05 02:05:03');
insert into login_user(id,user_name,create_date) values (16,'test','2018-10-06 02:06:03');
insert into login_user(id,user_name,create_date) values (17,'test','2018-10-07 02:07:03');
insert into login_user(id,user_name,create_date) values (18,'test','2018-10-08 02:08:03');
insert into login_user(id,user_name,create_date) values (19,'test','2018-10-08 02:09:03');
insert into login_user(id,user_name,create_date) values (20,'test','2018-10-10 02:10:03');

insert into login_user(id,user_name,create_date) values (21,'test','2018-10-01 03:01:03');
insert into login_user(id,user_name,create_date) values (22,'test','2018-10-02 03:02:03');
insert into login_user(id,user_name,create_date) values (23,'test','2018-10-03 03:03:03');
insert into login_user(id,user_name,create_date) values (24,'test','2018-10-04 03:04:03');
insert into login_user(id,user_name,create_date) values (25,'test','2018-10-05 03:05:03');
insert into login_user(id,user_name,create_date) values (26,'test','2018-10-06 03:06:03');
insert into login_user(id,user_name,create_date) values (27,'test','2018-10-07 03:07:03');
insert into login_user(id,user_name,create_date) values (28,'test','2018-10-08 03:08:03');
insert into login_user(id,user_name,create_date) values (29,'test','2018-10-08 03:09:03');
insert into login_user(id,user_name,create_date) values (30,'test','2018-10-10 03:10:03');

使用非分區(qū)字段查詢

全分區(qū)掃描

mysql root@localhost:my_database> explain select * from login_user where id = 1 ;
+----+-------------+------------+--------------------------------+------+---------------+-----+---------+-------+------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+--------------------------------+------+---------------+-----+---------+-------+------+----------+--------+
| 1 | SIMPLE | login_user | p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 | ref | id | id | 5 | const | 1 | 100.0 | <null> |
+----+-------------+------------+--------------------------------+------+---------------+-----+---------+-------+------+----------+--------+
1 row in set
Time: 0.024s

使用分區(qū)字段進(jìn)行查詢

直接讀取p1

mysql root@localhost:my_database> explain select * from login_user where create_date = '2018-10-01 01:01:03';
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
| 1 | SIMPLE | login_user | p1 | ALL | <null> | <null> | <null> | <null> | 3 | 33.33 | Using where |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
1 row in set
Time: 0.020s

分區(qū)字段進(jìn)行區(qū)間查詢1

讀取p1,p2,p3,p4,p5,p6,p7,p8 , 控制查詢范圍很重要肚逸,盡可能減少區(qū)間

mysql root@localhost:my_database> explain select * from login_user where create_date between '2018-10-01 01:01:03' and '2018-10-08 01:08:03' ;
+----+-------------+------------+-------------------------+------+---------------+--------+---------+--------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+-------------------------+------+---------------+--------+---------+--------+------+----------+-------------+
| 1 | SIMPLE | login_user | p1,p2,p3,p4,p5,p6,p7,p8 | ALL | <null> | <null> | <null> | <null> | 27 | 11.11 | Using where |
+----+-------------+------------+-------------------------+------+---------------+--------+---------+--------+------+----------+-------------+
1 row in set
Time: 0.019s

分區(qū)字段進(jìn)行分區(qū)查詢2

讀取p1,p2,p3 ,不影響其他分區(qū)

mysql root@localhost:my_database> explain select * from login_user where create_date between '2018-10-01 01:01:03' and '2018-10-03 01:08:03' ;
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
| 1 | SIMPLE | login_user | p1,p2,p3 | ALL | <null> | <null> | <null> | <null> | 9 | 11.11 | Using where |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
1 row in set
Time: 0.018s

分區(qū)字段進(jìn)行分組統(tǒng)計(jì)

讀取p1,p2,p3 ,不影響其他分區(qū)

mysql root@localhost:my_database> explain select id , count(1) from login_user where create_date between '2018-10-01 01:01:03' and '2018-10-03 01:08:03' group by id ;

+----+-------------+------------+------------+-------+---------------+-----+---------+--------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+---------------+-----+---------+--------+------+----------+--------------------------+
| 1 | SIMPLE | login_user | p1,p2,p3 | index | id | id | 10 | <null> | 9 | 11.11 | Using where; Using index |
+----+-------------+------------+------------+-------+---------------+-----+---------+--------+------+----------+--------------------------+
1 row in set
Time: 0.021s

使用分區(qū)字段進(jìn)行小于查詢

直接讀取p1

mysql root@localhost:my_database> explain delete from login_user where create_date < '2018-10-02 00:00:00'
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
| 1 | DELETE | login_user | p1 | ALL | <null> | <null> | <null> | <null> | 3 | 100.0 | Using where |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+------+----------+-------------+
1 row in set
Time: 0.023s

插入測試

直接插入對應(yīng)的分區(qū)表

mysql root@localhost:my_database> explain insert into login_user(id,user_name,create_date) values (5,'test','2018-10-05 01:05:03');
+----+-------------+------------+------------+------+---------------+--------+---------+--------+--------+----------+--------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+--------+----------+--------+
| 1 | INSERT | login_user | p5 | ALL | <null> | <null> | <null> | <null> | <null> | <null> | <null> |
+----+-------------+------------+------------+------+---------------+--------+---------+--------+--------+----------+--------+
1 row in set
Time: 0.018s

分區(qū)表擴(kuò)容及移動(dòng)方案.

因?yàn)榉謪^(qū)表有數(shù)量限制(1024),當(dāng)數(shù)據(jù)接近1024份時(shí)认境,需要進(jìn)行老數(shù)據(jù)的歸檔(將老數(shù)據(jù)遷移出分區(qū)表), 并且建立新的分區(qū)用來存放接下來的數(shù)據(jù)范圍.

ALTER TABLE login_user PARTITION BY RANGE (UNIX_TIMESTAMP(create_date))
(
PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-02 00:00:00') ),
PARTITION p2 VALUES LESS THAN ( UNIX_TIMESTAMP('2018-10-03 00:00:00') )
);

分區(qū)表DBA相關(guān)

LVM磁盤動(dòng)態(tài)擴(kuò)容

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市课竣,隨后出現(xiàn)的幾起案子嘉赎,更是在濱河造成了極大的恐慌,老刑警劉巖于樟,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件公条,死亡現(xiàn)場離奇詭異,居然都是意外死亡迂曲,警方通過查閱死者的電腦和手機(jī)靶橱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來路捧,“玉大人关霸,你說我怎么就攤上這事〗苌ǎ” “怎么了队寇?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長章姓。 經(jīng)常有香客問我佳遣,道長炭序,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任苍日,我火速辦了婚禮惭聂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘相恃。我一直安慰自己辜纲,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布拦耐。 她就那樣靜靜地躺著耕腾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪杀糯。 梳的紋絲不亂的頭發(fā)上扫俺,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機(jī)與錄音固翰,去河邊找鬼狼纬。 笑死,一個(gè)胖子當(dāng)著我的面吹牛骂际,可吹牛的內(nèi)容都是我干的疗琉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼歉铝,長吁一口氣:“原來是場噩夢啊……” “哼盈简!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起太示,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤柠贤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后类缤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體臼勉,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年呀非,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坚俗。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片镜盯。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡岸裙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出速缆,到底是詐尸還是另有隱情降允,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布艺糜,位于F島的核電站剧董,受9級特大地震影響幢尚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜翅楼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一尉剩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧毅臊,春花似錦理茎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蚯撩,卻和暖如春础倍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背胎挎。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工沟启, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人犹菇。 一個(gè)月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓美浦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親项栏。 傳聞我的和親對象是個(gè)殘疾皇子浦辨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355