为什么取出简单的十多条数据都要十多秒 应该如何优化代码结构 谢谢啦

在多台服务器上测试,排除服务器性能原因
仅仅取出数十条数据都要等十多秒
应该是代码优化的原因

Controller如下

       $list=$CaipanOrderModel->getCaipanOrder(null,$orderno,$uid,$productno,
            $play,$time,$winorlose,$type,'caipanorder_buytime desc',
            $issimulate,$usercategory,$agent_class,
            $createtime_starttime, $createtime_endtime,$closetime_starttime,$closetime_endtime);

        foreach ($list as $key => $value) {
            //倒计时
            $shengyu_second = strtotime($value['caipanorder_expiredtime']) - time();
            $list[$key]['daojishi'] = $shengyu_second;
        }

Model

 public function getCaipanOrder($id,$no,$uid,$productno,
                                     $play,$time,$winorlose,$type,$order,
                                     $issimulate,$usercategory,$agent_class,
                                     $createtime_starttime, $createtime_endtime,$closetime_starttime,$closetime_endtime){

          $m=M('caipan_order');

          if($id){
              $con['caipanorder_id']=$id;
          }

          if($no){
              $con['caipanorder_no']=$no;
          }

          if($uid){
              $con['caipanorder_uid']=$uid;
          }

          if($productno){
              $con['caipanorder_productno']=$productno;
          }

          if($play){
              $con['caipanorder_play']=$play;
          }

          if($time){
              $con['caipanorder_time']=$time;
          }

          if($winorlose){
              $con['caipanorder_winorlose']=$winorlose;
          }

          if($type!==null){
              $con['caipanorder_type']=$type;
          }

          if($issimulate!==null){
              $con['user_issimulate_user']=$issimulate;
          }

          if($usercategory!==null){
              $con['user_catgoryid']=$usercategory;
          }

          if($agent_class!==null){
              $con['user_agent_class']=$agent_class;
          }

          if($createtime_starttime){
              $con['caipanorder_buytime']=array(array('egt',$createtime_starttime),
                  array('elt',$createtime_endtime),'and') ;
          }

          if($closetime_starttime){
              $con['caipanorder_sellingtime']=array(array('egt',$closetime_starttime),
                  array('elt',$closetime_endtime),'and') ;
          }




          return $r=$m->where($con)->order($order)
              ->join("LEFT JOIN x_user ON x_user.user_id=x_caipan_order.caipanorder_uid")
              ->join("LEFT JOIN x_user_category ON x_user_category.usercategory_id=x_user.user_catgoryid")
              ->select();


      }
阅读 2.8k
6 个回答

去explain看看吧,一般这样的问题80%的是因为没有用到索引,即便是10条数据,但是如果有个表有10万条数据,没有用索引相当于查询10万次。
客观上来说你的foreach不会太慢,因为你就只有10条数据,首先要排查的是数据库的问题。

如果可以的话最好写纯 SQL 这样好优化 用框架自带的东西优化起来不是很直观

数了一下,一个方法有16个参数,原则上 方法的参数不应该超过2个,如果过多应该采用 对象 的方式传递。

其次再说代码 你可以用 microtime 方法 测一下 从进去第一个 if 到 调用 Model 前花销的时间,虽然这部分不会花费太多时间,但是他也是用了。

最后,看到你有连接两个表而且,没有 limit 也不知道你这3个表的数据量如何,是否有索引等。可以把 select 方法换成 buildSql 方法,来输出编译好的 SQL 语句,然后去 SQL 管理工具上用 explain 命令对 SQL 的执行率进行检查,

主要问题出现在这里

 $m=M('caipan_order');
 ......
 return $r=$m->where($con)->order($order)
              ->join("LEFT JOIN x_user ON x_user.user_id=x_caipan_order.caipanorder_uid")
              ->join("LEFT JOIN x_user_category ON x_user_category.usercategory_id=x_user.user_catgoryid")
              ->select();

核心问题如下:
1。没有指定select 字段,会导致查询三个表所有字段。
2。三个表联合查询,产生的笛卡尔积很大。where $con只作用caipan_order对其他两副表没有约束,故不建议做联合查询,改为单表查询。

补充点:目前caipan_order where $con是否使用到索引情况不明。

代码处理并不复杂, 主要耗时应该是在 sq l上, 建议题主把 sql 发出来, explain 也一起发出来.
返回数据量和耗时有一定关系, 但是最主要解耗时问题还是主要取决于查询计划.

关联查询的效率确实不咋地,可以根据where条件添加对应的索引,还有查询的字段也可以做下限定,只查自己需要用到的字段。如果数据量大的话,查询出来的数据再做一遍foreach更慢。。可以在sql语句对时间进行计算获取倒计时的时间戳。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题