mysql - any way to help fulltext search with another index?

谁说胖子不能爱 提交于 2020-01-01 05:35:07

问题


Let's say i have an "articles" table which has the columns:

article_text:  fulltext indexed
author_id:     indexed

now i want to search for a term that appears in an article that a particular arthor has written.

so something like:

select * from articles 
where author_id=54 
and match (article_text) against ('foo');

the explain for this query tells me that mysql is only going to use the fulltext index. I believe mysql can only use 1 index, but it sure seems like a wise idea to get all the articles a particular author has written first before fulltext searching for the term... so is there anyway to help mysql?

for example.. if you did a self-join?

select articles.* from articles as acopy 
                  join articles on acopy.author_id = articles.author_id 
where 
    articles.author_id = 54 
and match(article_text) against ('foo');

the explain for this lists the use of the author_id index first, then the fulltext search.

does that mean it's actually only doing the fulltext search on the limited set as filtered by author_id?

ADDENDUM

explain plan for the self join as follows:

*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: acopy
         type: ref
possible_keys: index_articles_on_author_id
          key: index_articles_on_author_id
      key_len: 5
          ref: const
         rows: 20
     filtered: 100.00
        Extra: Using where; Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: articles
         type: fulltext
possible_keys: index_articles_on_author_id,fulltext_articles
          key: fulltext_articles
      key_len: 0
          ref: 
         rows: 1
     filtered: 100.00
        Extra: Using where
2 rows in set (0.00 sec)

回答1:


Ok, so, since

Index Merge is not applicable to full-text indexes

http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html

I would try this approach: (replace author_id_index by the name of your index on author_id)

select * from articles use index (author_id_index)
where author_id=54 
and match (article_text) against ('foo');

Here the problem is the following:

  • it is indeed impossible to use a regular index in combination with a full-text index
  • if you join the table with itself, you are using an index already on each side of the join (the ON clause will use the author_id column, you definetly need the index here)

The most efficient has to be decided by you, with some test cases, whether using the author index is better than the text one.



来源:https://stackoverflow.com/questions/11217713/mysql-any-way-to-help-fulltext-search-with-another-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!