In a stored procedure, I am generating an array of keys and want to compare it to an array of keys off a table. What is the fastest way to compare two arrays to see if both of them have at least one value that matches?
So far, we've tried the following:
- Loop over the generated keys and compare them to the key field (slow)
- Use array operation overlap (&&) on the arrays
Here is the definition of table: solicitation
id bigint NOT NULL DEFAULT nextval('id_seq'::regclass),
type_id bigint,
name character varying(2000),
key character varying[],
status bigint
Sample Code from Stored Procedure
-- keys = list of generated keys
FOR _id, _name, _type IN
SELECT rec.eid, rec.domain, rec.name, rec.type_id
from solicitation rec
where rec.key && _keys
and rec.status in (1201, 1202, 1241)
LOOP
...
END LOOP;
key is indexed.