Unique
A unique node takes a sorted set of rows as an input and outputs one with all the duplicates removed. It can appear when using DISTINCT
and when UNION
is eliminating duplicates in its output. The output will be sorted the same as the input.
As of PostgreSQL 8.4, DISTINCT
will usually be implementing using a HashAggregate node instead of a Unique one, which on smaller tables can collect the unique values without having to explicitly sort them first.
EXPLAIN ANALYZE SELECT DISTINCT(state) FROM customers; QUERY PLAN ---------- HashAggregate (cost=726.00..726.51 rows=51 width=3) (actual time=91.950..92.048 rows=52 loops=1) -> Seq Scan on customers (cost=0.00..676.00 rows=20000 width=3) (actual time=0.009..49.466 rows=20000 loops=1) Total runtime: 92.319 ms
To see the old implementation in current PostgreSQL versions with something the size of the Dell Store data, it's easiest to just turn that optimization off:
SET enable_hashagg=off; EXPLAIN ANALYZE SELECT DISTINCT...