I have table in MySQL database with two integer columns for example:
userid | groupid
10 | 300
11 | 300
11 | 301
12 | 302
Given two groupids, I am looking for the best and quickest way to find userids which are in both groups. My table contains 23M rows and I need to that for each distinct pair of groupids. Currently both columns are indexed however it takes so long to get the result even for a single pair of groups and I have 1000 distinct groupids. The query I am running now is:
select count(t2.userid)
from usergroup t1, usergroup t2
where t1.groupid = 27 and t2.groupid = 714 and t1.userid = t2.userid
Is there a way to do it fast?