标签:
拿来主义,MySql 5.6 手册:http://dev.mysql.com/doc/refman/5.6/en/partitioning.html
MySql 支持Range,List,Hash,Key。最长用的是Range
Range:范围 CREATE TABLE employees ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30), hired DATE NOT NULL DEFAULT ‘1970-01-01‘, separated DATE NOT NULL DEFAULT ‘9999-12-31‘, job_code INT NOT NULL, store_id INT NOT NULL ) PARTITION BY RANGE (store_id) ( PARTITION p0 VALUES LESS THAN (6), PARTITION p1 VALUES LESS THAN (11), PARTITION p2 VALUES LESS THAN (16), PARTITION p3 VALUES LESS THAN (21) );
LIST:列表 CREATE TABLE employees ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30), hired DATE NOT NULL DEFAULT ‘1970-01-01‘, separated DATE NOT NULL DEFAULT ‘9999-12-31‘, job_code INT, store_id INT ) PARTITION BY LIST(store_id) ( PARTITION pNorth VALUES IN (3,5,6,9,17), PARTITION pEast VALUES IN (1,2,10,11,19,20), PARTITION pWest VALUES IN (4,12,13,14,18), PARTITION pCentral VALUES IN (7,8,15,16) );
Key:键 CREATE TABLE k1 ( id INT NOT NULL, name VARCHAR(20), UNIQUE KEY (id) ) PARTITION BY KEY() PARTITIONS 2;
HASH:哈希 CREATE TABLE employees ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30), hired DATE NOT NULL DEFAULT ‘1970-01-01‘, separated DATE NOT NULL DEFAULT ‘9999-12-31‘, job_code INT, store_id INT ) PARTITION BY HASH( YEAR(hired) ) PARTITIONS 4;
数据:新闻表,2010开始记录,假设10年到15年每年的数据为200W,总数1000W;
条件:查询15年7月所有的新闻数据;
未分区:需要把表遍历,1000W条数据,查询性能就不用说了;
分区:按照年份分区,当要查询15年数据,只会遍历15年的数据200W条;
标签:
原文地址:http://my.oschina.net/zhanyu/blog/479400