MySQL SELECT IN() 语句和 InnoDB 索引失败

发布于 2024-11-30 09:09:54 字数 626 浏览 1 评论 0原文

我有一个执行此操作的查询:

SELECT `threaded_comments`.* FROM `threaded_comments` WHERE `threaded_comments`.`parent_id` IN (4, 5)

我还在parent_id 列上设置了一个索引。

当我对查询执行 EXPLAIN SELECT 检查时,我得到以下信息:

`select_type`,`table`,`type`,`possible_keys`,`key`,`key_len`,`ref`,`rows`,`Extra`
'SIMPLE', 'threaded_comments', 'ALL', 'COMMENT_PARENT', NULL, NULL, NULL, 3, 'Using where'

看起来索引的 where 子句没有使用任何键。但是,当我执行简单的parent_id = 4 时,它就起作用了。但是当我执行parent_id=4 或parent_id=5 时,它会显示相同的消息。

我发现这与 innoDB 数据库表类型有关。 MySQL 似乎不喜欢在特定数据库引擎上执行 IN 操作。这可能是 EXPLAIN 工具的问题还是 InnoDB 缺少的东西?

I have a query that does this:

SELECT `threaded_comments`.* FROM `threaded_comments` WHERE `threaded_comments`.`parent_id` IN (4, 5)

I've also setup an INDEX on the parent_id column.

When I do a EXPLAIN SELECT check on the query I get the following information:

`select_type`,`table`,`type`,`possible_keys`,`key`,`key_len`,`ref`,`rows`,`Extra`
'SIMPLE', 'threaded_comments', 'ALL', 'COMMENT_PARENT', NULL, NULL, NULL, 3, 'Using where'

Looks like no keys are being used for the where clause of the index. However, when I do a simple parent_id = 4 then it works. But when I do parent_id=4 or parent_id=5 then it brings up the same message.

I figured out that it has todo with the innoDB database table type. MySQL seems to not like it when there are IN operations performed on that specific database engine. Could this be a problem with the EXPLAIN tool or it is something that InnoDB is missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剪不断理还乱 2024-12-07 09:09:54

不,它与 InnoDB 无关。您的 EXPLAIN 计划显示查询能够使用索引,但 MySQL 优化器决定不使用它。所选择的决定取决于许多因素。但后来当你的表变大时,优化器如果认为有用的话可以做出不同的决定。如果您想在该查询上显式使用索引,那么您可以尝试 力量指数

No, it has nothing to do with InnoDB. Your EXPLAIN plan shows that the query is able to use the index, but MySQL optimizer decided not to use it. The selected decision depends on many factors. But later when your table grows up, the optimizer can make a different decision if it thinks it's useful. If you want to explicitly use an index on that query then you may try to force index.

夜清冷一曲。 2024-12-07 09:09:54

看来表中的行数太少,最好进行全面扫描。

It seems that you have too less rows in tables and full scan is preferable.

你是年少的欢喜 2024-12-07 09:09:54

指数并不总是有好处。

来自文档

查询每个表索引,并使用最好的索引,除非优化器认为使用表扫描更有效。曾经,根据最佳索引是否跨越表的 30% 以上来使用扫描,但固定百分比不再决定使用索引还是扫描之间的选择。优化器现在更加复杂,并且根据其他因素进行估计,例如表大小、行数和 I/O 块大小。

在您的情况下,对于 parent_id = 4 来说索引被认为很好,但对于 parent_id IN (4, 5) 则首选表扫描

Index is not always a benefit.

From the documentation:

Each table index is queried, and the best index is used unless the optimizer believes that it is more efficient to use a table scan. At one time, a scan was used based on whether the best index spanned more than 30% of the table, but a fixed percentage no longer determines the choice between using an index or a scan. The optimizer now is more complex and bases its estimate on additional factors such as table size, number of rows, and I/O block size.

In your case, index is considered fine for parent_id = 4 but a table scan is preferred for parent_id IN (4, 5)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文