By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
CREATE TABLE
INSERT 0 200000
ALTER TABLE
CREATE INDEX
VACUUM
metric | bytes | bytes_pretty | bytes_per_row |
---|---|---|---|
core_relation_size | 18636800 | 18 MB | 93 |
visibility_map | 8192 | 8192 bytes | 0 |
free_space_map | 24576 | 24 kB | 0 |
table_size_incl_toast | 18677760 | 18 MB | 93 |
indexes_size | 10838016 | 10 MB | 54 |
total_size_incl_toast_and_indexes | 29515776 | 28 MB | 147 |
live_rows_in_text_representation | 13924345 | 13 MB | 69 |
------------------------------ | null | null | null |
live_tuples | 200000 | null | null |
dead_tuples | 0 | null | null |
SELECT 10
SELECT 86369
VACUUM
customer | avg_rows |
---|---|
86369 | 2.3156456599011219 |
SELECT 1
QUERY PLAN |
---|
Subquery Scan on cte (cost=0.42..12088.42 rows=1000 width=12) (actual rows=86369 loops=1) |
Filter: (cte.rn = 1) |
Rows Removed by Filter: 113631 |
-> WindowAgg (cost=0.42..9588.42 rows=200000 width=20) (actual rows=200000 loops=1) |
-> Index Only Scan using purchases_3c_idx on purchases (cost=0.42..6088.42 rows=200000 width=12) (actual rows=200000 loops=1) |
Heap Fetches: 0 |
Planning Time: 0.225 ms |
Execution Time: 180.625 ms |
EXPLAIN
QUERY PLAN |
---|
Subquery Scan on sub (cost=0.42..12088.42 rows=1000 width=12) (actual rows=86369 loops=1) |
Filter: (sub.rn = 1) |
Rows Removed by Filter: 113631 |
-> WindowAgg (cost=0.42..9588.42 rows=200000 width=20) (actual rows=200000 loops=1) |
-> Index Only Scan using purchases_3c_idx on purchases (cost=0.42..6088.42 rows=200000 width=12) (actual rows=200000 loops=1) |
Heap Fetches: 0 |
Planning Time: 0.122 ms |
Execution Time: 173.662 ms |
EXPLAIN
QUERY PLAN |
---|
Result (cost=0.42..6588.42 rows=69015 width=12) (actual rows=86369 loops=1) |
-> Unique (cost=0.42..6588.42 rows=69015 width=12) (actual rows=86369 loops=1) |
-> Index Only Scan using purchases_3c_idx on purchases (cost=0.42..6088.42 rows=200000 width=12) (actual rows=200000 loops=1) |
Heap Fetches: 0 |
Planning Time: 0.169 ms |
Execution Time: 50.946 ms |
EXPLAIN
QUERY PLAN |
---|
Sort (cost=57.15..57.40 rows=101 width=12) (actual rows=86369 loops=1) |
Sort Key: cte.customer_id |
Sort Method: external merge Disk: 1864kB |
CTE cte |
-> Recursive Union (cost=0.42..51.77 rows=101 width=12) (actual rows=86369 loops=1) |
-> Limit (cost=0.42..0.45 rows=1 width=12) (actual rows=1 loops=1) |
-> Index Only Scan using purchases_3c_idx on purchases (cost=0.42..6088.42 rows=200000 width=12) (actual rows=1 loops=1) |
Heap Fetches: 0 |
-> Nested Loop (cost=0.42..4.93 rows=10 width=12) (actual rows=1 loops=86369) |
-> WorkTable Scan on cte c (cost=0.00..0.20 rows=10 width=4) (actual rows=1 loops=86369) |
-> Limit (cost=0.42..0.45 rows=1 width=12) (actual rows=1 loops=86369) |
-> Index Only Scan using purchases_3c_idx on purchases purchases_1 (cost=0.42..2199.09 rows=66667 width=12) (actual rows=1 loops=86369) |
Index Cond: (customer_id > c.customer_id) |
Heap Fetches: 0 |
-> CTE Scan on cte (cost=0.00..2.02 rows=101 width=12) (actual rows=86369 loops=1) |
Planning Time: 0.254 ms |
Execution Time: 945.453 ms |
EXPLAIN
QUERY PLAN |
---|
Nested Loop (cost=0.42..155919.17 rows=86369 width=12) (actual rows=86369 loops=1) |
-> Seq Scan on customer c (cost=0.00..1246.69 rows=86369 width=4) (actual rows=86369 loops=1) |
-> Limit (cost=0.42..1.77 rows=1 width=12) (actual rows=1 loops=86369) |
-> Index Only Scan using purchases_3c_idx on purchases (cost=0.42..4.47 rows=3 width=12) (actual rows=1 loops=86369) |
Index Cond: (customer_id = c.customer_id) |
Heap Fetches: 0 |
Planning Time: 1.121 ms |
Execution Time: 187.620 ms |
EXPLAIN
QUERY PLAN |
---|
GroupAggregate (cost=0.42..8451.11 rows=69015 width=12) (actual rows=86369 loops=1) |
Group Key: customer_id |
-> Index Only Scan using purchases_3c_idx on purchases (cost=0.42..6088.42 rows=200000 width=12) (actual rows=200000 loops=1) |
Heap Fetches: 0 |
Planning Time: 0.161 ms |
Execution Time: 210.054 ms |
EXPLAIN