Limit
Like everything else, query limits are built on top of existing scans that return a set of rows:
EXPLAIN ANALYZE SELECT customerid FROM customers LIMIT 10; QUERY PLAN ---------- Limit (cost=0.00..0.34 rows=10 width=4) (actual time=0.016..0.063 rows=10 loops=1) -> Seq Scan on customers (cost=0.00..676.00 rows=20000 width=4) (actual time=0.013..0.030 rows=10 loops=1) Total runtime: 0.117 ms
Note the actual rows output by the Seq Scan
here. This shows one of the aspects of query execution that isn't necessarily obvious. The way queries are carried out, the top node in the query plan is started, and it asks its children nodes for things on-demand. It's a top-down execution model; nodes only produce output when said output is required. In this case, the Seq Scan
on the customers table could have output as many as 20000 rows. But because the limit was reached after only 10 rows, that's all the Seq Scan
node...