mysql大数据分表后查询
2016-05-11 14:28
316 查看
当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度,举例说明:
1亿条数据,分100张表
1.首先创建100张表
$i=0;
while($i<=99){
echo "$newNumber \r\n";
$sql="CREATE TABLE `code_".$i."` (
`full_code` char(10) NOT NULL,
`create_time` int(10) unsigned NOT NULL,
PRIMARY KEY (`full_code`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
mysql_query($sql);
$i++;
2.分表规则:
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获取数据存放的表名。
3.使用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数据了。
1亿条数据,分100张表
1.首先创建100张表
$i=0;
while($i<=99){
echo "$newNumber \r\n";
$sql="CREATE TABLE `code_".$i."` (
`full_code` char(10) NOT NULL,
`create_time` int(10) unsigned NOT NULL,
PRIMARY KEY (`full_code`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
mysql_query($sql);
$i++;
2.分表规则:
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获取数据存放的表名。
3.使用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数据了。
相关文章推荐
- 发现大量的TIME_WAIT解决办法
- 实用简介:MQTT协议及其在物联网中的应用
- 汇编学习第四课之main函数反汇编
- 大数据交易平台
- ActivityViewController 使用AirDrop分享
- STM32单片机实现中断后不继续向下执行而是返回到main函数
- CDISC SDTM EC/EX domain 学习笔记 - 4
- Mac Mail中删除服务器上的邮件
- kail2.0下hping3的安装和使用
- platform-tools\adb.exe,start-server' failed -- run manually if necessary,adb启动失败,端口被占用
- How to Test Controller Concerns in Rails 4
- Contains Duplicate
- Xcode运行错误:failed to get the task for process XXX
- LNK1123: failure during conversion to COFF: file invalid or corrupt-问题解决办法
- 墙国内新建Rails应用的要点(windows 7环境, Rails 4.2.0)
- svn Error: post-commit hook failed (exit code 127) with output
- Kafka High Availability (下)
- Kafka High Availability (上)
- CDISC SDTM EC/EX domain 学习笔记 - 3
- netstat监控大量ESTABLISHED连接与Time_Wait连接问题