为毛要分表和分区,,,,
所有数据库的通病,文件越大,性能越低...那问题就来了.数据越多文件越大...无解?哎,所以说知道 为毛要分区了吧!
那分表又是毛线?
分表就是把一张表拆分成若干表,,,根据情况常见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. 微信公众号开发(十二)OAuth2.0网页授权

    OAuth允许用户提供一个令牌,而不是用户名和密码来访问它们存放在特定服务器上的数据,每一个令牌授权一个特定的网站在特定时段内访问特定的资源. 授权过程如下: 1.引导用户进入授权页面同意授权,获取c ...

  2. AJAX中使用post,get接收发送数据的区别

    如何发起请求 xhr.send(); 备注: 如果是get方式,send()命令中不用写任何参数 传递的数据可以写在url中,服务器用$_GEET["参数名"]接收 如果是post ...

  3. Milking Time

    Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...

  4. SQL Server远程连接(2)

  5. C#使用Oracle.ManagedDataAccess.dll

    在刚接触C#的时候由于公司使用的就是Oracle数据库,那么C#怎么连接Oracle数据库就成了首要去掌握的知识点了.在那时没有ODP.NET,但visual studio却对Oralce数据库的调用 ...

  6. JS中有关数组Array的常用方法函数

    Array对象的方法主要有如下几种(我所知道的): concat()连接两个或多个数组,并返回结果,但是值得注意的是该方法并不改变数组本身,而仅仅返回一个数组连接的副本. push()在数组后面添加一 ...

  7. 【转】用PowerDesigner制作数据库升级脚本

    [原创]用PowerDesigner制作数据库升级脚本   很多人使用PD的时候就问有没有制作自动升级脚本的功能.其实是有的. 操作原理: 1.保存原来的版本,另存为apm的文件,生成一个Archiv ...

  8. Eclipse中代码整体左移,右移快捷键

    1.向右:将要移动的代码选中,然后按TAB键2.向左:将要移动的代码选中,然后按SHIFT+TAB键

  9. iPhone X 网页导航概念

     以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具.   在移动应用程序设计中,选择汉堡菜单按钮还是标签栏作为导航一直是个古老的争论话题.目前看来,由于 ...

  10. 关于SQLServer数据库中字段值为NULL,取出来该字段放在DataTable中,判断datatable中该字段值是否为NULL的三种方法

    1. DataTable dt;                               //假设字段为name, dt已经保存了数据dt.rows[0]["name"] == ...