mysql 分区小案例_神奇的MySQL分区-测试案例

==先看结果==

有两个结构一样的表,no_part_tab 和 part_tab,一个使用了分区,一个不使用,结果如下。

mysql> select * from no_part_tab where c1 = '80000';

+-------+--------------------+------------+

| c1    | c2                 | c3         |

+-------+--------------------+------------+

| 80000 | testing partitions | 1995-05-25 |

+-------+--------------------+------------+

1 row in set (1.57 sec)

mysql> select * from part_tab where c1 = '80000';

+-------+--------------------+------------+

| c1    | c2                 | c3         |

+-------+--------------------+------------+

| 80000 | testing partitions | 1995-05-25 |

+-------+--------------------+------------+

1 row in set (0.02 sec)

mysql> update part_tab set c2='zhmsong' where c1 = '80000';

Query OK, 1 row affected (0.21 sec)

Rows matched: 1  Changed: 1  Warnings: 0

mysql> update no_part_tab set c2='zhmsong' where c1 = '80000';

Query OK, 1 row affected (15.55 sec)

Rows matched: 1  Changed: 1  Warnings: 0

mysql> delete from no_part_tab where c1 = '80000';

Query OK, 1 row affected (1.46 sec)

mysql> delete from part_tab where c1 = '80000';

Query OK, 1 row affected (0.02 sec)

mysql> select count(*) from no_part_tab;

+----------+

| count(*) |

+----------+

|  7999999 |

+----------+

1 row in set (0.00 sec)

mysql> select count(*) from part_tab;

+----------+

| count(*) |

+----------+

|  7999999 |

+----------+

1 row in set (0.01 sec)

mysql> select count(*) from part_tab where c1 >= '80000' and c1 <= '150000';

+----------+

| count(*) |

+----------+

|    70000 |

+----------+

1 row in set (0.05 sec)

mysql> select count(*) from no_part_tab where c1 >= '80000' and c1 <= '150000';

+----------+

| count(*) |

+----------+

|    70000 |

+----------+

1 row in set (1.89 sec)

mysql> select count(*) from no_part_tab where c2 like '%test%';

+----------+

| count(*) |

+----------+

|  7999999 |

+----------+

1 row in set (1.52 sec)

mysql> select count(*) from part_tab where c2 like '%test%';

+----------+

| count(*) |

+----------+

|  7999999 |

+----------+

1 row in set (1.59 sec)

mysql> select * from no_part_tab where c2 like '%zhmsong%' limit 10 offset 100;

+---------+---------+------------+

| c1      | c2      | c3         |

+---------+---------+------------+

| 1100100 | zhmsong | 2004-05-13 |

| 1100101 | zhmsong | 1999-05-21 |

| 1100102 | zhmsong | 2004-05-26 |

| 1100103 | zhmsong | 1999-06-03 |

| 1100104 | zhmsong | 2004-06-09 |

| 1100105 | zhmsong | 1999-06-16 |

| 1100106 | zhmsong | 2004-06-22 |

| 1100107 | zhmsong | 1999-06-30 |

| 1100108 | zhmsong | 2004-07-05 |

| 1100109 | zhmsong | 1999-07-13 |

+---------+---------+------------+

10 rows in set (0.22 sec)

mysql> select * from part_tab where c2 like '%zhmsong%' limit 10 offset 100;

+---------+---------+------------+

| c1      | c2      | c3         |

+---------+---------+------------+

| 1100100 | zhmsong | 2004-05-13 |

| 1100101 | zhmsong | 1999-05-21 |

| 1100102 | zhmsong | 2004-05-26 |

| 1100103 | zhmsong | 1999-06-03 |

| 1100104 | zhmsong | 2004-06-09 |

| 1100105 | zhmsong | 1999-06-16 |

| 1100106 | zhmsong | 2004-06-22 |

| 1100107 | zhmsong | 1999-06-30 |

| 1100108 | zhmsong | 2004-07-05 |

| 1100109 | zhmsong | 1999-07-13 |

+---------+---------+------------+

10 rows in set (0.22 sec)

== 实现==

DROP TABLE IF EXISTS part_tab;

CREATE TABLE part_tab (

c1 int default NULL,

c2 varchar(30) default NULL,

c3 date default NULL

) engine=myisam

PARTITION BY RANGE (c1) (

PARTITION p1 VALUES LESS THAN (100000),

PARTITION p2 VALUES LESS THAN (200000),

PARTITION p3 VALUES LESS THAN (300000),

PARTITION p4 VALUES LESS THAN (400000),

PARTITION p5 VALUES LESS THAN (500000),

PARTITION p6 VALUES LESS THAN (600000),

PARTITION p7 VALUES LESS THAN (700000),

PARTITION p8 VALUES LESS THAN (800000),

PARTITION p9 VALUES LESS THAN (900000),

PARTITION p10 VALUES LESS THAN (1000000),

PARTITION p11 VALUES LESS THAN (1100000),

PARTITION p12 VALUES LESS THAN (1200000),

PARTITION p13 VALUES LESS THAN (1300000),

PARTITION p14 VALUES LESS THAN (1400000),

PARTITION p15 VALUES LESS THAN (1500000),

PARTITION p16 VALUES LESS THAN (1600000),

PARTITION p17 VALUES LESS THAN (1700000),

PARTITION p18 VALUES LESS THAN (1800000),

PARTITION p19 VALUES LESS THAN (1900000),

PARTITION p20 VALUES LESS THAN (2000000),

PARTITION p21 VALUES LESS THAN (2100000),

PARTITION p22 VALUES LESS THAN (2200000),

PARTITION p23 VALUES LESS THAN (2300000),

PARTITION p24 VALUES LESS THAN (2400000),

PARTITION p25 VALUES LESS THAN (2500000),

PARTITION p26 VALUES LESS THAN (2600000),

PARTITION p27 VALUES LESS THAN (2700000),

PARTITION p28 VALUES LESS THAN (2800000),

PARTITION p29 VALUES LESS THAN (2900000),

PARTITION p30 VALUES LESS THAN (3000000),

PARTITION p31 VALUES LESS THAN MAXVALUE

);

DROP TABLE IF EXISTS no_part_tab;

create table no_part_tab (

c1 int(11) default NULL,

c2 varchar(30) default NULL,

c3 date default NULL

) engine=myisam;

DROP PROCEDURE IF EXISTS load_part_tab;

delimiter //

create PROCEDURE load_part_tab()

begin

declare v int default 0;

while v < 8000000

do

insert into part_tab values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));

set v = v + 1;

end while;

end

//

delimiter ;

===存在问题===

分区使用的列必须是主键,且UNIQUE键必须进入分区列,这个使分区受到很大限制,特别是 联合主键不解决问题,必须使用UNIQUE键的情况。

你可能感兴趣的:(mysql,分区小案例)