Example dataset:
id | tag
---|------
1 | car
1 | bike
2 | boat
2 | bike
3 | plane
3 | car
id
and tag
are both indexed.
I am trying to get the id who matches the tags [car, bike] (the number of tags can vary).
A naive query to do so would be:
SELECT id
FROM test
WHERE tag = 'car'
OR tag = 'bike'
GROUP BY id
HAVING COUNT(*) = 2
However, doing so is quite inefficient because of the group by and the fact that any line that match one tag is taken into account for the group by (and I have a large volumetry).
Is there a more efficient query for this situation?
The only solution I see would be to have another table containing something like:
id | hash
---|------
1 | car,bike
2 | boat,bike
3 | plane,car
But this is not an easy solution to implement and maintain up to date.
Additional infos:
- the name matching must be exact (no fulltext index)
- the number of tags is not always 2