数据表
1、documents
CREATE TABLE `documents` (
`id` int(13) NOT NULL auto_increment,
`group_id` int(11) NOT NULL,
`group_id2` int(11) NOT NULL,
`date_added` datetime NOT NULL,
`title` varchar(255) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`),
KEY `title` (`title`),
FULLTEXT KEY `content` (`content`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=500006 ;
2、Sphinx
CREATE TABLE `sphinx` (
`id` int(13) NOT NULL,
`weight` int(11) NOT NULL,
`query` varchar(255) NOT NULL,
KEY `Query` (`query`)
) ENGINE=SPHINX DEFAULT CHARSET=utf8;
本次测试,在sphinx.conf中共建立三个索引。
1、dramx 对 documents表的title、content字段均建立索引。
2、dramx_title 对 documents表的title字段建立索引。
3、dramx_content 对 documents表的content字段建立索引。
三个索引在具体测试时,分别被用到。
程序调用Sphinx全文检索引擎有两种方式:
一、Php调用 API
二、安装SphinxSE.
安装方法:
1、 在官网下载mysql-5.0.45-sphinxse-r871-win32.zip解压。
2、 停掉本地mysql所有服务。并将解压后的文件覆盖到mysql相应目录。
3、 启动mysql ,建立一个至少含有三个字段的表(sphinx),字段数据类型依次是int、int、varchar,
Storage Engine选择sphinx即可。
4、 查询时结合表sphinx进行联合查询。
下面先将php+mysql和 Php+ Sphinx 通过like 、order by 、group by进行测试比较。最后单独给出使用第二种方法SphinxSE的查询测试。
测试环境:
内存:1GB 、CPU:intel pentium(R) dual CPU E2140
数据量:50万条
测试项目 | Php+Mysql | Php + Sphinx 全文检索引擎 | ||
所花时间(秒) | 代码 | 所花时间 | 代码 | |
Title like ‘%Fm5%’ | 0.93 | mysql_query('set names utf8'); $sql = "select title from documents where title like '%Fm5%'"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; | 0.012 | $cl = new SphinxClient (); //设置连接Sphinx主机名与端口 $start = getmicrotime(); $cl->SetServer('localhost',3312); $cl->SetMatchMode(SPH_MATCH_ALL); //搜索模式均采用匹配所有查询词模式 $cl->SetLimits(0,100000); $res = $cl->Query('Fm5', 'dramx_title'); $end = getmicrotime(); print_r($res); print $end-$start; |
Content Like ‘%中国LED照明市场%’ | 11.75 | mysql_query('set names utf8'); $sql = "select title from documents where content like '%中国LED照明市场%'"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; | 0.020 | $cl = new SphinxClient (); //设置连接Sphinx主机名与端口 $start = getmicrotime(); $cl->SetServer('localhost',3312); $cl->SetMatchMode(SPH_MATCH_ALL); $cl->SetLimits(0,100000); $res = $cl->Query('中国LED照明市场', 'dramx_content'); $end = getmicrotime(); print_r($res); print $end-$start; |
MATCH (content) AGAINST ('无线网卡厂商') | 0.93 | mysql_query('set names utf8'); $sql = "select title from documents where MATCH (content) AGAINST ('无线网卡厂商')"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; | 0.020 | $cl = new SphinxClient (); //设置连接Sphinx主机名与端口 $start = getmicrotime(); $cl->SetServer('localhost',3312); $cl->SetMatchMode(SPH_MATCH_ALL); $cl->SetLimits(0,100000); $res = $cl->Query('无线网卡厂商', 'dramx_content'); $end = getmicrotime(); print_r($res); print $end-$start; |
MATCH (content) AGAINST ('產業資訊') 并按照 id 排序(Order By id desc) | 1.50 | mysql_query('set names utf8'); $sql = "select title from documents where MATCH (content) AGAINST ('產業資訊') order by id desc"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; | 0.031 | $cl = new SphinxClient (); //设置连接Sphinx主机名与端口 $start = getmicrotime(); $cl->SetServer('localhost',3312); $cl->SetMatchMode(SPH_MATCH_ALL); $cl->SetSortMode(SPH_SORT_EXTENDED, 'id desc'); $cl->SetLimits(0,100000); $res = $cl->Query('產業資訊', 'dramx'); $end = getmicrotime(); print_r($res); print $end-$start; |
MATCH (content) AGAINST ('专题报道') 并根据group_id分组(Group By group_id ) | 1.01 | mysql_query('set names utf8'); $sql = "select title from documents where MATCH (content) AGAINST ('专题报道') group by group_id"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; | 0.081 | $cl = new SphinxClient (); //设置连接Sphinx主机名与端口 $start = getmicrotime(); $cl->SetServer('localhost',3312); $cl->SetMatchMode(SPH_MATCH_ALL); $cl->SetGroupBy('group_id', SPH_SORT_ATTR_DESC,"@group desc "); $cl->SetLimits(0,100000); $res = $cl->Query('专题报道台', 'dramx'); $end = getmicrotime(); print_r($res); print $end-$start; |
Content Like ‘%中国LED照明市场%’ order by id desc | 52.54 | mysql_query('set names utf8'); $sql = "select title from documents where content like '%中国LED照明市场%' order by id desc"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; | 0.031 | $cl = new SphinxClient (); //设置连接Sphinx主机名与端口 $start = getmicrotime(); $cl->SetServer('localhost',3312); $cl->SetMatchMode(SPH_MATCH_ALL); $cl->SetSortMode(SPH_SORT_EXTENDED, 'id desc'); $cl->SetLimits(0,100000); $res = $cl->Query(中国LED照明市场', 'dramx'); $end = getmicrotime(); print_r($res); print $end-$start; |
Content Like ‘%国际Dram报价平台%’ group by group_id desc | 40.05 | $sql = "select title from documents where content like '%中国LED照明市场%' group by group_id"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; | 0.081 | $cl = new SphinxClient (); //设置连接Sphinx主机名与端口 $start = getmicrotime(); $cl->SetServer('localhost',3312); $cl->SetMatchMode(SPH_MATCH_ALL); $cl->SetGroupBy('group_id', SPH_SORT_ATTR_DESC,"@group desc "); $cl->SetLimits(0,100000); $res = $cl->Query('国际Dram报价平台', 'dramx'); $end = getmicrotime(); print_r($res); print $end-$start; |
针对php+mysql的查询,mysql-nt进程占用CPU的峰值最高到50%, 一般在20%之间,而php+Sphinx由于查询时根本没有用到mysql引擎而是查询自身的索引引擎,所以,在查询上CPU的占用可以忽略。
SphinxSE性能测试
测试项目 | SphinxSE | |
所花时间(秒) | 代码 | |
Title 包含 ‘Dramexchange 国际Dram报价平台’ | 0.045 | mysql_query('set names utf8'); $sql = "select c.* from documents as c,sphinx as t where c.id=t.id and t.query='@title 国际Dram报价平台;mode=extended'"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; |
Content 包含 国际Dram报价平台 | 0.043 | mysql_query('set names utf8'); $sql = "select c.* from documents as c,sphinx as t where c.id=t.id and t.query='@content 国际Dram报价平台;mode=extended'"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; |
Title、Content 包含无线网卡厂商 | 0.016 | mysql_query('set names utf8'); $sql = "select c.* from documents as c inner join sphinx as t on c.id=t.id where t.query='包含无线网卡厂商'"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; |
Title、Content 包含’中国LED照明市场’并按照group_id排序 | 0.052 | mysql_query('set names utf8'); $sql = "select c.* from documents as c inner join sphinx as t on c.id=t.id where t.query='中国LED照明市场;mode=extended;sort=extended:group_id desc'"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; |
Title、Content 包含’中国LED照明市场’并按照group_id 分组 | 0.052 | mysql_query('set names utf8'); $sql = "select c.* from documents as c inner join sphinx as t on c.id=t.id where t.query='中国LED照明市场;mode=extended;groupby=attr:group_id'"; $start = getmicrotime(); $result = mysql_query($sql) or die(mysql_error()); $end = getmicrotime(); print mysql_num_rows($result).'<br>'; print $end-$start; |
谢谢!