加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL分区与传统的分库分表

发布时间:2017-02-01 04:45:33 所属栏目:MySql教程 来源:站长网
导读:副标题#e# 传统的分库分表 传统的分库分表都是通过应用层逻辑实现的,对于数据库层面来说,都是普通的表和库。 分库 分库的原因 首先,在单台数据库服务器性能足够的情况下,分库对于数据库性能是没有影响的。在数据库存储上,database只起到一个namespace

则下面的查询没有利用分区,因为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

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读