I am currently working on a website which needs some optimisations ... since the front page takes about 15-20 seconds to be loaded I thought that some optimisation would be nice.
Here is one query that appeared on the MySQL slow query log:
SELECT a.user,a.id
FROM `profil_perso` pp
INNER JOIN `acces` a ON pp.parrain = a.id
INNER JOIN `acces` ap ON ap.id = pp.id
WHERE pp.parrain_visibilite = '1'
AND a.actif = 1
GROUP BY a.id
ORDER BY ap.depuis DESC LIMIT 15;
On profil_perso
(~207K lines -- contains emails and profiles) there is perso_id
that is the primary key, there is also id
(foreign key) + parrain
(referer) + parrain_visibilite
(referer is showed) that are indexes.
On acces
there is id
that is the primary key, there is also depuis (registration date) that is indexed
The benchmark shows this actually:
First time : 1.94532990456
Last time : 1.94532990456
Average time : 0.0389438009262
I tried to put it this way :
SELECT DISTINCT a.id, a.user
FROM `profil_perso` pp
LEFT JOIN `acces` a ON pp.parrain = a.id
WHERE pp.parrain_visibilite = 1
AND a.actif = 1
AND pp.id != 0
ORDER BY pp.id DESC LIMIT 15;
Still the benchmark show this:
First time: 1.96376991272
Last time: 1.96376991272
Average time: 0.0393264245987
Any hint to lower the query time ?
Here's the full indexes:
acces :
id (primary)
derniere_visite -- last visit
pays_id -- country_id
depuis -- registration time
perso_id -- foreign key to profil_perso primary key
actif -- account status
compte_premium -- if account is premium
profil_perso :
perso_id (primary)
id -- foreign key to acces primary key
genre -- gender
parrain_visibilite -- visibility of referer
parrain -- referer
parrain_contexte
telephone
orientation
naissance -- birthdate
photo -- if it has a picture