This is probably the first time I post a blog that handles variants of
how to use SQL :)
I got the inspiration from reading some new and old blogs by Öystein Grövlen
where he mentioned various ways to rewrite queries as Common Table
Expressions (CTEs) and using windows functions instead of using subqueries.
I tried this and found that the rewritten queries was faster using windows in some
cases. Obviously the CTE variant and the subquery variant of the query can
be executed in the same way. But SQL implementations are not perfect and
are able to handle some constructs better than others.
This got me a bit puzzled, so I wanted to understand what specifically is the
thing that makes the CTEs and windows functions variant run faster.
Let's take Q17 in TPC-H as an example.
Here is the original …
[Read more]