简体   繁体   English

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

[英]How to optimize mysql fulltext union search?

I am making a mysql fulltext search. 我正在进行mysql全文搜索。

my database table article1 has ~18000 articles, article2 has ~7000 articles, article3 has ~13000 articles. 我的数据库表中article1~18000条, article2~7000条, article3~13000篇。 FIELD cat is a INDEX field FIELD cat是INDEX字段

Now I want to make a union search. 现在,我要进行联合搜索。 there are 5 groups words put into 3 table, match out the results. 将5组单词放入3个表中,以匹配结果。 But the process time is 3.1213495136 seconds . 但是处理时间为3.1213495136 seconds (I add microtime() to see how much time it will cost). (我添加了microtime()以查看将花费多少时间)。 Is there any way to optimize mysql fulltext union search? 有什么方法可以优化mysql全文联合搜索吗? Thanks. 谢谢。

(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

First thing you should do is to add 您应该做的第一件事是添加

Order By date DESC LIMIT 10 

into each subquery as you don't need more than 10 results in the end. 进入每个子查询,因为最后不需要超过10个结果。

There also must be indexes on fields "date" in all tables. 所有表中的“日期”字段上也必须有索引。

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

ADDITIONALLY: 另外:

You may shorten and slightly speed it query by changing search terms to form: "() | ()" 您可以通过将搜索词更改为以下形式来缩短并稍微加快查询速度:“()|()”

(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

An alternative to your union would be to carry these out as separate searches. 工会的替代方法是将这些作为单独的搜索进行。 If you order your criteria by relevance, the first set is your best; 如果您按相关性排序标准,则第一组是最好的。 return partial results and only if there's fewer than 10, try the next match, etc. 仅返回少于10的部分结果,请尝试下一场比赛,依此类推。

Then the query is fast for the end-user, return results in relevance order, and don't waste MySQL resources getting unwanted data. 然后,查询对于最终用户来说是快速的,以相关顺序返回结果,并且不会浪费MySQL资源获取不需要的数据。 If the user asks for more, a longer, inefficient query can run, but is meaningful then. 如果用户要求更多,则可以运行更长,效率低下的查询,但那才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM