News for hackers. Stuff that matters.
Re:We Can Do Better Than SQL by marcinzm on Tuesday August 11, 2020 @09:38AM (#24120124)
>For example in Linq you can chain arbitrary many select/join/where/group by in arbitrary order. In SQL you need nested subqueries to achieve the same which is a much more convoluted syntax.

WITH statements alleviate some of this issue by allowing you to write subqueries in any order.

Re:We Can Do Better Than SQL by jimsparkman on Tuesday August 11, 2020 @09:43AM (#24120152)
WITH (CTEs) make queries so much more readable and digestible. As a programmer who now does data and SQL, I latched on to these as soon as I found I could reduce repetition in a query with them.
Re:We Can Do Better Than SQL by meritt on Tuesday August 11, 2020 @11:28AM (#24121351)
Make sure you understand what optimization fences are and how they affect your performance. CTEs are nice to read but routinely destroy the performance.

[1] https://thoughtbot.com/blog/advanced-postgres-performance-ti...

Re:We Can Do Better Than SQL by bjt on Tuesday August 11, 2020 @11:45AM (#24121585)
As of Postgres 12 this has changed substantially. Instead of being materialized, CTEs are inlined and optimized with the rest of the query.

Exceptions:

1. If the results of the CTE are used more than once then it is materialized by default, though you can override this by adding "NOT MATERIALIZED" to the call.

2. Recursive and INSERT/UPDATE/DELETE CTEs are always materialized.

https://paquier.xyz/postgresql-2/postgres-12-with-materializ...

Re:We Can Do Better Than SQL by oarabbus_ on Tuesday August 11, 2020 @01:33PM (#24123086)
>CTEs are nice to read but routinely destroy the performance.

This may be true on archaic versions of MySQL and Postgres but is not the case today, barring some esoteric edge cases (bugs) where the optimizer gets thrown out of whack. Once while doing data science consulting I rewrote a ~1000 line query in Aurora (MySQL flavored) which had a ~2.5s runtime, which was far too slow for the client's use-case.

After rewriting all the CTEs (there were many) into subqueries, there was a 2-3% increase in query speed, barely (on the order of under a tenth of a second). There was a very tiny improvement far smaller than the normal variance of the runtime.

Then I rebuilt the query and the joins, and was able to get the query to consistently run in the range of 0.8 - 1.2s. For my own purposes I then duplicated the query, re-implemented the CTEs, and did validate that indeed there is only a negligible increase in query time when using CTE.

Re:We Can Do Better Than SQL by 3pt14159 on Tuesday August 11, 2020 @10:10AM (#24120392)
For monster queries (thousands of lines of SQL) I find that temporary tables are also great. You can index them and, depending on your DB and its settings, they're usually held in RAM so they're super fast.
Re:We Can Do Better Than SQL by sumtechguy on Tuesday August 11, 2020 @10:40AM (#24120728)
That is implementation specific. In some db's putting an index on a temp table does nothing. In later versions of the same db it does but only in particular cases. Make sure you read the docs around that.
Re:We Can Do Better Than SQL by achr2 on Tuesday August 11, 2020 @10:26AM (#24120568)
CTEs do not get cached though, so they are actually quite bad for repetition without also using a temp table.
Re:We Can Do Better Than SQL by ComodoHacker on Wednesday August 12, 2020 @07:31AM (#24130918)
Depends on DBMS and version. Some are smart enough to pipeline/tie or materialize it internally.
Re:We Can Do Better Than SQL by pmarreck on Tuesday August 11, 2020 @10:20AM (#24120501)
Ecto.Query in the Elixir universe seems like it was inspired by linq
Re:We Can Do Better Than SQL by goto11 on Tuesday August 11, 2020 @02:18PM (#24123738)
WITH's are great and definitely solves some of the issues with SQL. Some implementations unfortunately don't optimize them as subqueries, but that is not the fault of SQL.