您的位置:首页 > 数据库 > MySQL

mysql分表

2017-01-17 20:04 405 查看
1、水平分表
创建结构相同的N个表
create table student_0 (
id int not null auto_increment,
name varchar(12),
primary key (id)
);

create table student_1 (
id int not null auto_increment,
name varchar(12),
primary key (id)
);

create table student_2 (
id int not null auto_increment,
name varchar(12),
primary key (id)
);

create table student_id (
id int not null auto_increment,
primary key (id)
);

//伪代码
$tableList = array(
'student_0', 'student_1', 'student_2'
);

$tableNums = count($tableList);
$sqlId = "insert into student_id values(null)";
$stuId = last_insert_id();
$tableName = $tableList[$stuId % $tableNums];
$sql = "insert into {$tableName} values({$stuId}, '测试')";

2、merge,mrg_myisam存储引擎
mysql提供一个可以将多个结构相同的myisam表,合并到一起的存储引擎。

3、垂直分表
表中存在多个字段,将常用字段和非常用字段分别存到两张或以上的表中,
主要目的,减少每条记录的长度。
比如学生表:
基础信息表student_base
额外信息表student_extra
基础表与额外表之间通过ID来对应。

(*水平分表现在用mysql自带的partition已经很好解决了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: