为毛要分表和分区,,,,
所有数据库的通病,文件越大,性能越低...那问题就来了.数据越多文件越大...无解?哎,所以说知道 为毛要分区了吧!
那分表又是毛线?
分表就是把一张表拆分成若干表,,,根据情况常见2种方式,一种是横向(水平分表),不断复制完全一样的表,一种是纵向(垂直分表) 按列拆分成若干个表. 
那分区又是毛线?
说白了就是mysql帮咱们分表,储存到不同的位置(自定义)
别扯了,说重点吧!
好.
分表
水平分表 
完全相同的数据结构来复制表,
比如 
user1 user2 user3 都有相同的数据结构 . id, username 假定每个表只存10万条数据. 那么 10万01就会存到user2中,  17万8000 也应该在第二个表中, 公式是 该条数据 % 10万(上限) 就可以得到表了 
执行查询也是先计算该数据存在哪个表中. 缺点自然能看的出来. 搜索三个表的数据时比较 麻烦.这时可以对某些字段单独建一个关联表. 
垂直分表
还是以user为例 id username password  10亿条数据可能过大. 那怎么办呢. 
建 user,userpass 两个表.字段分别是 id,username   id,password  一一关联. 即可.  缺点.如果使用 where username and password时就无法使用了.    所以 这种分表方式必须根据条件来判断是否可取. 
分区
分区有4种方式分别是  hash   key    list  range

先说hash

[SQL] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
drop table if exists test1;
CREATE TABLE test1 (
    id INT NOT NULL primary key  auto_increment, -- 自动递增
    username varchar(5) not null -- 用户名
)
ENGINE=innodb
PARTITION BY HASH(id) -- 以id(必须是主键才可以)分区,共分4个区.
PARTITIONS 4 ( -- 分别是
        partition p0  -- p0   数据位置和索引位置分别如下
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p1
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p2
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p3
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test'
) ;
insert test1 (username) values
('test1'),-- 储存在p0分区
('test2'),-- 储存在p1分区
('test3'),-- p2
('test4'),-- p3
('test5');-- po分区

如果出现 Error : Got error -1 from storage engine 说明没权限 

/data/wwwroot/test/test   注意这里多出一个test

total 768
drwxrwx---  6 _mysql        wheel    204  4 13 11:27 .
drwxrwxrwx  3 xxxxxxxx   wheel    102  4 13 11:27 ..
-rw-rw----  1 _mysql        wheel  98304  4 13 11:27 test1#P#p0.ibd
-rw-rw----  1 _mysql        wheel  98304  4 13 11:27 test1#P#p1.ibd
-rw-rw----  1 _mysql        wheel  98304  4 13 11:27 test1#P#p2.ibd
-rw-rw----  1 _mysql        wheel  98304  4 13 11:27 test1#P#p3.ibd

如果换成myisam 则是这样
/data/wwwroot/test  注意这里是设置的test路径  里面的test内容 就是上面的内容  其它就是myisam的分区文件 内容了

test                        test1#P#p1.MYD        test1#P#p2.MYI
test1#P#p0.MYD        test1#P#p1.MYI        test1#P#p3.MYD
test1#P#p0.MYI        test1#P#p2.MYD        test1#P#p3.MYI

接下来就是range

[SQL] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
drop table if exists test1;
CREATE TABLE test1 (
    id INT NOT NULL primary key  auto_increment, -- 自动递增
    username varchar(5) not null -- 用户名
)
ENGINE=innodb
PARTITION BY RANGE(id) -- 以id(必须是主键才可以)分区,共分4个区.
PARTITIONS 4 ( -- 分别是
        partition p0 VALUES LESS THAN (3)  -- 小于3的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p1 values less than (5)  -- 小于5的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p2 values less than (10) -- 小于10的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test' ,
        partition p3 values less than MAXVALUE -- >10 其它的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test'
) ;
insert test1 (username) values
('test1'),-- 1 储存在p0分区
('test2'),-- 2 p0
('test3'),-- 3 p1
('test4'),-- 4 p1
('test5'),-- 5 p2
('test6');-- 6 p2

再然后就是list了

[SQL] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
drop table if exists test1;
CREATE TABLE test1 (
    id INT NOT NULL primary key  auto_increment, -- 自动递增
    username varchar(5) not null -- 用户名
)
ENGINE=innodb
PARTITION BY list(id) -- 以id(必须是主键才可以)分区,共分4个区.
PARTITIONS 2 ( -- 分别是
        partition p0 VALUES in(1,2,3,4,5)  -- id mod 10
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p1 values in(6,7,8,9,0)  -- id mod 10
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test'
) ;
insert test1 (username) values
('test1'),-- 1 储存在p0分区
('test2'),-- 2 p0
('test3'),-- 3 p0
('test4'),-- 4 p0
('test5'),-- 5 p0
('test6');-- 6 p1

最后就是key了. 

理解key跟hash差不多就可以了,只不过Hash分区允许使用用户自定义的表达式,而Key分区不允许使用用户自定义的表达式,需要使用MySQL服务器提供的HASH函数;同时Hash分区只支持整数分区,而Key分区支持使用BLOB或Text类型外其他类型的列作为分区键

[SQL] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
drop table if exists test1;
CREATE TABLE test1 (
    id INT NOT NULL primary key  auto_increment, -- 自动递增
    username varchar(5) not null -- 用户名
)
ENGINE=innodb
PARTITION BY key(id) -- 以key.
( -- 分别是
        partition p0
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p1 
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p2 
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test' ,
        partition p3
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test'
)  
 
;

另外还支持子分区.
比如  range基础上再来一个hash子分区

[SQL] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
drop table if exists test1;
CREATE TABLE test1 (
    id INT NOT NULL primary key  auto_increment, -- 自动递增
    username varchar(5) not null -- 用户名
)
ENGINE=innodb
PARTITION BY RANGE(id) -- 以id(必须是主键才可以)分区,共分4个区.
SUBPARTITION by hash(id)
( -- 分别是
        partition p0 VALUES LESS THAN (3)  -- 小于3的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p1 values less than (5)  -- 小于5的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test',
        partition p2 values less than (10) -- 小于10的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test' ,
        partition p3 values less than MAXVALUE -- >10 其它的
                data directory='/data/wwwroot/test'
                index directory='/data/wwwroot/test'
)  
 
;

mysql 5.5后增加了个 COLUMNS 分区

mysql-5.5开始支持COLUMNS分区,可视为RANGE和LIST分区的进化,COLUMNS分区可以直接使用非整形数据进行分区。COLUMNS分区支持以下数据类型:
  所有整形,如INT SMALLINT TINYINT BIGINT。FLOAT和DECIMAL则不支持。
  日期类型,如DATE和DATETIME。其余日期类型不支持。
  字符串类型,如CHAR、VARCHAR、BINARY和VARBINARY。BLOB和TEXT类型不支持。
  COLUMNS可以使用多个列进行分区。

mysql分区分表的更多相关文章

  1. mysql分区分表讲解

    为什么要分表和分区? 日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询的情况,性能 ...

  2. 《Mysql 分区分表》

    一:分区/分表 为了什么? - 当MySQL单表的数据量过大时,数据库的访问速度会下降,需要处理大量数据,所以需要把数据分散存储. - 常用 "水平" 切分 二:MySQL常见的水 ...

  3. 一文搞懂│mysql 中的备份恢复、分区分表、主从复制、读写分离

    目录 mysql 的备份和恢复 mysql 的分区分表 mysql 的主从复制读写分离 mysql 的备份和恢复 创建备份管理员 创建备份管理员,并授予管理员相应的权限 备份所需权限:select,r ...

  4. FreeSql (三十一)分区分表

    分区 分区就是把一个数据表的文件和索引分散存储在不同的物理文件中.把一张表的数据分成N多个区块,这些区块可以在同一个磁盘上,也可以在不同的磁盘上,数据库不同实现方式有所不同. 与分表不同,一张大表进行 ...

  5. SqlServer数据库分区分表实例分享(有详细代码和解释)

    数据库单表数据量太大可能会导致数据库的查询速度大大下降(感觉都是千万级以上的数据表了),可以采取分区分表将大表分为小表解决(当然这只是其中一种方法),比如数据按月.按年分表,最后可以使用视图将小表重新 ...

  6. 在LINUX系统中MySQL数据库区分表名的大小写--解决办法

    因为linux下mysql默认是要区分表名大小写的.mysql是否区分大小写设置是由参数lower_case_table_names决定的, 其中:1)lower_case_table_names = ...

  7. 设置Linux中的Mysql不区分表名大小写

    1. MySQL数据库的表名在Linux系统下是严格区分大小写的,在Windows系统下开发的程序移植到Linux系统下,如果程序中SQL语句没有严格按照大小写访问数据库表,就可能会出现找不到表的错误 ...

  8. 数据库分区分表(sql、mysql)

    http://blog.csdn.net/lgb934/article/details/8662956 http://www.2cto.com/database/201503/380348.html ...

  9. mysql分库分区分表

    分表: 分表分为水平分表和垂直分表. 水平分表原理: 分表策略通常是用户ID取模,如果不是整数,可以首先将其进行hash获取到整. 水平分表遇到的问题: 1. 跨表直接连接查询无法进行 2. 我们需要 ...

随机推荐

  1. python常用模块上篇

    python常见模块 分两篇分别介绍下述模块 time模块 random模块 hashlib模块 os模块 sys模块 logging模块 序列号模块 configparser模块 re模块 time ...

  2. 如何在Windows上搭建Android开发环境

    Android开发越来越火,许多小伙伴们纷纷学习Android开发,学习Android开发首要任务是搭建Android开发环境,由于大家 主要实在Windows 上开发Android,下面就详细给大家 ...

  3. 火狐浏览器打开html文件,中文乱码

    当html文件为: 解决方法: 将   <meta http-equiv="Content-Type" content="text/html; charset=UT ...

  4. JS深层继承

    我们在书写JS的时候常常被一种现象困扰 let jsonA = { a1: { b1:1; }, }; let jsonB = jsonA; jsonB.a1.b1 = 2; console.log( ...

  5. 一个强大的封装好的pdo处理类

    php5.5后就不支持mysql扩展了,也就是说这以后都不能使用msyql_conncet之类的函数了.不过没有关系,pdo比mysql有更多优势,写法也很简单,下面贴出一个来自互联网的pdo处理类. ...

  6. MAC - PhpStorm安装调试环境xdebug

    今天下午一直在捣鼓如何用PhpStorm进行调试,查找了许多资料,零零碎碎的,所以自己弄篇文章记录一下步骤. 安装xdebug 使用brew安装xdebug,语法如下 brew install hom ...

  7. Leetcode题解(十九)

    54.Spiral Matrix 题目: 题目意思很简单,就是螺旋式访问矩阵元素.也没有比较经典的算法可以解决此题,只需要模拟一下这个过程即可. 代码如下: class Solution { publ ...

  8. JavaScript instanceof 运算符深入剖析【转载】

    http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/   instanceof 运算符简介 在 JavaScript ...

  9. div内长串数字或字母不断行处理

    比如: <div>1111tryrt645645rt4554111112324353453454364</div> <div>qwewretrytuytuiyiuo ...

  10. java 导出blob图片到excel

    实现功能,导出当前页面显示员工的图片,核心代码已给出,仅供参考, 如需转载请注明出处http://www.cnblogs.com/wangjianguang/p/7852060.html 随便再扯2句 ...