SQLite User Forum

Log Explain Query Plan with Python?
Login

Log Explain Query Plan with Python?

(1.1) By neurosis (neurosis69) on 2022-09-10 08:37:34 edited from 1.0 [link] [source]

Does anyone know if there is a way to log the queries explain plan(like .eqp) with python?

I know the python function set_trace_callback, which can be used to log all the SQL statements out. But when debugging, it could make sense to also see the chosen access plan by the optimizer.

(2) By anonymous on 2022-09-10 08:55:44 in reply to 1.1 [link] [source]

Have you tried just prefacing your SQL with EXPLAIN QUERY PLAN and then just following with your SQL and logging the response?

And then resubmit your SQL should you so desire.

(3) By neurosis (neurosis69) on 2022-09-10 13:18:32 in reply to 2 [link] [source]

I've tried that, but the SQL statement with EXPLAIN QUERY PLAN wasn't logged by the set_trace_callback function and I was looking for a more easy way to just turn it on/off on a session level.

But I haven't spent a lot of time yet to dig into the issue. Was just curious if there is a well known easy way to do it.

(4) By Simon Slavin (slavin) on 2022-09-10 19:39:28 in reply to 1.1 [link] [source]

What do you man by 'log' here ?

EXPLAIN QUERY PLAN returns a table, like a SELECT statement. If you execute it the way you would a SELECT statement, it should return the same data structure your library would get from any other query. Do what you like with it, including processing it in a callback.

However, SQLite does not do all the processing needed to neatly format what EXPLAIN QUERY PLAN returns every time you do a SELECT. If you want to know the query plan for every SELECT you do, you're going to have to execute two statements for every SELECT you do.

By the way, do not depend on the format of EXPLAIN QUERY PLAN in a production system. It's intended for programmers only. The things it returns can change between versions of SQLite.

(5.2) By Keith Medcalf (kmedcalf) on 2022-09-10 20:00:15 edited from 5.1 in reply to 4 [source]

Not to mention, of course, that the "actually executed" plan may not be the same as the "EXPLAIN QUERY PLAN" o r"EXPLAIN" plan for the same statement. You can "make them the same" by making sure STAT4 is not enabled and by making sure that the Query Planner Stability Guarantee mode is set for the connection (SQLITE_DBCONFIG_ENABLE_QPSG).