【发布时间】:2018-03-02 15:56:54
【问题描述】:
我有一个很慢的查询...我想显示我附近的最后 12 个最新成员(靠近登录用户),我的开发数据库有 150k 行。
花了 1 秒多的时间,解释查询告诉我过滤了 30k 行 所以在我的开发数据库中过滤了 150k 行 30k...我的在线服务器比这个大得多....
这是我的查询:
SELECT profils.*,
Users.username,
( SELECT count(*)
from profilsphotos pp
where pp.iduser=Profils.iduser
) as nbpics,
ATAN2(SQRT(POW(COS(RADIANS(50.78961000)) * SIN(RADIANS(Y(gm_coor) - 4.64956000)),
2) + POW(COS(RADIANS(X(gm_coor))) * SIN(RADIANS(50.78961000)) - SIN(RADIANS(X(gm_coor))) * COS(RADIANS(50.78961000)) * COS(RADIANS(Y(gm_coor) - 4.64956000)),
2)), (SIN(RADIANS(X(gm_coor))) * SIN(RADIANS(50.78961000)) + COS(RADIANS(X(gm_coor))) * COS(RADIANS(50.78961000)) * COS(RADIANS(Y(gm_coor) - 4.64956000)))
) * 6372.795 AS distance
from Users
inner join Profils ON Users.id=Profils.iduser
where Profils.Actif=1
and profils.idsexe=2
and profils.idlookingfor=1
and Profils.iduser<>1
HAVING distance<400
order by Users.id desc, distance asc
limit 12
请注意,我在这四个字段上添加了索引:actif、idsexe、idlookingfor 和 iduser
我的查询有什么问题?
非常感谢!
帕斯卡
【问题讨论】:
-
你能粘贴解释输出以及这个查询的结果吗:select count(*) from Users inner join Profils on Users.id=Profils.iduser where Profils.Actif = 1 and profils. idsexe = 2 和 profils.idlookingfor = 1 和 Profils.iduser 1
-
你好,马克,同样的事情...... 30k 过滤......
标签: mysql performance optimization having