如何在 MySQL 中解释带参数的查询
我有一个查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要您只执行等于(而不是类似,这可能会产生短路影响),只需将其替换为一个值即可:
由于它实际上并未执行查询,因此结果不应与实际结果不同。在某些情况下,情况并非如此(我已经提到过 LIKE)。以下是
LIKE
不同情况的示例:Foo
- 如果索引存在,则可以使用索引扫描。%Foo
- 即使索引存在也需要全表扫描Foo%
- 可以使用索引扫描,具体取决于索引和其他因素如果您要加入,则 where 子句会产生不可能的组合(因此会短路)。例如:
如果第一个和第二个参数相同,则有一个执行计划,如果不同,则会短路(并返回 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:
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
:Foo
- Can use an index scan if an index exists.%Foo
- Requires a full table scan, even if an index existsFoo%
- May use an index scan, depending on the cardinality of the index and other factorsIf you're joining, and the where clause yields to an impossible combination (and hence it will short circuit). For example:
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...
根据您输入的内容,解释计划可能会有所不同。我认为没有实际参数的解释计划没有任何意义。
The explain plan may be different depending on what you put in. I think explain plans without real parameter don't mean anything.
我认为这是不可能的。
WHERE some_column ='value'
、WHERE some_column = other_column
和WHERE 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
andWHERE some_column = (SELECT .. FROM a JOIN b JOIN c ... WHERE ... ORDER BY ... LIMIT 1 )
return different execution plans.