10t數(shù)據(jù) 你算下需要多少臺(tái)服務(wù)器存,然后哈希用戶唯一標(biāo)識(shí),給用戶平均的分配到服務(wù)器上,

創(chuàng)新互聯(lián)建站專注于西盟網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供西盟營銷型網(wǎng)站建設(shè),西盟網(wǎng)站制作、西盟網(wǎng)頁設(shè)計(jì)、西盟網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造西盟網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供西盟網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
但是,你的這個(gè)假設(shè)也太逗了。假設(shè)1g數(shù)據(jù)單表。存500萬條,你這10t數(shù)據(jù),,一天就要產(chǎn)生5千億條記錄。你的網(wǎng)站干嘛的,給全銀河系的人訪問的吧
下面通過創(chuàng)建100張表來演示下1億條數(shù)據(jù)的分表過程,具體請(qǐng)看下文代碼。
當(dāng)數(shù)據(jù)量猛增的時(shí)候,大家都會(huì)選擇庫表散列等等方式去優(yōu)化數(shù)據(jù)讀寫速度。筆者做了一個(gè)簡單的嘗試,1億條數(shù)據(jù),分100張表。具體實(shí)現(xiàn)過程如下:
首先創(chuàng)建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++;
下面說一下我的分表規(guī)則,full_code作為主鍵,我們對(duì)full_code做hash
函數(shù)如下:
$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;
}
這樣插入數(shù)據(jù)前通過get_hash_table獲取數(shù)據(jù)存放的表名。
最后我們使用merge存儲(chǔ)引擎來實(shí)現(xiàn)一張完整的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數(shù)據(jù)了。
以上介紹就是本文的全部內(nèi)容,希望對(duì)大家有所幫助。
分卷導(dǎo)出思路:統(tǒng)計(jì)sql語句變量的長度,按1個(gè)字符當(dāng)成1
字節(jié)比較,如果大于設(shè)定分卷大小,則寫入一個(gè)sql文件(我也不知道這樣統(tǒng)計(jì)是否穩(wěn)當(dāng),這也是借鑒其他的人的)。
分卷導(dǎo)入思路:按行讀取sql文件,將每一行當(dāng)作完整的sql語句存到數(shù)組再循環(huán)執(zhí)行插入數(shù)據(jù)庫就可以了,但是在創(chuàng)建表語句分了多行,這個(gè)需要單獨(dú)處理(就這個(gè)花了我好長時(shí)間的);
?php
//宋正河
轉(zhuǎn)載請(qǐng)注明出處
set_time_limit(0);
header('content-type:text/html;charset=utf-8');
mysql_connect('localhost','root','root');
mysql_select_db('test');
$table_array=get_tables('test');
mysql_query('set
names
utf8');
$filesize=1024*1024*4;
$start=$_GET['start']?$_GET['start']:0;
$part=$_GET['part']?$_GET['part']:'1';
$table_index=$_GET['table_index']?$_GET['table_index']:'0';
$table=$table_array[$table_index];
$num=200000000;//這個(gè)數(shù)要足夠大,可以是總記錄數(shù)
$backupdata='';
if($start=='0'){
$query="SHOW
CREATE
TABLE
`{$table}`";
$result
=
mysql_query($query);
$row
=
mysql_fetch_row($result);
$backupdata
.=
"DROP
TABLE
IF
EXISTS
`{$table}`;\n"
.
$row[1]
.
";\n\n";
}
$limit=($start=='0')?'':"
limit
$start,$num
";
$query="select
*
from
`{$table}`
$limit
";
$result=mysql_query($query);
$numfields
=
mysql_num_fields($result);
//統(tǒng)計(jì)字段數(shù)
while($row=mysql_fetch_row($result)){
$comma
=
'';
//存儲(chǔ)逗號(hào)
$backupdata_tmp
=
"INSERT
INTO
`{$table}`
VALUES
(";
for($i=0;
$i$numfields;
$i++){
$backupdata_tmp
.=
$comma
.
"'"
.
mysql_escape_string($row[$i])
.
"'";
$comma
=
',';
}
$backupdata_tmp
.=
");\n";
if(strlen($backupdata)+strlen($backupdata_tmp)
$filesize){
//寫入文件并跳轉(zhuǎn)
$file='data/'.$table.'-'.$part.'.sql';
file_put_contents($file,$backupdata);
echo
$file.'
備份完成,程序繼續(xù)進(jìn)行!';
$part++;
//分段
//表名
//起點(diǎn)
//跳轉(zhuǎn)
sleep(3);
echo
"scriptlocation.href='?start={$start}table_index={$table_index}part={$part}';/script";
exit;
}
$backupdata.=$backupdata_tmp;
$start++;
}
if($backupdata){
$file='data/'.$table.'-'.$part.'.sql';
file_put_contents($file,$backupdata);
}
echo
$table.'備份完成!br
/';
sleep(2);
$table_index++;
if($table_array[$table_index]){
echo
"scriptlocation.href='?table_index={$table_index}';/script";
exit;
}else{
echo
'恭喜你,數(shù)據(jù)庫備份完畢!';
}
function
get_tables($db){
$tq
=
mysql_list_tables($db);
while($tr
=
mysql_fetch_row($tq)){
$arrtb[]
=
$tr[0];
}
return
$arrtb;
}
?
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
網(wǎng)站名稱:php分表存儲(chǔ)數(shù)據(jù) php分表分庫
文章來源:http://chinadenli.net/article14/dojgdge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、Google、App設(shè)計(jì)、網(wǎng)站改版、網(wǎng)站建設(shè)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)