6

假设我有一个“文章”表,其中包含以下列:

article_text:  fulltext indexed
author_id:     indexed

现在我想搜索一个出现在某个作者写的文章中的词。

所以像:

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

这个查询的解释告诉我mysql只会使用全文索引。我相信 mysql 只能使用 1 个索引,但在全文搜索该术语之前获取特定作者首先写的所有文章似乎是一个明智的主意......那么无论如何可以帮助 mysql 吗?

例如..如果您进行了自我加入?

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');

对此的解释首先列出了 author_id 索引的使用,然后是全文搜索。

这是否意味着它实际上只对由 author_id 过滤的有限集进行全文搜索?

附录

解释自我加入的计划如下:

*************************** 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)
4

1 回答 1

0

好的,所以,因为

索引合并不适用于全文索引

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

我会尝试这种方法:(替换author_id_index为您在 author_id 上的索引名称)

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

这里的问题如下:

  • 确实不可能将常规索引与全文索引结合使用
  • 如果将表与自身连接,则连接的每一侧都已使用索引(ON 子句将使用 author_id 列,您在这里肯定需要索引)

最有效的必须由您决定,通过一些测试用例,使用作者索引是否优于文本索引。

于 2012-06-27T00:44:52.587 回答