近期領導以爲網站的搜索太慢了。讓咱們優化一下。通過研究。感受以前使用的like查詢效率過低。因此致使查詢速度慢。從網上找到了sphinx。聽說這個軟件是作搜索用的。能夠大大提升效率。因爲以前歷來沒有接觸過。因此只能對着文檔進行研究。因爲咱們的網站是純英文的。因此只須要sphinx便可。針對中文作全文索引的軟件叫coreseek。最初我認爲sphinx就是一個數據庫。和mysql相似。只是比mysql效率高。通過研究,發現並非那麼回事。php
sphinx是對數據庫裏的表作索引。須要查詢的數據提交到sphinx進行查詢。返回在mysql數據庫裏的id,讓後拿id去mysql數據庫裏去查詢。mysql
一、先安裝sphinxclient
#cd /usr/local/src
#wget http://sphinxsearch.com/files/sphinx-0.9.9.tar.gz
#tar xzvf sphinx-0.9.9.tar.gz
#cd sphinx-0.9.9/api/libsphinxclient
#vim sphinxclient.c
找到
void sock_close ( int sock );
改成
static void sock_close ( int sock );
#./configure --prefix=/usr/local/sphinxclient
#make
#make install
二、安裝sphinx擴展
#wget http://pecl.php.net/get/sphinx-1.0.4.tgz
#tar xvzf sphinx-1.0.4.tgz
#cd sphinx-1.0.4
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/sphinxclient
#make
#make install
修改php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
[sphinx]
extension=sphinx.so
#vim sphinx.php
<?php
$s = new SphinxClient;
setServer("localhost", 9312);
$s->setMatchMode(SPH_MATCH_ANY);
$s->setMaxQueryTime(3);
$result = $s->query("demo");
var_dump($result);
?>
#/usr/local/php/bin/php sphinx.php 運行結果
array(9) {
["error"]=>
string(0) ""
["warning"]=>
string(0) ""
["status"]=>
int(0)
["fields"]=>
array(5) {
[0]=>
string(6) "cat_id"
[1]=>
string(13) "provider_name"
[2]=>
string(12) "goods_number"
[3]=>
string(18) "promote_start_date"
[4]=>
string(8) "keywords"
}
["attrs"]=>
array(8) {
["goods_sn"]=>
string(1) "3"
["goods_name"]=>
string(1) "3"
["brand_id"]=>
string(1) "1"
["goods_weight"]=>
string(1) "5"
["market_price"]=>
string(1) "5"
["shop_price"]=>
string(1) "5"
["promote_price"]=>
string(1) "5"
["gid"]=>
string(10) "1073741825"
}
["total"]=>
int(0)
["total_found"]=>
int(0)
["time"]=>
float(0)
["words"]=>
array(1) {
["demo"]=>
array(2) {
["docs"]=>
int(0)
["hits"]=>
int(0)
}
}
}