On Fri, 9 Sep 2022 09:46:08 -0700 (PDT), Pieter Roggemans wrote:
> I don't know if it can help you in this specific case, and I didn't
> test it, but when using subselects sqlite tends to rewrite the
> queries
> by "flattening" them... possibly with the union's you get a similar
> behaviour
>
> You can "nudge" the query optimizer not to flatten subqueries by
> using
> LIMIT -1 OFFSET 0. As an example, a query where this construct is
> used
> can be found here:
>
>
https://github.com/geofileops/geofileops/blob/master/geofileops/util/_geoops_sql.py#L1080
>
Pieter,
this is very interesting to know.
going in further depth: SQLite isn't really a DBMS (and this
is pretty obvious, just consider how microscopic is its
binary code).
in effects it's a virtual machine interpreting a special
set of op-codes specifically intended to support very
low-level data access primitives.
The Query Optimizer module is kind of a compiler:
it parses SQL statements checking for correctness,
and then translating it in a small program based
on op-codes that finally will be executed.
this is an astonishing architecture usually ensuring
very fast performances.
but if the Query Planner becomes confused you'll surely
get a deadly slow query; and one of the surest methods
for confusing it is writing too much complex
SQL expressions.
and this explains why breaking some huge query into
many smaller and simples queries usually ensures
an impressing performance boost.
-------------------------------
that's not all: one the first actions of the Query
Planer is to _REWRITE_ your SQL, so to flatten and
simplify the syntax as much as possible.
short conclusione: what you've carefully coded
and whar is actually executed not necessarily
is the same.
final critical details.
first of all the Query Optimizer can decide to
automatically create some temporaty index whenever
it thinks to be usefull; but this could be a very
dangerous option because can completely distort
your real intentions.
second critical point: the RTree Spatial Index
is often paramount for fast Spatial Queries.
but for the Query Planner an RTree isn't at all
an _INDEX_, it's just another _TABLE_ as any other.
and this explains why too much complex SQL statements
could easily ignore the Spatial Index, thus leading
to deadly slowliness.
---------------------------
your best friends.
regularly use EXPLAIN and EXPLAIN QUERY PLAN
in order to check how your SQL will be actually
translated into a sequece of op-codes before
going under execution.
it could be boring and difficult, but the
secret of your success with SQLite is here.
never forget: SQLite isn't a DBMS as any other,
it's something of radically different.
writing a SQL query in a form or another, although
syntactically equivalent, could easily have a
dramatic impact on performances.
bye Sandro