Mysql全文搜索和group by一起很慢

时间:2015-11-18 16:04:21

标签: mysql

我有一个包含以下索引的表:

PRIMARY KEY (`msg_id`),
KEY `ix_msg_user_id` (`msg_user_id`),
FULLTEXT KEY `ix_msg_text` (`text`)

如果我只进行正常的全文搜索,则速度很快并使用全文索引:

SELECT * FROM msg WHERE MATCH (msg_text) AGAINST ("some text" IN BOOLEAN MODE);

关于该查询的解释是:

+----+-------------+--------+----------+----------------+----------------+---------+------+------+-------------+
| id | select_type | table  | type     | possible_keys  | key            | key_len | ref  | rows | Extra       |
+----+-------------+--------+----------+----------------+----------------+---------+------+------+-------------+
|  1 | SIMPLE      | tweets | fulltext | ix_msg_text    | ix_msg_text    | 0       | NULL |    1 | Using where |
+----+-------------+--------+----------+----------------+----------------+---------+------+------+-------------+

现在,如果我执行相同的查询,但是添加一个组,则需要很长时间:

SELECT max(created_at) FROM msg WHERE MATCH (msg_text) AGAINST ("some text" IN BOOLEAN MODE) group by msg_user_id;

该查询的解释是:

 +----+-------------+--------+----------+----------------+----------------+---------+------+------+----------------------------------------------+
 | id | select_type | table  | type     | possible_keys  | key            | key_len | ref  | rows | Extra                                        |
 +----+-------------+--------+----------+----------------+----------------+---------+------+------+----------------------------------------------+
 |  1 | SIMPLE      | tweets | fulltext | ix_msg_text    |   ix_msg_text | 0       | NULL |    1 | Using where; Using temporary; Using filesort |
 +----+-------------+--------+----------+----------------+----------------+---------+------+------+----------------------------------------------+

我尝试添加另一个包含text和user_id的索引,如下所示:

ALTER TABLE ADD FULLTEXT INDEX `msg_msg_user_id` (`msg_text`, `msg_user_id`);
ERROR 1283 (HY000): Column 'msg_user_id' cannot be part of FULLTEXT index

但是,它不会让我失望,因为msg_user_id是一个int并且不能成为全文索引的一部分。加快查询速度的最佳方法是什么?

0 个答案:

没有答案