| 1 | = Partition = |
| 2 | |
| 3 | 1. mysql restore sample data (mysqlsampledatabase.sql) from [https://www.mysqltutorial.org/mysql-sample-database.aspx here] |
| 4 | {{{ |
| 5 | rpl_user@node1:/mysql/test/T3$ /mysql/bin/mysql -uroot --protocol=socket --socket=/tmp/mysql.0.sock < mysqlsampledatabase.sql |
| 6 | }}} |
| 7 | |
| 8 | 2. create table payments2. primary key MUST include in PARTITION |
| 9 | {{{ |
| 10 | mysql> CREATE TABLE payments2 ( |
| 11 | customerNumber int(11) NOT NULL, |
| 12 | checkNumber varchar(50) NOT NULL, |
| 13 | paymentDate datetime NOT NULL, |
| 14 | amount decimal(10,2) NOT NULL, |
| 15 | PRIMARY KEY (customerNumber,paymentDate ) ) PARTITION BY RANGE( TO_DAYS(paymentDate) ) |
| 16 | ( PARTITION p2003 VALUES LESS THAN (TO_DAYS('2004-01-01')), |
| 17 | PARTITION p2004 VALUES LESS THAN (TO_DAYS('2005-01-01')), |
| 18 | PARTITION p2005 VALUES LESS THAN (TO_DAYS('2006-01-01')) ); |
| 19 | }}} |
| 20 | |
| 21 | 3. copy payments table to just create payments2 |
| 22 | {{{ |
| 23 | mysql> insert into payments2 select * from payments |
| 24 | }}} |
| 25 | |
| 26 | 3. test and notice the '''rows''' between table payments (273) vs payments2 (37) |
| 27 | {{{ |
| 28 | mysql> explain select * from payments2 where paymentDate = "2005-05-03"; |
| 29 | +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+ |
| 30 | | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | |
| 31 | +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+ |
| 32 | | 1 | SIMPLE | payments3 | p2005 | ALL | NULL | NULL | NULL | NULL | 37 | 10.00 | Using where | |
| 33 | +----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+ |
| 34 | 1 row in set, 1 warning (0.00 sec) |
| 35 | |
| 36 | mysql> explain select * from payments where paymentDate = "2005-05-03"; |
| 37 | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ |
| 38 | | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | |
| 39 | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ |
| 40 | | 1 | SIMPLE | payments | NULL | ALL | NULL | NULL | NULL | NULL | 273 | 10.00 | Using where | |
| 41 | +----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+ |
| 42 | 1 row in set, 1 warning (0.00 sec) |
| 43 | |
| 44 | }}} |