Live data from Hacker News

Modern SQL: With – Organize Complex Queries

modern-sql.com

21–30 of 63 posts

Re: Modern SQL: With – Organize Complex Queries

#21
post #12

Very interesting site, but does anyone know why Oracle and mysql have a so poor support of the standard ? It looks as if postgres was the only one trying to comply.

How do you feel Oracle's support for "with" is lacking? I use it on a daily basis, and it seems to work fine for me.

Re: Modern SQL: With – Organize Complex Queries

#22
post #3

What I really want from SQL is the ability to do a single select with one or more parts of each returned record being an array (defined by a one-to-many join) and others simple scalars. It looks like Postgres can do it [1] with its JSON capabilities, and to be honest its being a little while since I've dug into it, so things might have changed (I'm actually not even sure I'm phrasing the question correctly), but the…

> It looks like Postgres can do it [1] with its JSON capabilities

Postgres could do it long before its JSON capabilities, array_agg was added in Postgres 8.4 (2009) and hstore in 8.3 (2008)

Re: Modern SQL: With – Organize Complex Queries

#23
post #3

What I really want from SQL is the ability to do a single select with one or more parts of each returned record being an array (defined by a one-to-many join) and others simple scalars. It looks like Postgres can do it [1] with its JSON capabilities, and to be honest its being a little while since I've dug into it, so things might have changed (I'm actually not even sure I'm phrasing the question correctly), but the…

You mean like

    SELECT key, value1,
      ARRAY(SELECT value2 FROM bar WHERE bar.key = foo.key)
        AS value2_array
    FROM foo;
?

(In Postgres; no "JSON capabilities" needed.)

Re: Modern SQL: With – Organize Complex Queries

#24
post #3

What I really want from SQL is the ability to do a single select with one or more parts of each returned record being an array (defined by a one-to-many join) and others simple scalars. It looks like Postgres can do it [1] with its JSON capabilities, and to be honest its being a little while since I've dug into it, so things might have changed (I'm actually not even sure I'm phrasing the question correctly), but the…

> It looks like Postgres can do it [1] with its JSON capabilities Postgres could do it long before its JSON capabilities, array_agg was added in Postgres 8.4 (2009) and hstore in 8.3 (2008)

Also, ARRAY(), since 7.4 in 2003.

Re: Modern SQL: With – Organize Complex Queries

#25
post #12

Very interesting site, but does anyone know why Oracle and mysql have a so poor support of the standard ? It looks as if postgres was the only one trying to comply.

In oracle's case "poor support" seems to imply being more lenient than required (not needing recursive, allowing qualified names for CTE), whereas Postgres is just more strict.

Which seems to be a general with those two DBMS'...

Re: Modern SQL: With – Organize Complex Queries

#26

If your query is complex enough to warrant a (non-recursive) CTE, it's probably complex enough to warrant breaking that CTE out into a separate view (or function, if it performs a write). Now you can independently test and reuse that view, which you can't do with a CTE. (Bonus if you're in Postgres: Postgres optimizes across views, but not across CTEs. Go figure.)

For many of my use cases, this is a handy workflow. CTEs are just perfect for iteratively building up a query to what I need. Once it's done, I can either package the whole shebang or break out the really useful bits into views.

Notably, MSSQL does optimize across CTEs, so there's not as much pressure to break out to views on that platform. And if you're feeling especially perverse, you can make a CTE a view, which sorta... sporks the problem.

Re: Modern SQL: With – Organize Complex Queries

#27

If your query is complex enough to warrant a (non-recursive) CTE, it's probably complex enough to warrant breaking that CTE out into a separate view (or function, if it performs a write). Now you can independently test and reuse that view, which you can't do with a CTE. (Bonus if you're in Postgres: Postgres optimizes across views, but not across CTEs. Go figure.)

Disagree completely, CTEs and local temp/variable tables can serve an important purpose. I do think that a fair amount of CTEs can just be inner queries (not joins, well, joined queries...).

Re: Modern SQL: With – Organize Complex Queries

#28

Updatable CTEs are great, for example if you want to set a column to 1 where the date is the latest day for each value in a different column, you would do something like this ;WITH cte AS(SELECT ROW_NUMBER() OVER(PARTITION BY SomeVal ORDER BY SomeDate DESC) AS row, * FROM SomeTable) UPDATE cte SET id = CASE WHEN row = 1 THEN 1 ELSE 0 END Here is the DDL and DML in case you want to play around with this CREATE TABLE S…

So I've never come across INSERT table SELECT values UNION ALL SELECT values.

Any reason to prefer that over INSERT INTO table (col, col) VALUES (v1, v2), (v1, v2), ... ?

Re: Modern SQL: With – Organize Complex Queries

#29
post #17
post #12

Very interesting site, but does anyone know why Oracle and mysql have a so poor support of the standard ? It looks as if postgres was the only one trying to comply.

Have you read the SQL99 or even 2003 spec? No database supports everything. It's a matter which features one cherrypicks. If you want to use lots of XML and ORMs inside your database or use Java as stored procedures than SQL 2003 spec is great. The enterprise grade database software Oracle, DB2, MSSQL and Postgres are all somewhat comparable. MySQL with e.g. its InnoDB storage engine is very fast and web scale. Whoul…

I think you're glossing over some important differences there. Some databases really are more standards-compliant than others, and that Postgres would be more standards-compliant than Oracle or MySQL is 100% consistent with my experience.

For some use cases that's fine, sure. But I've also seen many developers come to regret choosing MySQL when, months or years down the line, they ran into issues that hadn't been apparent at first. And honestly I don't see how it's a "tradeoff" when Postgres can offer performance that's every bit the equal of MySQL.

Post reply on HN