There’s a table with compound index (col1, col2, col3). There’s a lot of data in this table.
Want to build query for example WHERE col1 = 2 AND col2 = 12 AND col3 IN (1, 2, 3, …, 40)
Is there a way to use index fully (with 3 columns)?
When I’m trying
SELECT *
FROM table t
WHERE t.col1 = 2
AND t.col2 = 12
AND t.col3 IN (1, 2, 3, ..., 40)
Postgres planner makes index scan on (col1, col2) and then uses SeqScan to filter one by one 400k of rows with col3 IN (1, 2, 3, ..., 40)
If I try
SELECT *
FROM table t
WHERE (col1, col2, col3) IN VALUES (2, 12, 1), (2, 12, 2), (2, 12, 3), ... ,(2, 12, 40)
it gives error:
temporary file size exceeds temp_file_limit
So it works slow. Is there a way to make postgres use somehow compound index for 3 columns?
2
Answers
You could try loading the
col3
possible values into a bona fide table and then rewriting the query to the following:This assumes that
table2
has the following structure:table1
might be able to use an index on(col1, col2, col3)
. An index should also be placed ontable2 (col3)
, to ensure rapid lookups.Based on your comment, it looks like we can force use of the index through an explicit join on
(col1, col2, col3) = (arg1, arg2, arg3)
.I don’t know how you are calling this query, but if called from a host language that allows passing an
int[]
type through the database driver, my query would look like this:A working fiddle with random test records and
explain