In table users
, there is a column user_ids varchar[]
with GIN index. Each user_id
of column user_ids
in each row is unique.
I want to count the number of rows that contain user_id
for each user_id
in an input list.
I try using this query but EXPLAIN
shows it doesn't use GIN index:
SELECT user_id, count(*) + 1 COUNT
FROM (
SELECT unnest(user_ids) user_id
FROM users
WHERE user_ids && ARRAY['1', '2', '3']::varchar[]
) sub
WHERE user_id = ANY(ARRAY['1', '2', '3']::varchar[])
GROUP BY user_id;
Is there any way to make use of GIN index here?