douzhi1879 2012-03-06 14:52
浏览 47
已采纳

如何优化这个mysql查询? 在nginx和fastcgi上

i have a web app built on code igniter framework on the top of nginx , fastcgi and mysql

i have a payout table . table structure is here.

in this table , country names , perminutecost vs are stored and nearly 56,373 records on it.


in main page , there is a form that requests user to type his cellphone number to retrieve perminute cost.Btw i am using auto-complete feature as user types in

here is my backend code:

$str holds user input(cellphone number)

$ret = true; $count = 3;

        while($ret){
            $sub = substr($str,0,$count); //9053
            $ret = R::getAll("SELECT Destination,PerMinuteCost FROM `payout` WHERE `Prefix` REGEXP '^$sub(.)*$' LIMIT 0 , 30");
            $count++;
        }

        $sub = substr($str,0,$count-2);

        $ret =  R::getAll("SELECT Destination,PerMinuteCost FROM `payout` WHERE `Prefix` REGEXP '^$sub(.)*$' LIMIT 0 , 30");

        return $ret[0];

this code lets me get perminutecost from a cellphone number.(the table holds just prefixes not all the cell phone numbers)

i did some modifications on nginx and fastcgi to extend timeout limits

but when too much people use the service at the same time , mysqld cpu usage is getting over 100% ,

how could i improve this algorithm ?

thanks.

  • 写回答

3条回答 默认 最新

  • doutang7707 2012-03-06 15:01
    关注

    I think just a LIKE '$sub%' would be faster than regex and it might be better for your db if they are not autocompleting until 3 numbers.

    Post some example SQL output if you put "EXPLAIN" at the beginning of the sql outside of this script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题