JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.05k stars 676 forks source link

feat: EXPOSED-238 Support EXPLAIN statements #2022

Closed bog-walk closed 4 months ago

bog-walk commented 4 months ago

explain() takes any valid SELECT, INSERT, UPDATE, DELETE etc statement in its lambda body. It blocks the execution of this internal statement and uses it to prepare an EXPLAIN query, which returns a result set of query execution path details (very different depending on database). It acts like a standard Query in that it will not be executed until it is iterated over in some way:

val result = explain {
    Countries.select(Countries.id).where { Countries.code like "A%" }
}.toList()

println(result)
// PostgreSQL example
// QUERY PLAN=Seq Scan on countries  (cost=0.00..25.38 rows=6 width=4)
// QUERY PLAN=  Filter: ((country_code)::text ~~ 'A%'::text)

explain() blocks execution of the lambda statement by using 2 new Transaction properties. 1 of these properties makes sure that the lambda statement is captured so it can be passed to the ExplainQuery constructor, since not all statement functions return Statement<*> (namely update() and delete() return numbers).

Next steps:

Not supported:

bog-walk commented 4 months ago

Noted! One of the reasons I don't think an attempt should be made to define/determine the output in a restrictive way. The user being able to print the potentially changing output will hopefully suffice for now.