How to get the query plan from a prepared statment

眉间皱痕 提交于 2019-12-22 08:15:52

问题


I don't remember ever seeing a way to use prepared statements from the console and somehow don't think running an explain query thought as a prepared statement from the API will get what I want.

This is related to this old question of mine.

I'm primarily interested in MySQL but would be interested in other DBs as well.


回答1:


According to the brief research that I conducted, I don't see a way to get it. Ideally, the real execution plan would be generated once the variables are provided. Lookup tables can quickly eliminate actually running the query if a constant is not present. The ideal execution plan would take into account the frequency of occurrence. My understanding is that MySQL at least used to prepare an execution plan when the statement is prepared in order to validate the expression. Then, when you execute it, it generates another explain plan.

I believe the explain plan is temporarily housed in a table in MySQL but is quickly removed after it is used.

I would suggest asking on the MySQL internals list.

Good Luck,

Jacob




回答2:


"You can't"

https://dev.mysql.com/doc/internals/en/prepared-stored-statement-execution.html

That basically says that the execution plan created for the prepared statement at compile time is not used. At execution time, once the variables are bound, it uses the values to create a new execution plan and uses that one.

This means that if you want to know what it will do, you can take the query you intended on preparing, give it the values you will bind to it, and EXPLAIN PLAN that complete query.



来源:https://stackoverflow.com/questions/1320416/how-to-get-the-query-plan-from-a-prepared-statment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!