32 ms·
The code: WITH dt AS ( SELECT unnest(array[1, 2]) AS n ) SELECT * FROM dt; Is more complex than necessary. This produces the same result: SELE
by sbuttgereit 5y ago
The code:
WITH dt AS (
SELECT unnest(array[1, 2]) AS n
)
SELECT * FROM dt;
Is more complex than necessary. This produces the same result:
SELECT n FROM unnest(array[1, 2]) n;
┌───┐
│ n │
├───┤
│ 1 │
│ 2 │
└───┘
(2 rows)
I think I see some other opportunities as well.
I know the code is from a section dealing with CTEs, but CTEs aren't needed for every situation including things like using VALUES lists. Most people in the target audience can probably ignore this next point (probably on a newer version of PostgreSQL), but older versions of PostgreSQL would materialize the CTE results prior to processing the main query, which is not immediately obvious.
- simonw 5y agoI think that was a deliberate choice by the author to consistently demonstrate CTEs.
- sbuttgereit 5y agoSorry was editing to address this point probably while you were typing your response. It's well taken, but we should be clear that it's not required.