Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql cpu usage increasing sorting lat lon distance nearest point
    primarykey
    data
    text
    <p>the question is: Why mysql's cpu usage goes to 300% -> 400% when i add this query in my script php ?</p> <p>I want to find the nearest Point of intereset (poi_id) of certain category (cat_id) from a coordinates point (MYLAT, MYLON)</p> <p>TABLE of <b>POI</b> (poi): </p> <pre><code> | poi_id | name | city_id | lat | lon | </code></pre> <p>TABLE of <b>correlations</b> (corr):</p> <pre><code>| cat_id | poi_id | city_id | </code></pre> <p>TABLE of <b>CITIES</b> (city)</p> <pre><code>| city_id | name | </code></pre> <p><b> SCRIPT: </b></p> <p>$lat = <b>MYLAT</b>; // Latitude of my starting point</p> <p>$lon = <b>MYLON</b>; // Longitude of my starting point</p> <p><b>Bounding box</b>:</p> <p>$lon1 = $lon - ($dist/abs(cos(rad2deg($lat)) * 60 * 1.1515));</p> <p>$lon2 = $lon + ($dist/abs(cos(rad2deg($lat)) * 60 * 1.1515));</p> <p>$lat1 = $lat - ($dist/ 60 * 1.1515);</p> <p>$lat2 = $lat + ($dist/ 60 * 1.1515);</p> <p>When I add this query the CPU usage goes insane:</p> <pre><code>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 &gt; $lon1 and a.lon &lt; $lon2 AND a.lat &gt; $lat1 and a.lat &lt; $lat2 AND c.cat_id = $cat_id HAVING distance &lt; $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&gt; 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload