I am trying here to basically find users that do have sports & regions targeted by an activity. In the acces [users] table there is around 17K users. Each can have a certain number of sport interests and one region.
There query here look for each users that have one sport & one region at least that are targeted via the activities. Sports can be up to 75 when we select each of em [not quite good with an IN query].
SELECT a.user, pp.courriel
FROM acces a
LEFT JOIN acces_profil_sport ap ON ap.id = a.id
LEFT JOIN profil_perso pp ON pp.id = a.id
WHERE ap.sport_id IN
(
SELECT ac.sport_id
FROM activite_sport ac
RIGHT JOIN activite a ON a.activite_id = ac.activite_id AND a.is_cron = 1 AND a.cron_processed = 0
)
AND pp.region_id IN
(
SELECT ar.region_id
FROM activite_region ar
RIGHT JOIN activite a ON a.activite_id = ar.activite_id AND a.is_cron = 1 AND a.cron_processed = 0
)
GROUP BY a.id
If I remove the sport lookup, the query takes arounds 30 secs to run. Otherwise it takes quite forever and use around 99% of the proc with mysql.
Any hints to help that ?
[edit : Table structure]
Acces : id (primary key), user, perso_
id (Key/Foreign key to profil_
perso[perso_
id]) [some-other-fields]
profil_
perso : perso_
id (primary key) courriel, region_
id, id (foreign key to acces[id]) [some other fields]
acces_
profil_
sport : id/sport_
id (dual primary key), niveau_
id (dual key with sport_
id)