用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1941|回复: 4
收起左侧

[已解决] sphinx索引速度异常慢,求大神啊 T_T

[复制链接]
发表于 2017-2-9 18:40:23 | 显示全部楼层 |阅读模式
把sphinx写成library调用,连接设置都很正常,也能运行搜索,问题是速度异。。。。。。。。。。常。。。。。。的。。。。。。慢。。。。。。普通搜索要两秒才有结果,果断通过mysql连接sphinx测试,同样的搜索只要0.02秒,完全没有头绪。。。。。。求大神啊
发表于 2017-2-9 20:07:09 | 显示全部楼层
贴代码看看,不过这个和 CI 有什么关系...
 楼主| 发表于 2017-2-9 22:46:17 | 显示全部楼层
Hex 发表于 2017-2-9 20:07
贴代码看看,不过这个和 CI 有什么关系...

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

  3. class Sphinx {
  4.     public $api='';
  5.     public $index = 'arczw_content';
  6.     public $host = 'localhost';
  7.     public $port = '9312';
  8.    
  9.     public function __construct(){
  10.         require_once(APPPATH.'libraries\sdk\sphinx\sphinxapi.php');
  11.         $this->api=new SphinxClient ();
  12.     }
  13.    
  14.     public function initServer(){
  15.         $this->api->SetServer($this->host,$this->port);
  16.     }
  17.    
  18.     public function setIndex($index){
  19.         if(is_string($index)){
  20.             $this->index = $index;
  21.         }
  22.     }
  23.    
  24.     public function search($q,$limit=20,$rejectArray = array(),$SetArray = array(),$idRangeStart=0 ,$idRangeEnd=0 ,$sortQuery = '' ){
  25.         if($this->api =='' || $this->index=='') return false;
  26.         $this->api->ResetFilters();//重置过滤信息,把之前设置的过滤设置清楚
  27.         $this->api->SetIDRange($idRangeStart,$idRangeEnd);//设置文章id范围,两个参数都是0是指不设置
  28.         !empty($sortQuery) && $this->api->SetSortMode(SPH_SORT_EXTENDED,$sortQuery);//sql类排序模式,根据字段属性排序,具体用法找文档。默认值是指根据id进行降序排列
  29.         !empty($rejectArray)&&$this->api->SetFilter('@id',$rejectArray,true);//id过滤设置,过滤掉输入的rejectArray数组里面的id值,与普通的字段过滤不一样,id过滤需要携程"@id"
  30.         foreach($SetArray as $k => $v){
  31.             !empty($k) && !empty($v) && $this->api->SetFilter($k,$v);
  32.         }
  33.         $this->api->SetLimits(0,$limit);//设置获取文章的数量
  34.         $res = $this->api->Query($q,$this->index);
  35.         echo $q.$this->index;
  36.         return $res;
  37.     }
  38. }
复制代码
 楼主| 发表于 2017-2-10 00:49:12 | 显示全部楼层
原因找到了,sphinx自带的api是通过fsockopen链接服务的,host填localhost时,php会先查找dns。。。。猜测绕过了系统本身的host文件进行查找(这里还不确定,如果真的绕过了应该是找不到ip才对,但是最终却找到了),把localhost改成ip就可以了。。。。。有哪位大神知道这里填localhost会变慢的原因吗?
发表于 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 这种本地域名。

本版积分规则