几千万的数据用 mysql 查询就很慢

2015-02-28 01:15:14 +08:00
 johnnyR

<?php
session_start();
error_reporting(0);
header("Content-type: text/html; charset=utf-8");
require "config.php";
@date_default_timezone_set(PRC);
set_time_limit(0);
@ob_end_clean();
ob_implicit_flush(true);
switch($_GET['act']){
case "database":
if(empty($_SESSION['member'])){
exit('var database=new Array("login");');
}
$connect_db = mysql_connect($dbnhost, $dbnuser, $dbnpass);
$select_db = mysql_select_db($dbname, $connect_db);
mysql_query("SET NAMES 'UTF8'");
mysql_query("SET CHARACTER SET UTF8");
mysql_query("SET CHARACTER_SET_RESULTS=UTF8");
$rs = mysql_query("SHOW TABLES FROM $dbname");
$tables = array();
while ($row = mysql_fetch_row($rs)) {
$tables[] = $row[0];
}
mysql_free_result($rs);
$array_tj=count($tables);
$count=1;
$text="";
foreach($tables as $key=>$tableName){
if($key==count($tables)-1){
$dian="";
}else{
$dian=",";
}
$text=$text.'"'.$tableName.'"'.$dian;
$count++;
}
echo "var database = new Array($text);";

break;
case "select":
if(empty($_SESSION['member'])){
echo "cnrv_msg(\"请登录\");addRow(\"登录后查询\",\"登录后查询\",\"登录后查询\",\"登录后查询\");";
exit;

}
        $select_act=(int)addslashes(trim($_POST['select_act']));
        $match_act=(int)addslashes(trim($_POST['match_act']));
        $key=addslashes(trim($_POST['key']));
        $table=addslashes(trim($_POST['table']));
            if(empty($key) || $key==''){exit("请输入查询内容");}
            if(strlen($key)<4){exit("key length!!!");}

                $key = str_replace("_","\_",$key);
                $key = str_replace("%","\%",$key);
                    switch($match_act){
                        case 2:$key = '=\''.$key.'\'';break;
                        case 1:$key = ' like \''.$key.'%\'';break;
                        default:exit("fuck you!");
                    }
                    switch($select_act){//查询方式
                        case 1:$limits="username".$key;break;
                        case 2:$limits="email".$key;break;
                        case 3:$limits="username".$key."or email".$key;break;
                        default:exit("fuck you!");
                    }
                        $connect_db = mysql_connect($dbnhost, $dbnuser, $dbnpass);
                        $select_db = mysql_select_db($dbname, $connect_db);
                        mysql_query("SET NAMES 'UTF8'");
                        mysql_query("SET CHARACTER SET UTF8");
                        mysql_query("SET CHARACTER_SET_RESULTS=UTF8");
                    $sql="select $Field  from `$table` where $limits LIMIT 20";
                    require "database.php";
                        $databasename=database($table);
                        if($result=mysql_query($sql)){
                            while($rows=mysql_fetch_assoc($result)){
                                    $username= mysql_real_escape_string($rows['username']);
                                    $email= mysql_real_escape_string($rows['email']);
                                    $password= mysql_real_escape_string($rows['password']);
                                    echo "addRow(\"$username\",\"$email\",\"$password\",\"$databasename\");";
                            }// end while
                        }




    break;
    default:print_r("fuck you!");
}

索引FULLTEXT建立了。还是慢。几千万就用了30~配置双核2g内存ssd
2971 次点击
所在节点    问与答
5 条回复
wico77
2015-02-28 01:16:03 +08:00
sphinx?
johnnyR
2015-02-28 01:22:51 +08:00
@wico77 才几千万不用把。
yangqi
2015-02-28 01:25:45 +08:00
又是优化的问题。。。

先explain 然后profiling
johnnyR
2015-02-28 01:36:05 +08:00
@yangqi 嗯../.可以具体一些吗
johnnyR
2015-02-28 01:42:06 +08:00
以解决,谢谢 yangqi

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/173380

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX