問題は、このクエリをスクリプト php に追加すると、mysql の CPU 使用率が 300% -> 400% になるのはなぜですか?
座標点(MYLAT、MYLON)から特定のカテゴリ(cat_id)の最も近い目標点(poi_id)を見つけたい
POIの表(poi):
| poi_id | name | city_id | lat | lon |
相関表(corr):
| cat_id | poi_id | city_id |
TABLE of CITIES (都市)
| city_id | name |
脚本:
$lat = MYLAT ; // 出発点の緯度
$lon =マイロン; // 出発地の経度
境界ボックス:
$lon1 = $lon - ($dist/abs(cos(rad2deg($lat)) * 60 * 1.1515));
$lon2 = $lon + ($dist/abs(cos(rad2deg($lat)) * 60 * 1.1515));
$lat1 = $lat - ($dist/ 60 * 1.1515);
$lat2 = $lat + ($dist/ 60 * 1.1515);
このクエリを追加すると、CPU 使用率が非常に高くなります。
SELECT a.poi_id,a.name,a.city_id,g.name,a.lat, a.lon,
((ACOS(SIN($lat * PI() / 180) * SIN(a.lat * PI() / 180) +
COS($lat * PI() / 180) * COS(a.lat * PI() / 180) *
COS(($lon - a.lon) * PI() / 180)) * 180 / PI()) * 69.09) AS distance
FROM poi a JOIN corr c on a.poi_id = c.poi_id JOIN city g on c.city_id = g.city_id
WHERE a.lon > $lon1 and a.lon < $lon2
AND a.lat > $lat1 and a.lat < $lat2
AND c.cat_id = $cat_id
HAVING distance < $dist
ORDER BY distance
LIMIT 20
+----+-------------+-------+--------+----------------------------------+---------+---------+--------------------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+----------------------------------+---------+---------+--------------------+------+-----------------------------+
| 1 | SIMPLE | a | range | poi_id,lat,lon,lat_2 | lat_2 | 14 | NULL | 6924 | Using where; Using filesort |
| 1 | SIMPLE | c | eq_ref | city_id_2,cat_id,poi_id,city_id | cat_id | 5 | const,a.poi_id | 1 | |
| 1 | SIMPLE | g | eq_ref | PRIMARY | PRIMARY | 4 | c.city_id | 1 | Using where |
+----+-------------+-------+--------+----------------------------------+---------+---------+--------------------+------+-----------------------------+
mysql> show profile;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000138 |
| checking permissions | 0.000010 |
| checking permissions | 0.000006 |
| checking permissions | 0.000008 |
| Opening tables | 0.000028 |
| System lock | 0.000015 |
| init | 0.000043 |
| optimizing | 0.000023 |
| statistics | 0.000221 |
| preparing | 0.000024 |
| Creating tmp table | 0.000029 |
| executing | 0.000005 |
| Copying to tmp table | 0.174197 |
| Sorting result | 0.000066 |
| Sending data | 0.000065 |
| end | 0.000006 |
| removing tmp table | 0.000010 |
| end | 0.000006 |
| query end | 0.000005 |
| closing tables | 0.000011 |
| freeing items | 0.000077 |
| logging slow query | 0.000005 |
| cleaning up | 0.000006 |
+----------------------+----------+
23 rows in set (0.00 sec)