gpt4 book ai didi

MySQL 查询优化

转载 作者:行者123 更新时间:2023-11-29 06:24:26 25 4
gpt4 key购买 nike

我目前正在开发一个需要一些优化的网站...由于首页需要大约 15-20 秒才能加载,我认为进行一些优化会很好。

以下是 MySQL 慢查询日志中出现的一个查询:

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;

profil_perso (约 207K 行 - 包含电子邮件和个人资料)上,有 perso_id 是主键,还有 id (外键)+ parrain(引用者)+ parrain_visibilite(显示引用者)是索引。

acces 上,id 是主键,还有索引的 depuis(注册日期)

基准测试实际上表明了这一点:

第一次:1.94532990456
最后时间:1.94532990456
平均时间:0.0389438009262

我试着这样说:

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;

基准测试仍然表明:

第一次:1.96376991272
最后时间:1.96376991272
平均时间:0.0393264245987

有任何减少查询时间的提示吗?

以下是完整索引:

访问:

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

个人资料:

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

最佳答案

运行解释选择不同的a.id .....;

这将帮助您了解哪里可能缺少索引等。

关于MySQL 查询优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/835568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com