Home >php教程 >php手册 >1亿条数据如何分表100张到Mysql数据库中(PHP)

1亿条数据如何分表100张到Mysql数据库中(PHP)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 19:50:101107browse

这篇文章主要介绍了当数据量猛增的时候如何把一亿条数据分表100张到Mysql数据库中,需要的朋友可以参考下

下面通过创建100张表来演示下1亿条数据的分表过程,,具体请看下文代码。

当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度。笔者做了一个简单的尝试,1亿条数据,分100张表。具体实现过程如下:

首先创建100张表:

$i=0; while($i

下面说一下我的分表规则,full_code作为主键,我们对full_code做hash

函数如下:

$table_name=get_hash_table('code',$full_code); function get_hash_table($table,$code,$s=100){ $hash = sprintf("%u", crc32($code)); echo $hash; $hash1 = intval(fmod($hash, $s)); return $table."_".$hash1; }

这样插入数据前通过get_hash_table获取数据存放的表名。

最后我们使用merge存储引擎来实现一张完整的code表

CREATE TABLE IF NOT EXISTS `code` ( `full_code` char(10) NOT NULL, `create_time` int(10) unsigned NOT NULL, INDEX(full_code) ) TYPE=MERGE UNION=(code_0,code_1,code_2.......) INSERT_METHOD=LAST ;

这样我们通过select * from code就可以得到所有的full_code数据了。

以上介绍就是本文的全部内容,希望对大家有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn