-- the keys of the scan's IN list that a joined table is keyed by, read by one scan of the table's btree (the list as its keys and the join's `column = value` conditions on the next index column), where the table was read whole -- label: {rows} * 1.5 events of 3,000 parents, a links table of 5 * {rows} rows (about 333 per parent, a fifth of them with the status asked for), `JOIN links ON parent_id = e.foreign_id AND weight = 1 AND status = 0 WHERE e.foreign_id IN (200 parents)`, btree on `(parent_id, status)` -- ordered: yes --@ setup DO $do$ BEGIN IF to_regclass('ik_events') IS NULL THEN CREATE TABLE ik_events (id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, foreign_id int NOT NULL, kind int NOT NULL, deleted_at timestamptz); INSERT INTO ik_events (foreign_id, kind) SELECT 1 + (random() * 2999)::int, g % 7 FROM generate_series(1, {rows} * 3 / 2) g; CREATE TABLE ik_links (id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, parent_id int NOT NULL, label text NOT NULL, weight int NOT NULL, status smallint NOT NULL); INSERT INTO ik_links (parent_id, label, weight, status) SELECT 1 + g % 3000, 'lbl_' || g / 3000 % 10, g / 3000 / 5 % 3, g / 3000 % 5 FROM generate_series(1, {rows} * 5) g ORDER BY random(); CREATE INDEX ik_links_parent_status ON ik_links (parent_id, status); CREATE INDEX ik_events_col ON ik_events USING sift_col (id, foreign_id, kind, deleted_at) WITH (key_cut = foreign_id); END IF; END $do$; VACUUM (FREEZE, ANALYZE) ik_events, ik_links --@ pg SELECT l.label, count(*) AS n FROM ik_events e JOIN ik_links l ON l.parent_id = e.foreign_id AND l.weight = 1 AND l.status = 0 WHERE e.foreign_id IN (1,16,31,46,61,76,91,106,121,136,151,166,181,196,211,226,241,256,271,286,301,316,331,346,361,376,391,406,421,436,451,466,481,496,511,526,541,556,571,586,601,616,631,646,661,676,691,706,721,736,751,766,781,796,811,826,841,856,871,886,901,916,931,946,961,976,991,1006,1021,1036,1051,1066,1081,1096,1111,1126,1141,1156,1171,1186,1201,1216,1231,1246,1261,1276,1291,1306,1321,1336,1351,1366,1381,1396,1411,1426,1441,1456,1471,1486,1501,1516,1531,1546,1561,1576,1591,1606,1621,1636,1651,1666,1681,1696,1711,1726,1741,1756,1771,1786,1801,1816,1831,1846,1861,1876,1891,1906,1921,1936,1951,1966,1981,1996,2011,2026,2041,2056,2071,2086,2101,2116,2131,2146,2161,2176,2191,2206,2221,2236,2251,2266,2281,2296,2311,2326,2341,2356,2371,2386,2401,2416,2431,2446,2461,2476,2491,2506,2521,2536,2551,2566,2581,2596,2611,2626,2641,2656,2671,2686,2701,2716,2731,2746,2761,2776,2791,2806,2821,2836,2851,2866,2881,2896,2911,2926,2941,2956,2971,2986) AND e.deleted_at IS NULL GROUP BY 1 ORDER BY 1