简体   繁体   English

MYSQL GROUP BY未使用FULLTEXT SEARCH返回任何结果

[英]MYSQL GROUP BY returns no results with FULLTEXT SEARCH

I have the following MYSQL schema: 我有以下MYSQL模式:

CREATE TABLE IF NOT EXISTS `tag` (
   `id` SMALLINT UNSIGNED NOT NULL,
   `tag` VARCHAR(15) NOT NULL,
   FULLTEXT INDEX(`tag`),
   PRIMARY KEY (`id`,`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `tag`(`id`,`tag`) VALUES
('1','motor'),
('2','motor');

I want to select rows from tag table grouped by tag column. 我想选择行tag按分组表tag栏。 I ran the following command: 我运行了以下命令:

SELECT COUNT(id),tag FROM tag
GROUP BY tag

The expected result: 预期结果:

COUNT(id) | tag
------------------
    2     | motor

The actual result: no rows returned 实际结果: no rows returned

If I remove the FullText Index from the table, the results return as expected. 如果我删除FullText从表中的索引,返回的结果如预期。 I don't know what is going wrong when using fulltext with group 我不知道将fulltextgroup使用时出了什么问题

Update With further research the problem seems to be from the composite primary key. 更新随着进一步的研究,问题似乎出在复合主键上。 If I switch to one-column primary key, query works again but I need to use a composite key for this table as the same id could have multiple tags. 如果我切换到单列主键,查询将再次起作用,但是我需要为此表使用复合键,因为相同的ID可能具有多个标签。

I created an SQL fiddle for you to try: http://sqlfiddle.com/#!9/1f765d/1/0 我创建了一个SQL提琴供您尝试: http : //sqlfiddle.com/#!9/1f765d/1/0

Finally discovered the problem. 终于发现了问题。 The engine has 2 indices to choose from when executing the query, which ends up not using any at all and returning no results. 引擎在执行查询时有2个索引可供选择,最终将完全不使用任何索引,并且不返回任何结果。

It's very likely that is a bug. 这很可能是一个错误。 This is a case where FORCE INDEX comes in handy as a workaround. 在这种情况下, FORCE INDEX可以派上用场。

So the final working command: 所以最后的工作命令是:

SELECT COUNT(id),tag FROM tag
FORCE INDEX(PRIMARY)
GROUP BY tag

And this is a fiddle with the updated code: http://sqlfiddle.com/#!9/a8568/21/0 这是一个带有更新代码的小提琴: http : //sqlfiddle.com/#!9/a8568/21/0

Thanks everyone! 感谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM