1. 程式人生 > >mysql大資料分表後查詢

mysql大資料分表後查詢

當資料量猛增的時候,大家都會選擇庫表雜湊等等方式去優化資料讀寫速度,舉例說明:

1億條資料,分100張表

1.首先建立100張表

i=0;while(i=0; while(i<=99){ echo “$newNumber \r\n”; sql=&quot;CREATETABLEcode&quot;.sql=&quot;CREATE TABLE `code_&quot;.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

tablename=gethashtable(code,table_name=get_hash_table(&#x27;code&#x27;,full_code);

function get_hash_table(table,table,code,$s=100){ hash=

sprintf(&quot;hash = sprintf(&quot;%u&quot;, crc32(code)); echo $hash; hash1=intval(fmod(hash1 = intval(fmod(hash, $s)); return table.&quot;&quot;.table.&quot;_&quot;.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資料了。