Rex_Gao 发表于 2017-2-9 18:40:23

sphinx索引速度异常慢,求大神啊 T_T

把sphinx写成library调用,连接设置都很正常,也能运行搜索,问题是速度异。。。。。。。。。。常。。。。。。的。。。。。。慢。。。。。。普通搜索要两秒才有结果,果断通过mysql连接sphinx测试,同样的搜索只要0.02秒,完全没有头绪。。。。。。求大神啊:'(

Hex 发表于 2017-2-9 20:07:09

贴代码看看,不过这个和 CI 有什么关系...

Rex_Gao 发表于 2017-2-9 22:46:17

Hex 发表于 2017-2-9 20:07
贴代码看看,不过这个和 CI 有什么关系...

类是从之前的项目拷贝过来的,之前用的速度都ok。。。理论上来说应该不影响才对,实在想不明白是什么原因了
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sphinx {
    public $api='';
    public $index = 'arczw_content';
    public $host = 'localhost';
    public $port = '9312';
   
    public function __construct(){
      require_once(APPPATH.'libraries\sdk\sphinx\sphinxapi.php');
      $this->api=new SphinxClient ();
    }
   
    public function initServer(){
      $this->api->SetServer($this->host,$this->port);
    }
   
    public function setIndex($index){
      if(is_string($index)){
            $this->index = $index;
      }
    }
   
    public function search($q,$limit=20,$rejectArray = array(),$SetArray = array(),$idRangeStart=0 ,$idRangeEnd=0 ,$sortQuery = '' ){
      if($this->api =='' || $this->index=='') return false;
      $this->api->ResetFilters();//重置过滤信息,把之前设置的过滤设置清楚
      $this->api->SetIDRange($idRangeStart,$idRangeEnd);//设置文章id范围,两个参数都是0是指不设置
      !empty($sortQuery) && $this->api->SetSortMode(SPH_SORT_EXTENDED,$sortQuery);//sql类排序模式,根据字段属性排序,具体用法找文档。默认值是指根据id进行降序排列
      !empty($rejectArray)&&$this->api->SetFilter('@id',$rejectArray,true);//id过滤设置,过滤掉输入的rejectArray数组里面的id值,与普通的字段过滤不一样,id过滤需要携程"@id"
      foreach($SetArray as $k => $v){
            !empty($k) && !empty($v) && $this->api->SetFilter($k,$v);
      }
      $this->api->SetLimits(0,$limit);//设置获取文章的数量
      $res = $this->api->Query($q,$this->index);
      echo $q.$this->index;
      return $res;
    }
}

Rex_Gao 发表于 2017-2-10 00:49:12

原因找到了,sphinx自带的api是通过fsockopen链接服务的,host填localhost时,php会先查找dns。。。。猜测绕过了系统本身的host文件进行查找(这里还不确定,如果真的绕过了应该是找不到ip才对,但是最终却找到了),把localhost改成ip就可以了。。。。。有哪位大神知道这里填localhost会变慢的原因吗?

Hex 发表于 2017-2-10 10:39:21

Rex_Gao 发表于 2017-2-10 00:49
原因找到了,sphinx自带的api是通过fsockopen链接服务的,host填localhost时,php会先查找dns。。。。猜测 ...
这个和 mysql host 填 localhost 问题类似,都是先去做域名解析,并不是绕过本地 hosts 可能是先去网络上解析DNS,再从本地取,所以能写 IP 就不要写域名,特别是 localhost 这种本地域名。
页: [1]
查看完整版本: sphinx索引速度异常慢,求大神啊 T_T