【发布时间】:2017-01-31 00:16:13
【问题描述】:
当我在布尔搜索中更改关键字的顺序时,我得到了相同的结果,但性能结果却大不相同。
在 MySQL 5.6.33 上使用 MyISAM 表 ft_min_word_len=2 和 description_index 作为 FULLTEXT 索引在 title 和 description 上的分析返回:
# Query 1
SELECT id
FROM archive, topic
WHERE topic.type=0 AND archive.status=2
AND MATCH(title, description) AGAINST ('+house* +tz*' IN BOOLEAN MODE)
AND archive.topicId = topic.id
ORDER BY archive.featured DESC, archive.submissionDate DESC LIMIT 0,20
结果:
Total count: 12
Key_read_requests: 2384607
Creating sort index: 7.950430 sec (!)
Duration: 8.851252 sec
# Query 2
SELECT id
FROM archive, topic
WHERE topic.type=0 AND archive.status=2
AND MATCH(title, description) AGAINST ('+tz* +house*' IN BOOLEAN MODE)
AND archive.topicId = topic.id
ORDER BY archive.featured DESC, archive.submissionDate DESC LIMIT 0,20
结果:
Total count: 12
Key_read_requests: 415
Creating sort index: 0.003449
Duration: 0.004054 sec
每个关键字的总记录数:
tz*: 135092
tz: 25596
house*: 12
两个查询的解释是一样的:
id | select_type | Table | Type | Key | Key len | Ref | Rows | Extra
1 | SIMPLE | archive | fulltext | description_index | 0 | | 1 | Using where; Using filesort
1 | SIMPLE | topic | eq_ref | PRIMARY | 3 | archive.topicId | 1 | Using where
这两个查询之间只有 Key_read_requests 和 Creating sort index 不同。
看来:
- 关键字顺序是一个关键的性能因素
- 关键字以相反的顺序使用
- 在末尾添加最具辨别力的关键字可以提高性能。
问题:
- 造成这种巨大性能差异的原因是什么?
- 什么是规则/最佳实践? (我在 mysql 的文档中找不到任何东西)。
【问题讨论】:
-
MyISAM 即将消失;看看你用 InnoDB 的 FULLTEXT 变体得到了什么。
-
@RickJames MyISAM 在某些情况下(我们的案例)仍然更好。我们正在等待似乎具有出色 innodb 性能的 mysql 8。在那之前,我们需要解决和理解这个问题。 :) percona.com/blog/2016/10/11/mysql-8-0-end-myisam
-
您能否为这两个查询发布
EXPLAIN的输出? -
你有
FULLTEXT索引吗?如果有,它的定义是什么? -
@Stoleg 似乎这个问题与全文如何以布尔模式分析字符串有关。我已经更新了这个问题。请注意,除了
Key_read_requests和Creating sort index的值之外,一切都是相同的(查询计划、值、解释等)。
标签: mysql optimization full-text-search