3 ms·
You can always macroexpand and debug that. CTEs exist for very good reasons. Chief among those is that they make recursive queries easy to express and underst
by cryptonector 4y ago
You can always macroexpand and debug that.
CTEs exist for very good reasons. Chief among those is that they make recursive queries easy to express and understand. But also because it lets you have one command that implements what might look like temp table/view creation, population of those tables, run various queries, and delete said temp schema, but the optimizer gets to see what all you are doing and optimize accordingly (there might be no temp tables in the optimized query). As well CTEs help with readability by letting you abstract sub-queries and outdent them.
CTEs do have some downsides, mainly that if you want to reuse them across statements, well, you can't.
> using macros with SQL is like using python script to autogenerate classes and interfaces in Java for $my_class and $my_interface, instead of having a formally defined hierarchy of classes and interfaces with methods and signatures
We live in a world full of generated code. The alternative is much worse, so we have to be able to debug through generated code.
- slt2021 4y agoI agree that CTEs can help compose complex query from several ingredient queries. But once several different queries start sharing common CTE ingredients - this is an indicator that you are relying on implicit(informal) schema, which should be formalized and formalized. Just take your CTE ingredient queries and declare them as views, isolated to your namespace. And let everyone reuse your views, instead of copy-pasting CTE ingredients, or using code-generation to achieve the same. The benefit is single source of truth - there will be only one definition of CTE subquery, and it can evolve/extend independently while letting everyone reuse your parts. I assure you - if you take your CTE with 4 subqueries, and instead create 4 views, the query plan will be the same regardless of using CTEs with code-generation or using views. The benefit is you dont have to use codegeneration, each view will be testable/verifiable