Pau*_*ber 6 mysql full-text-search
当我添加一些以布尔模式为条件的单词时,全文匹配忽略了它的索引。选择如下:
explain select * from seeds WHERE MATCH(text) AGAINST ("mount cameroon" IN BOOLEAN MODE);
Run Code Online (Sandbox Code Playgroud)
产出
+----+-------------+-------+----------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+----------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | seeds | fulltext | text | text | 0 | | 1 | Using where |
+----+-------------+-------+----------+---------------+------+---------+------+------+-------------+
Run Code Online (Sandbox Code Playgroud)
具有多个条件词的相同查询
explain select * from seeds WHERE MATCH(text) AGAINST ("mount cameroon" IN BOOLEAN MODE) = 4;
Run Code Online (Sandbox Code Playgroud)
产出
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | seeds | ALL | NULL | NULL | NULL | NULL | 9607 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
Run Code Online (Sandbox Code Playgroud)
这肯定不是正确的行为吗?
不幸的是,这是正确的行为。
当使用 FULLTEXT 索引时,MySQL 查询优化器很容易偏离主题。
我在 StackOverflow 上写了关于如何解决这个问题的文章。
您可能必须将原始查询嵌套在子查询中,并将 MATCH 函数作为列返回。然后,评估子查询外部的 MATCH 列。
而不是你的查询
select * from seeds WHERE MATCH(text) AGAINST ("mount cameroon" IN BOOLEAN MODE) = 4;
Run Code Online (Sandbox Code Playgroud)
你必须将其重构为如下所示:
SELECT B.* FROM
(
SELECT id,MATCH(text) AGAINST
("mount cameroon" IN BOOLEAN MODE) score
FROM seeds
WHERE MATCH(text) AGAINST
("mount cameroon" IN BOOLEAN MODE)
) A
INNER JOIN seeds B USING (id)
WHERE A.score = 4;
Run Code Online (Sandbox Code Playgroud)
试一试 !!!