【问题标题】:What's is the best way to create indexes for the query below为以下查询创建索引的最佳方法是什么
【发布时间】:2016-02-23 02:26:49
【问题描述】:

我有这个问题:

select * from `metro_stations`
where `is_active` = 1
  and (`title` like '%search%' or `title_en` like '%search%')

如果is_active 是 TINYINT 字段并且标题是 VARCHAR(255),如何创建有效索引?

那么这个查询呢:

select * from `metro_stations`
where `is_active` = 1
 and (`title` like '%search%' or
      `title_en` like '%search%' or
      `description` like '%search%')

如果描述字段是文本?

【问题讨论】:

标签: php mysql sql indexing


【解决方案1】:

对每一列使用全文索引。 如果在查询中使用“或”单独使用 fts 索引,如果使用“和”混合 fts 索引(在一个索引中使用多个列) Full Text Index

【讨论】:

  • 我为 title 和 title_en 字段添加了两个不同的全文索引,但查询:EXPLAIN EXTENDED select id from metro_stations where is_active = 1 and (title like "%мото% " 或 title_en 如 "%мото%") 似乎没有使用索引。怎么可能?
  • 从 metro_stations 中选择 id WHERE match(title) against('мото' IN BOOLEAN MODE)
  • 好吧,这是我的错误。谢谢!性能怎么样?这个决定比在没有任何索引的情况下使用 LIKE '%search%' 更好吗?
  • 全文搜索MySQL的FULLTEXT索引和LIKE查询性能分析makandracards.com/makandra/…
  • 阅读本文但不了解 InnoDB 和 MyISAM。我的表有 InnoDB 引擎。在这种情况下我可以使用索引吗?
【解决方案2】:
FULLTEXT(title, title_en)

WHERE is_active = 1
  AND MATCH(title, title_en) AGAINST ("+search" IN BOOLEAN MODE)

(这应该适用于 InnoDB (5.6+) 或 MyISAM。)

请记住 FULLTEXT 中“字长”和“停用词”的限制。

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多