我正在为iOS应用程序提供所有php服务。 其中一个似乎非常快地消耗我所有的3个CPU内核(而没有其他内容)。 该服务将51个最近的“气泡”(bulles)提取到用户实际所在的位置。 这是我的php:
$sql="SELECT *, get_distance('$latitude','$longitude',latitude,longitude) AS proximite
FROM bulles INNER JOIN
(SELECT userId, userFirstName, userLastName, userName, userPictureUrl from user) AS users
ON bulles.auteur = users.userId
INNER JOIN interests
ON bulles.interet1 = interests.id
WHERE auteur = '$userID'
OR auteur
IN (
SELECT followedId
FROM following
WHERE followerId = '$userID'
)
ORDER BY proximite
LIMIT 51";
这是我存储的函数“get_distance”:
FUNCTION `get_distance`(lat1 DOUBLE, lng1 DOUBLE, lat2 DOUBLE, lng2 DOUBLE) RETURNS double
BEGIN
DECLARE rlo1 DOUBLE;
DECLARE rla1 DOUBLE;
DECLARE rlo2 DOUBLE;
DECLARE rla2 DOUBLE;
DECLARE dlo DOUBLE;
DECLARE dla DOUBLE;
DECLARE a DOUBLE;
SET rlo1 = RADIANS(lng1);
SET rla1 = RADIANS(lat1);
SET rlo2 = RADIANS(lng2);
SET rla2 = RADIANS(lat2);
SET dlo = (rlo2 - rlo1) / 2;
SET dla = (rla2 - rla1) / 2;
SET a = SIN(dla) * SIN(dla) + COS(rla1) * COS(rla2) * SIN(dlo) * SIN(dlo);
RETURN (6378137 * 2 * ATAN2(SQRT(a), SQRT(1 - a)));
END
每秒10次此服务请求使cpu饱和。
我之前遇到过INNER JOIN语句的问题,所以也许它来自那里?
有什么想法吗?
答案 0 :(得分:3)
MySQL'IN'效率非常低(虽然是一个很好的构造),因为查询会针对每一行进行重新处理。即。
IN (
SELECT followedId
FROM following
WHERE followerId = '$userID'
)
将其替换为FROM子句中的表。 e.g:
...FROM ( SELECT followedId FROM following WHERE followerId = '$userID') AS whatever
(then in the WHERE put ) OR auteur = whatever.followedId...
干杯。