mysql大数据分表后查询

当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度,举例说明:

1亿条数据,分100张表

1.首先创建100张表

i = 0 ; w h i l e ( i=0; while( i=0;while(i<=99){
echo “$newNumber \r\n”;
s q l = &quot; C R E A T E T A B L E ‘ c o d e &quot; . sql=&quot;CREATE TABLE `code_&quot;. sql="CREATETABLEcode".i.“(full_codechar(10) NOT NULL,create_timeint(10) unsigned NOT NULL, PRIMARY KEY (full_code`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8”;
mysql_query($sql);
$i++;

2.分表规则:

full_code作为主键,对full_code做hash

t a b l e n a m e = g e t h a s h t a b l e ( ′ c o d e ′ , table_name=get_hash_table(&#x27;code&#x27;, tablename=gethashtable(code,full_code);

function get_hash_table( t a b l e , table, table,code,$s=100){
h a s h = s p r i n t f ( &quot; hash = sprintf(&quot;%u&quot;, crc32( hash=sprintf("code));
echo $hash;
h a s h 1 = i n t v a l ( f m o d ( hash1 = intval(fmod( hash1=intval(fmod(hash, $s));
return t a b l e . &quot; &quot; . table.&quot;_&quot;. 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数据了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值