On 23.07.2017 14:35, dilaz03 . wrote: > - IN-VALUES clause adds new node to plan. Has additional node big > overhead? How about filter by two or more IN-VALUES clause? > Hmmm. This works. -- Full table can fit in memory show shared_buffers; shared_buffers ---------------- 4GB show work_mem; work_mem ---------- 16MB SET max_parallel_workers_per_gather TO 0; SET max_parallel_workers TO 0; -- 10 000 000 events of 30 types from 500 sources CREATE TABLE events AS SELECT trunc(random() * 500)::bigint AS source_id, md5(trunc(random() * 30)::text) AS type FROM generate_series(1, 10000000); -- Prepare all clauses SELECT ('(' || string_agg(source_id::text, ',') || ')') AS source_id_in_clause FROM (SELECT source_id FROM events GROUP BY source_id ORDER BY source_id LIMIT 200) AS s \gset SELECT ('(' || string_agg(('''' || type || ''''), ',') || ')') AS type_in_clause FROM (SELECT type FROM events GROUP BY type ORDER BY type LIMIT 100) AS s \gset SELECT ('(VALUES ' || string_agg('(' || source_id::text || ')', ',') || ')') AS source_id_values_clause FROM (SELECT source_id FROM events GROUP BY source_id ORDER BY source_id LIMIT 200) AS s \gset SELECT ('(VALUES ' || string_agg('(''' || type::text || ''')', ',') || ')') AS type_values_clause FROM (SELECT type FROM events GROUP BY type ORDER BY type LIMIT 100) AS s \gset -- Run queries EXPLAIN (ANALYZE, BUFFERS) SELECT count(*) FROM events WHERE source_id IN :source_id_in_clause AND type IN :type_in_clause; Execution time: 21314.277 ms EXPLAIN (ANALYZE, BUFFERS) SELECT count(*) FROM events WHERE source_id IN :source_id_values_clause AND type IN :type_in_clause; Execution time: 9421.592 ms EXPLAIN (ANALYZE, BUFFERS) SELECT count(*) FROM events WHERE source_id IN :source_id_in_clause AND type IN :type_values_clause; Execution time: 17598.467 ms EXPLAIN (ANALYZE, BUFFERS) SELECT count(*) FROM events WHERE source_id IN :source_id_values_clause AND type IN :type_values_clause; Execution time: 5589.925 ms -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general