MySQL分区与传统的分库分表
|
则下面的查询没有利用分区,因为partitions中包含了所有的分区:
mysql> explain partitions select * from salaries where salary > 100000G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: salaries
partitions: p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 2835486
Extra: Using where
只有在where条件中加入分区列才能起到作用,过滤掉不需要的分区:
mysql> explain partitions select * from salaries where salary > 100000 and from_date > '1998-01-01'G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: salaries
partitions: p15,p16,p17,p18
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1152556
Extra: Using where
与普通搜索一样,在运算符左侧使用函数将使分区过滤失效,即使与分区函数想同也一样:
mysql> explain partitions select * from salaries where salary > 100000 and year(from_date) > 1998G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: salaries
partitions: p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 2835486
Extra: Using where
分区和分表的比较传统分表后,count、sum等统计操作只能对所有切分表进行操作后之后在应用层再次计算得出最后统计数据。而分区表则不受影响,可直接统计。 Queries involving aggregate functions such as SUM() and COUNT() can easily be parallelized. A simple example of such a query might be SELECT salesperson_id, COUNT(orders) as order_total FROM sales GROUP BY salesperson_id;. By “parallelized,” we mean that the query can be run simultaneously on each partition, and the final result obtained merely by summing the results obtained for all partitions. 分区对原系统改动最小,分区只涉及数据库层面,应用层不需要做出改动。 分区有个限制是主表的所有唯一字段(包括主键)必须包含分区字段,而分表没有这个限制。 分表包括垂直切分和水平切分,而分区只能起到水平切分的作用。 转自:http://www.jianshu.com/p/89311703b320 (编辑:邯郸站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |


