mysql千万级数据怎么做优化
<br />function insert(){<br /> global $m;<br /> if (!isset($_REQUEST['strs']) || !isset($_REQUEST['type'])<br /> || !isset($_REQUEST['hash'])){<br /> echo 'param error';<br /> return;<br /> }<br /><br /> //strs为所有字符串<br /> $poststr = $_REQUEST['strs'];<br /> <br /> $xstrs = json_decode(stripslashes($poststr), true);<br /> <br /> $type = $_REQUEST['type'];<br /> $hash = $_REQUEST['hash'];<br /> <br /> if (count($xstrs) <= 0){<br /> $msg = 'str error';<br /> DsLog::errLog($msg.$poststr);<br /> echo $msg;<br /> return;<br /> }<br /><br /> if ($type == '0'){<br /> $table = 'white';<br /> }<br /> else if($type == '1'){<br /> $table = 'black';<br /> }<br /> else{<br /> $msg = 'type error';<br /> DsLog::errLog($msg);<br /> echo $msg;<br /> return;<br /> }<br /><br /> $strs = array();<br /> <br /> for($i = 0; $i < count($xstrs); $i++) {<br /> $strtmp = $xstrs[$i];<br /> $strtmp = trim($strtmp);<br /> $strtmp = strtolower($strtmp);<br /> $strtmp = addslashes($strtmp);<br /> if (strlen($strtmp) > 256){<br /> $strtmp = substr($strtmp, 0, 256);<br /> }<br /> if (strlen($strtmp) >= 7)<br /> {<br /> array_push($strs, $strtmp);<br /> }<br /> }<br /> <br /> <br /> //拼接所有字符串<br /> $tmp = '("'.implode('","', $strs).'")';<br /><br /> //获取已存在的字符串<br /> $sql = "select * from $table where str in $tmp";<br /> $ret = mysql_query($sql, $m);<br /> if (!$ret){<br /> $msg = 'exec error:'.mysql_error($m).','.$sql;<br /> DsLog::errLog($msg);<br /> echo $msg;<br /> return;<br /> }<br /><br /> $exists = array();<br /> $notexists = array();<br /> $count = mysql_num_rows($ret);<br /> for ($i = 0; $i < $count; $i++)<br /> {<br /> $item = mysql_fetch_assoc($ret);<br /> if (!$item){<br /> break;<br /> }<br /> array_push($exists, $item['str']);<br /> }<br /> <br /> for ($i = 0; $i < count($strs); $i++){<br /> if (in_array($strs[$i], $exists)){<br /> continue;<br /> }<br /><br /> array_push($notexists, $strs[$i]);<br /> }<br /><br /> for($i = 0; $i < count($exists); $i++) {<br /> $exists[$i] = addslashes($exists[$i]);<br /> }<br /> for($i = 0; $i < count($notexists); $i++) {<br /> $notexists[$i] = addslashes($notexists[$i]);<br /> }<br /> <br /> if (count($exists) > 0){<br /> //更新已存在字符串的count字段<br /> $tmp = '("'.implode('","', $exists).'")';<br /> $time = date('YmdHi');<br /> $sql = "update $table set count=count+1 where str in $tmp";<br /> $ret = mysql_query($sql, $m);<br /> if (!$ret){<br /> $msg = 'exec error:'.mysql_error($m).','.$sql;<br /> DsLog::errLog($msg);<br /> echo $msg;<br /> return;<br /> }<br /><br /> //更新已存在字符串的upd字段<br /> $sql = "update $table set upd='$time' where str in $tmp";<br /> $ret = mysql_query($sql, $m);<br /> if (!$ret){<br /> $msg = 'exec error:'.mysql_error($m).','.$sql;<br /> DsLog::errLog($msg);<br /> echo $msg;<br /> return;<br /> }<br /> }<br /> <br /> <br /> //插入新信息<br /> if (count($notexists) > 0){<br /> $time = date('YmdHi');<br /> $sql = "insert ignore into $table (str,hash,count, upd) values";<br /> for ($i = 0; $i < count($notexists); $i++){<br /> $str = $notexists[$i];<br /> $crc = sprintf("%u", crc32($str));<br /> $sql .= "('$str','$crc','1', '$time'),";<br /> }<br /><br /> $sql = substr($sql, 0, strlen($sql) - 1);<br /> $ret = mysql_query($sql, $m);<br /> if (!$ret){<br /> $msg = 'insert error:'.mysql_error($m).','.$sql;<br /> DsLog::errLog($msg);<br /> echo $msg;<br /> return;<br /> }<br /> }<br /> <br /> echo !!$ret;<br /> }<br />