【问题标题】:mysql - any way to help fulltext search with another index?mysql - 有什么方法可以帮助使用另一个索引进行全文搜索?
【发布时间】:2012-06-28 09:12:24
【问题描述】:

假设我有一个包含列的“文章”表:

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)

【问题讨论】:

  • 检查 author_id 列是否可以为空
  • 它不可为空(我认为这对这种情况更好。但如果不是,我当然也可以将其设为可空)
  • 不,不为空总是更好

标签: mysql full-text-indexing full-text-search


【解决方案1】:

好的,所以,因为

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

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 列,您在这里肯定需要索引)

最有效率的要由你自己决定,有一些测试用例,使用作者索引是否优于文本索引。

【讨论】:

  • 谢谢@Sebas。因此,仅在您提出建议的方式或我编写查询的初始方式时才需要索引合并。但是,使用 selfjoin,理论上 author_id 索引将仅用于连接 2 个表中的匹配行。那么希望全文可以在生成的过滤集上工作?
  • 根据 mysql 文档的说法,每个表引用只会使用一个索引。 where 子句被认为属于表引用,所以我们不能真正谈论resulting filtered set。在我们得到实际的resulting filtered set 之前评估 where 子句...不过我很想看看解释计划。
  • 添加了解释计划..也是的,我在数据库级别谈论resulting filtered set..仅取决于mysql尝试进行查找的顺序..它试图解释的计划是否你会 =)
  • 您用于测试的表的体积是多少?
猜你喜欢
  • 2011-04-20
  • 1970-01-01
  • 2011-10-08
  • 2015-09-21
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多