I have a processing in database that iterate over an entire table. At some point, I need to check if a VARCHAR matches any value in a column.
I want to know what is the fast way to do this. Do SQL comparison each time? Retrieve the table values to a VARCHAR array? Another solution?
The iteration in the table is over millions, so the comparison will be done million times. In the match table, there a few hundred values.
So, what is the best approach for this?
Have an index on the
varcharcolumn in the small table. Then the look-up will be as fast as possible. A plain btree index (default) is good for the equality operator.If you only look up some values, this will be the fastest way:
Resulting in an index search. If you look up all values (does not sound like you do), a
LEFT JOINis faster:Transforming the values from the small table into array and checking there cannot compete with an index search.