如何优化mysql全文联合搜索?

时间:2011-04-19 19:18:10

标签: php mysql full-text-search

我正在进行mysql全文搜索。

我的数据库表article1包含~18000篇文章,article2包含~7000篇文章,article3包含~13000篇文章。 FIELD cat是一个INDEX字段

现在我想进行联合搜索。将5组单词放入3表中,匹配结果。但是处理时间是3.1213495136 seconds。 (我添加microtime()以查看它将花费多少时间)。有没有办法优化mysql全文联合搜索?谢谢。

(SELECT title,content,date FROM article1 WHERE 
(cat='novel' AND MATCH (title,content) AGAINST ('+Mary +Barnard' IN BOOLEAN MODE)) 
OR 
(cat='novel' AND MATCH (title,content) AGAINST ('+Patricia +Beer' IN BOOLEAN MODE)) 
OR 
(cat='novel' AND MATCH (title,content) AGAINST ('+Aphra +Behn' IN BOOLEAN MODE)) 
OR 
(cat='novel' AND MATCH (title,content) AGAINST ('+Judy +Blume' IN BOOLEAN MODE)) 
OR 
(cat='novel' AND MATCH (title,content) AGAINST ('+Elizabeth +Bowen' IN BOOLEAN MODE)))
UNION 
(SELECT title,content,date FROM article2 WHERE 
(MATCH (title,content) AGAINST ('+Mary +Barnard' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content) AGAINST ('+Patricia +Beer' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content) AGAINST ('+Aphra +Behn' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content) AGAINST ('+Judy +Blume' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content)AGAINST ('+Elizabeth +Bowen' IN BOOLEAN MODE)))
UNION 
(SELECT title,content,date FROM article3 WHERE 
(MATCH (title,content) AGAINST ('+Mary +Barnard' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content) AGAINST ('+Patricia +Beer' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content) AGAINST ('+Aphra +Behn' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content) AGAINST ('+Judy +Blume' IN BOOLEAN MODE)) 
OR 
(MATCH (title,content)AGAINST ('+Elizabeth +Bowen' IN BOOLEAN MODE)))
Order By date DESC LIMIT 10

2 个答案:

答案 0 :(得分:3)

你应该做的第一件事是添加

Order By date DESC LIMIT 10 

到每个子查询中,因为最后不需要超过10个结果。

所有表格中的字段“date”也必须有索引。

alter table "TABLENAME" add index date_idx(date);

此外:

您可以通过更改搜索字词来缩短并略微加快查询速度:“()|()”

(SELECT title,content,date FROM article1 WHERE 
(cat='novel' AND MATCH (title,content) AGAINST ('(+Mary +Barnard) | (+Patricia +Beer) | (+Aphra +Behn) | (+Judy +Blume) | (+Elizabeth +Bowen)' IN BOOLEAN MODE)) 
Order By date DESC LIMIT 10)
UNION 
(SELECT title,content,date FROM article2 WHERE 
(MATCH (title,content) AGAINST ('(+Mary +Barnard) | (+Patricia +Beer) | (+Aphra +Behn) | (+Judy +Blume) | (+Elizabeth +Bowen)' IN BOOLEAN MODE)) 
Order By date DESC LIMIT 10)
UNION 
(SELECT title,content,date FROM article3 WHERE 
(MATCH (title,content) AGAINST ('(+Mary +Barnard) | (+Patricia +Beer) | (+Aphra +Behn) | (+Judy +Blume) | (+Elizabeth +Bowen)' IN BOOLEAN MODE)) 
Order By date DESC LIMIT 10)
Order By date DESC LIMIT 10

答案 1 :(得分:1)

你工会的另一种选择是将这些作为单独的搜索进行。如果您按相关性订购标准,则第一套是最好的;返回部分结果,仅当少于10时,尝试下一场比赛等。

然后查询对最终用户来说很快,以相关顺序返回结果,并且不会浪费MySQL资源获取不需要的数据。如果用户要求更多,则可以运行更长,效率更低的查询,但这样有意义。