当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP操作mysql数据库分表的方法

PHP操作mysql数据库分表的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法。首先,我们需要想好到底分多少个 表,前提当然是满足应用。这里我使用了一个比较简单的分表方法,就是根据自增id的尾数来分,也就是说分0-9一共10个表,其取值也很好做,就是对10 进行取模。另外,还可以根据某一字段的md5值取其中几位进行分表,这样的话,可以分的表就很多了。

好了,先来创建表吧,代码如下:

create table `ttlsa_com`.`article_0` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_1` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_2` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_3` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_4` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_5` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_6` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_7` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_8` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci
create table `ttlsa_com`.`article_9` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine = myisam character set utf8 collate utf8_general_ci

好了10个表创建完毕了,需要注意的是,这里的id不能设为自增,而且所有的表结构必须一致,包括结构,类型,长度,字段的顺序都必须一致那么对于这个id如何取得呢?后面我会详细说明。现在,我们需要一个合并表,用于查询,创建合并表的代码如下:

create table `ttlsa_com`.`article` ( 
`id` bigint( 20 ) not null ,
`subject` varchar( 200 ) not null ,
`content` text not null ,
primary key ( `id` )
) engine=mrg_myisam default charset=utf8 insert_method=0 union =(`article_0`,`article_1`,`article_2`,`article_3`,`article_4`,`article_5`,`article_6`,`article_7`,`article_8`,`article_9`);

注意,合并表也必须和前面的表有相同的结构,类型,长度,包括字段的顺序都必须一致这里的insert_method=0表示不允许对本表进行 insert操作。好了,当需要查询的时候,我们可以只对article这个表进行操作就可以了,也就是说这个表仅仅只能进行select操作,那么对于 插入也就是insert操作应该如何来搞呢,首先就是获取唯一的id了,这里就还需要一个表来专门创建id,代码如下:

create table `ttlsa_com`.`create_id` ( 
`id` bigint( 20 ) not null auto_increment primary key 
) engine = myisam

也就是说,当我们需要插入数据的时候,必须由这个表来产生id值,我的php代码的方法如下:

<?php 
function get_ai_id() { 
$sql = "insert into create_id (id) values('')"; 
$this->db->query($sql); 
return $this->db->insertid(); 
} 
?>

好了,现在假设我们要插入一条数据了,应该怎么操作呢?还是继续看代码吧

<?php 
function new_article() { 
$id = $this->get_ai_id(); 
$table_name = $this->get_table_name($id); 
$sql = "insert into {$table_name} (id,subject,content) values('{$id}','测试标题','测试内容')"; 
$this->db->query($sql); 
} 
/** 
* 用于根据id获取表名 
*/ 
function get_table_name($id) { 
return 'article_'.intval($id)%10; 
} 
?>

其实很简单的,对吧,就是先获取id,然后根据id获取应该插入到哪个表,然后就很简单了。
对于update的操作我想应该不需要再说了吧,无非是有了id,然后获取表名,然后进行update操作就好了。
对于用户表,建个最少列最基本信息的用户名,比如用户id,用户名,密码。用户的其他信息分布到以用户id分表的表上。
怎么分表如何分表以业务需求而定。

你可以根据id分,也可以根据年,月,地区来分。要按照业务需求。

以上所述是小编给大家介绍的php操作mysql数据库分表的方法,希望对大家有所帮助,如果大家有不同的见解欢迎提出,共同学习进步!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网