1827 shaares
21 results
tagged
Tricks
TL;DR
Standard IN Clause
A standard IN clause with 5,000 IDs forces the database to tokenize each element individually, resulting in high Planning Time. The problem is that the parser needs to parse each constant and build a massive expression tree before execution starts.
EXPLAIN (ANALYZE, BUFFERS)
SELECT reading_value
FROM device_metrics
WHERE device_id IN (101, 102, 103, ... 5000);
ANY(ARRAY[])
When you use ANY(ARRAY[]) syntax, you tell PostgreSQL to treat the thousands of IDs as a single object rather than thousands of individual constants. This drastically reduces the query's structural complexity.
EXPLAIN (ANALYZE, BUFFERS)
SELECT reading_value
FROM device_metrics
WHERE device_id = ANY(ARRAY[101, 102, 103, ... 5000]::int[]);Row Constructor Comparison
TL;DR : x > a OR (x == a AND y > b) to (x, y) > (a, b)
Replace NOT IN by NOT EXISTS
ORDER BY a + 0
Remplacer NOT IN soit par NOT EXIST ou un left outer join where NULL