为毛要分表和分区,,,,
所有数据库的通病,文件越大,性能越低...那问题就来了.数据越多文件越大...无解?哎,所以说知道 为毛要分区了吧!
那分表又是毛线?
分表就是把一张表拆分成若干表,,,根据情况常见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. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. centos 源码安装python

    一.准备环境 首先在官网下载想要的python对应版本http//www.python.org/downloads/source 下载tgz就可以了.文件有两种 1,Python-版本号.tgz(解压 ...

  3. 使用Identity Server 4建立Authorization Server (1)

    预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 本文内容基本完全来自于Identity Server 4官方文档: https://identitys ...

  4. stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结

    stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...

  5. this 和 new 构造函数

    function people(name) {     这样定义是在全局命名空间(global namespace)    name: name,    sayname: function() {   ...

  6. 【最新版】从零开始在 macOS 上配置 Lua 开发环境

    脚本语言,你可能更需要的是 Lua 不同的脚本语言有不同的特性,第一接触的脚本语言,可能会影响自己对整个脚本语言的理解和认知.我以前接触最多的脚本语言是 JavaScript.后果就是:我一度以为脚本 ...

  7. jQuery实现checkbox即点即改,批量计数,以及中间遇到的坑

    最近要用jQuery实现一个批量删除操作,效果如下图 最终页面page.html,此页面使用了bootstrap和jQuery,如果没有需要下载一下 <!DOCTYPE html> < ...

  8. 一个简单的基于BIO的RPC框架

    github地址:https://github.com/Luyu05/BioRpcExample PART1:先来整体看下项目的构成 其中bio-rpc-core就是所谓的rpc框架 bio-rpc- ...

  9. 机器学习数学|Taylor展开式与拟牛顿

    机器学习中的数学 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原创文章,如需转载请保留出处 本博客为七月在线邹博老师机器学习数学课程学习笔记 Taylor 展式与拟牛顿 索引 taylor ...

  10. rem布局配合less的快速开发

    最近在进行静态页面的制作,为了方便和快速的布局,自己整理了一套工具可以快速的进行工作,剩余的时间大家都懂的,话不多说,来看具体的东西吧! 1.ps 下载这个软件→cutterman 十分强大的切图功能 ...