如何在 MySQL 中解释带参数的查询

发布于 2024-09-06 13:44:01 字数 121 浏览 6 评论 0原文

我有一个查询:

SELECT foo FROM bar WHERE some_column = ?

我可以在不填写参数值的情况下从 MySQL 获取解释计划吗?

I have a query

SELECT foo FROM bar WHERE some_column = ?

Can I get a explain plan from MySQL without filling in a value for the parameter?

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

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

发布评论

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

评论(3

烟花肆意 2024-09-13 13:44:02

只要您只执行等于(而不是类似,这可能会产生短路影响),只需将其替换为一个值即可:

EXPLAIN SELECT foo FROM bar WHERE some_column = 'foo';

由于它实际上并未执行查询,因此结果不应与实际结果不同。在某些情况下,情况并非如此(我已经提到过 LIKE)。以下是 LIKE 不同情况的示例:

SELECT * FROM a WHERE a.foo LIKE ?
  1. Param 1 == Foo - 如果索引存在,则可以使用索引扫描。
  2. Param 1 == %Foo - 即使索引存在也需要全表扫描
  3. Param 1 == Foo% - 可以使用索引扫描,具体取决于索引和其他因素

如果您要加入,则 where 子句会产生不可能的组合(因此会短路)。例如:

SELECT * FROM a JOIN b ON a.id = b.id WHERE a.id = ? AND b.id = ?

如果第一个和第二个参数相同,则有一个执行计划,如果不同,则会短路(并返回 0 行,没有命中任何数据)...

还有其他的,但这些都是我现在脑子里就能想到...

So long as you're doing only an equals (and not a like, which can have short circuit affects), simply replace it with a value:

EXPLAIN SELECT foo FROM bar WHERE some_column = 'foo';

Since it's not actually executing the query, the results shouldn't differ from the actual. There are some cases where this isn't true (I mentioned LIKE already). Here's an example of the different cases of LIKE:

SELECT * FROM a WHERE a.foo LIKE ?
  1. Param 1 == Foo - Can use an index scan if an index exists.
  2. Param 1 == %Foo - Requires a full table scan, even if an index exists
  3. Param 1 == Foo% - May use an index scan, depending on the cardinality of the index and other factors

If you're joining, and the where clause yields to an impossible combination (and hence it will short circuit). For example:

SELECT * FROM a JOIN b ON a.id = b.id WHERE a.id = ? AND b.id = ?

If the first and second parameters are the same, it has one execution plan, and if they are different, it will short circuit (and return 0 rows without hitting any data)...

There are others, but those are all I can think of off the top of my head right now...

无戏配角 2024-09-13 13:44:02

根据您输入的内容,解释计划可能会有所不同。我认为没有实际参数的解释计划没有任何意义。

The explain plan may be different depending on what you put in. I think explain plans without real parameter don't mean anything.

南薇 2024-09-13 13:44:02

我认为这是不可能的。
WHERE some_column ='value'WHERE some_column = other_columnWHERE some_column = (SELECT .. FROM a JOIN b JOIN c ... WHERE ... ORDER BY ... LIMIT 1 ) 返回不同的执行计划。

I don't think it's possible.
WHERE some_column ='value', WHERE some_column = other_column and WHERE some_column = (SELECT .. FROM a JOIN b JOIN c ... WHERE ... ORDER BY ... LIMIT 1 ) return different execution plans.

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