Aggregate
Aggregate functions take in a series of values and produce a single output. Examples of aggregates are AVG()
, COUNT()
, EVERY()
, MIN()
, MAX()
, STDDEV()
, SUM()
, and VARIANCE()
. To compute an aggregate, all of the rows are typically read, then fed through the aggregate node to compute a result:
EXPLAIN ANALYZE SELECT max(zip) FROM customers; QUERY PLAN ---------- Aggregate (cost=726.00..726.01 rows=1 width=4) (actual time=75.168..75.169 rows=1 loops=1) -> Seq Scan on customers (cost=0.00..676.00 rows=20000 width=4) (actual time=0.007..32.769 rows=20000 loops=1) Total runtime: 75.259 ms
This isn't always the case though, because some values can be computed with indexes instead. Looking for the highest customerid
in the customers
table, where that's the primary key, doesn't have to look at every row:
EXPLAIN ANALYZE SELECT max(customerid) FROM customers; QUERY PLAN ---------- Result ...