Langsom query
Here goes:I have a question about optimizing a query.
A have one table with all my users and then 4 other tables with
userpreferences.
I join the 4 other tables in a query like this:
---
SELECT DISTINCT userlist.ID, userlist.username, userlist.age,
userlist.geografi,
userlist.malefemale, userlist.photo, userlist.kortintro,
date_format(userlist.lastaccess,'%d.%m.%y %H:%i') AS lastaccess FROM
userlist
LEFT JOIN R_wants ON (userlist.ID = R_wants.userID)
LEFT JOIN R_humanpro ON (userlist.ID = R_humanpro.userID)
LEFT JOIN R_interrest ON (userlist.ID = R_interrest.userID)
LEFT JOIN R_musictaste ON (userlist.ID = R_musictaste.userID)
where userlist.aktiv = 2
AND (R_wants.wantsID = 2 OR R_wants.wantsID = 3)
AND (R_interrest.interrestID = 25)
AND (R_musictaste. musictaste ID = 17 OR R_musictaste. musictaste ID = 22)
AND (R_humanpro.humanproID = 16 OR R_humanpro.humanproID = 13
OR R_humanpro.humanproID = 17)
ORDER BY logins
---
This query seems can be rather slow if the user selects more options than
this
example, so I'm wondering if I can make a better/faster query.
When I do a EXPLAIN I get this:
---
table type possible_keys key key_len ref rows Extra
userlist ALL 107 where used
R_wants ref userID userID 3 userlist.ID 10 where used
R_humanpro ref userID userID 3 userlist.ID 10 where used
R_interrest ref userID userID 3 userlist.ID 10 where used
R_musictaste ref userID userID 3 userlist.ID 10 where used
---
What does this tell me? That is needs to scan (107*10*10*10*10) = 1,070,000
rows OR
just (107*10)+(107*10)+(107*10)+(107*10) = 4280 rows?
I only join on ID's which are all INTs.
I made indexes on all the ID columns
What can I do? The query takes up to 40 sec. if there is a lot of options
in the where part.
Please tell my if you need more information to help me.