Home | Mirror | Search

12. Sharding

Sharding是近幾年提出的概念,可以做分表,分庫切割,通過hash值定位。但都存在一個問題,數據連續性,索引無法跨表。

Oracle 在8.x中就支持分區功能,MySQL在5.1.x中也是閒類似功能,PostgreSQL 因存儲結構設計的較好,基本不需要做分區。

12.1. horizontal

ALTER TABLE `goods`  DROP INDEX `goods_sn_2`;
ALTER TABLE goods PARTITION BY RANGE (goods_id) (
    PARTITION p0 VALUES LESS THAN (10000),
    PARTITION p1 VALUES LESS THAN (20000),
    PARTITION p2 VALUES LESS THAN (30000),
    PARTITION p3 VALUES LESS THAN (40000),
    PARTITION p4 VALUES LESS THAN MAXVALUE
);

ALTER TABLE goods PARTITION BY HASH(goods_id) PARTITIONS 10;

ALTER TABLE goods  PARTITION BY KEY (is_on_sale) PARTITIONS 2;

ALTER TABLE goods PARTITION BY HASH(YEAR(FROM_UNIXTIME(add_time))) PARTITIONS 4;
		

12.2. vertical

comments powered by Disqus