-- the rows of a table joined by the scan's IN list taken from the entries of a btree that holds their columns (INCLUDE), where the page is all-visible, with no heap fetch -- label: {rows} * 1.5 events of 3,000 parents, a links table of 5 * {rows} rows (about 333 per parent), `NOT EXISTS (.. WHERE parent_id = e.foreign_id AND status = 0 AND label = 'lbl_7')` for 200 parents, btree on `(parent_id, status) INCLUDE (label)` -- ordered: yes --@ setup DO $do$ BEGIN IF to_regclass('iq_events') IS NULL THEN CREATE TABLE iq_events (id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, foreign_id int NOT NULL, kind int NOT NULL, deleted_at timestamptz); INSERT INTO iq_events (foreign_id, kind) SELECT 1 + (random() * 2999)::int, g % 7 FROM generate_series(1, {rows} * 3 / 2) g; CREATE TABLE iq_links (id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, parent_id int NOT NULL, label text NOT NULL, status smallint NOT NULL); INSERT INTO iq_links (parent_id, label, status) SELECT 1 + g % 3000, 'lbl_' || g / 3000 % 10, g / 3000 % 5 FROM generate_series(1, {rows} * 5) g ORDER BY random(); CREATE INDEX iq_links_parent_status ON iq_links (parent_id, status) INCLUDE (label); CREATE INDEX iq_events_col ON iq_events USING sift_col (id, foreign_id, kind, deleted_at) WITH (key_cut = foreign_id); END IF; END $do$; VACUUM (FREEZE, ANALYZE) iq_events, iq_links --@ pg SELECT e.kind, count(*) AS n FROM iq_events e 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 AND NOT EXISTS (SELECT 1 FROM iq_links l WHERE l.parent_id = e.foreign_id AND l.status = 0 AND l.label = 'lbl_7') GROUP BY 1 ORDER BY 1