function insert(){global $m;if (!isset($_REQUEST['strs']) || !isset($_REQUEST['type']) || !isset($_REQUEST['hash'])){echo 'param error';return;}//strs為所有字串$poststr = $_REQUEST['strs']; $xstrs = json_decode(stripslashes($poststr), true); $type = $_REQUEST['type'];$hash = $_REQUEST['hash'];if (count($xstrs) <= 0){$msg = 'str error';DsLog::errLog($msg.$poststr);echo $msg;return;}if ($type == '0'){$table = 'white';}else if($type == '1'){$table = 'black';}else{$msg = 'type error';DsLog::errLog($msg);echo $msg;return;} $strs = array(); for($i = 0; $i < count($xstrs); $i++) { $strtmp = $xstrs[$i]; $strtmp = trim($strtmp); $strtmp = strtolower($strtmp); $strtmp = addslashes($strtmp); if (strlen($strtmp) > 256){$strtmp = substr($strtmp, 0, 256);} if (strlen($strtmp) >= 7) { array_push($strs, $strtmp); } } //拼接所有字串$tmp = '("'.implode('","', $strs).'")';//擷取已存在的字串$sql = "select * from $table where str in $tmp";$ret = mysql_query($sql, $m);if (!$ret){$msg = 'exec error:'.mysql_error($m).','.$sql;DsLog::errLog($msg);echo $msg;return;}$exists = array();$notexists = array();$count = mysql_num_rows($ret);for ($i = 0; $i < $count; $i++){$item = mysql_fetch_assoc($ret);if (!$item){break;}array_push($exists, $item['str']);}for ($i = 0; $i < count($strs); $i++){if (in_array($strs[$i], $exists)){continue;}array_push($notexists, $strs[$i]);}for($i = 0; $i < count($exists); $i++) { $exists[$i] = addslashes($exists[$i]); } for($i = 0; $i < count($notexists); $i++) { $notexists[$i] = addslashes($notexists[$i]); }if (count($exists) > 0){//更新已存在字串的count欄位$tmp = '("'.implode('","', $exists).'")';$time = date('YmdHi');$sql = "update $table set count=count+1 where str in $tmp";$ret = mysql_query($sql, $m);if (!$ret){$msg = 'exec error:'.mysql_error($m).','.$sql;DsLog::errLog($msg);echo $msg;return;}//更新已存在字串的upd欄位$sql = "update $table set upd='$time' where str in $tmp";$ret = mysql_query($sql, $m);if (!$ret){$msg = 'exec error:'.mysql_error($m).','.$sql;DsLog::errLog($msg);echo $msg;return;}}//插入新資訊if (count($notexists) > 0){$time = date('YmdHi');$sql = "insert ignore into $table (str,hash,count, upd) values";for ($i = 0; $i < count($notexists); $i++){$str = $notexists[$i];$crc = sprintf("%u", crc32($str));$sql .= "('$str','$crc','1', '$time'),";}$sql = substr($sql, 0, strlen($sql) - 1);$ret = mysql_query($sql, $m);if (!$ret){$msg = 'insert error:'.mysql_error($m).','.$sql;DsLog::errLog($msg);echo $msg;return;}}echo !!$ret;}
我現在要對字串做統計,統計每個字串的數量,就是類似一個map, 這符串:數量,當然,資料庫順便還儲存了一下字元中的crc等資訊
insert函數接收post過來的strs,這是一個字串數組,每次post過來的字串數量平均為1500
現在資料庫裡的記錄1200W條,每一次insert花費的時間平均為20秒(經常出現30秒逾時)
請問下各位,該怎麼做最佳化,現在只是統計了很小一部分,預計統計完資料量大概在1-10億條
回複討論(解決方案)
對了,str欄位做了唯一索引,其它欄位沒做,mysql為MyISAM
你能確定瓶頸是在資料庫做 查詢 和 修改 的3條語句上嗎?
你說 每次post過來的字串數量平均為1500
那是指 1500 的單詞嗎?就算不是(是串長度)那麼以平均每個單詞20個字元計算,也有75個單詞
你的過濾條件是 str in $tmp (str in ('xxx','xxx'....))也就是對錶中的每一條記錄都要做 1500(75)次字串比較,而命中率至多是1/1500(1/75)你認為這樣做合適嗎?
資料庫的強項在於記錄間的比較,而你卻在做弱勢的列間比較
你可以用傳入的資料構造出一個暫存資料表(每行一個單詞),再去與主表關聯操作。這樣才能發揮資料庫的優勢
再有,你的連續 2 條 update 語句不能合并在一起嗎?
你能確定瓶頸是在資料庫做 查詢 和 修改 的3條語句上嗎?
你說 每次post過來的字串數量平均為1500
那是指 1500 的單詞嗎?就算不是(是串長度)那麼以平均每個單詞20個字元計算,也有75個單詞
你的過濾條件是 str in $tmp (str in ('xxx','xxx'....))也就是對錶中的每一條記錄都要做 1500(75)次字串比較,而命中率至多是1/1500(1/75)你認為這樣做合適嗎?
資料庫的強項在於記錄間的比較,而你卻在做弱勢的列間比較
你可以用傳入的資料構造出一個暫存資料表(每行一個單詞),再去與主表關聯操作。這樣才能發揮資料庫的優勢
再有,你的連續 2 條 update 語句不能合并在一起嗎?
1500是指字元數,字元數應該在5W左右
你的意思是先把1500條記錄insert到一個暫存資料表tmp_table,然後
然後 select* from table inner join tmp_talbe on table.str = tmp_table.str 這樣嗎?
你能確定瓶頸是在資料庫做 查詢 和 修改 的3條語句上嗎?
你說 每次post過來的字串數量平均為1500
那是指 1500 的單詞嗎?就算不是(是串長度)那麼以平均每個單詞20個字元計算,也有75個單詞
你的過濾條件是 str in $tmp (str in ('xxx','xxx'....))也就是對錶中的每一條記錄都要做 1500(75)次字串比較,而命中率至多是1/1500(1/75)你認為這樣做合適嗎?
資料庫的強項在於記錄間的比較,而你卻在做弱勢的列間比較
你可以用傳入的資料構造出一個暫存資料表(每行一個單詞),再去與主表關聯操作。這樣才能發揮資料庫的優勢
再有,你的連續 2 條 update 語句不能合并在一起嗎?
1500是字串數,大概3W個字元
你的意思是把1500個字元中插入到暫存資料表tmp裡,然後再 select* from white inner join tmp on white.str=tmp.str 嗎?
我剛測了下
select* from white inner join tmp on white.str=tmp.str 執行時間10S
id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE tmp index str str 770 NULL 2531 Using index1 SIMPLE white eq_ref str str 770 bayes.tmp.str 1
直接執行 select * from white where str in(''xxx','xxx'...) 執行時間9S
id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE white range str str 770 NULL 2531 Using where
感覺效果差不多一樣
1、mysql 有記憶體表,你並未使用
2、將兩條 update 合并,應該能減少 10 秒
mysql_error這種庫函數的效率也不高吧而且官方也不推薦,我幾乎沒有用過這種,我學PHP的時候,直接開始PDO的。
還有,你用了in這個關鍵字,好像它是不走索引的吧。
感覺效果差不多一樣
應該是你測試環境的問題吧,聯表查詢大多數情況下是要比in快的
你測的結果差不多可能是如下原因
1.查詢表的資料沒有達到一定量
2.兩種查詢方式查出的結果集不同
3.查詢欄位未加索引或加的不對
4.被其他索引過多而影響
5.其他原因
我認為比較明顯的就是第2個原因
你聯表查時沒有指定查詢欄位,聯表出的欄位肯定要比不連表的要多
還有聯表查詢會有一對多的情況,那樣一條記錄就會變成多條的,要加group by