如何加快纬度和经度的搜索索引

时间:2017-04-28 03:38:44

标签: php mysql pdo

下面的stmt工作正常,它只选择一定距离(英里)内的client_id。问题是它非常慢。 12秒搜索180万条小记录。是的,我已经在纬度和经度上创建了一个索引。

$stmt = $conn->prepare("SELECT client_id,
 ( 3959 * acos( cos( radians(:currentlat) ) * cos( radians( lat ) ) * cos( radians( longitude ) - radians(:currentlon) ) 
+ sin( radians(:currentlat) ) * sin( radians( lat ) ) ) )   AS distance FROM $live_table

WHERE  (companyname like :name
 OR
   contact LIKE :name
 OR
   yourcategory LIKE :name)
 AND
   is_active != :active_switch
 HAVING distance < :mydistance ORDER BY client_id desc ");

几天前,我从stackoverflow获得了帮助,我们想出了以下内容。 Awsome,现在只需要3到4秒。但它没有计算距离 正常。它正确地返回LIKE命令,但都是。如果我选择$ mydistance为20英里或更少,它仍会显示100英里以外的结果。 我花了好几个小时试图解决这个问题并在我的机智结束时。请再次帮助

$stmt = $conn->prepare("SELECT client_id,
 (lat  BETWEEN :currentlat - (:mydistance / 69.0 ) AND :currentlat + (:mydistance / 69.0 )),
  longitude BETWEEN :currentlon - (:mydistance / (111.045 * COS(RADIANS(lat)))) AND :currentlon + (:mydistance / (111.045 * COS(RADIANS(lat))))

FROM $live_table
WHERE (companyname like :name
  OR
   contact LIKE :name
  OR
   yourcategory LIKE :name)
  AND 
   is_active != :active_switch
  HAVING distance < :mydistance ORDER BY client_id DESC ");

好的斯隆,我想我明白你在说什么,所以我添加了#34; AS distance&#34;计算纬度和经度后仍然无法正常工作

$stmt = $conn->prepare("SELECT client_id,
 (lat  BETWEEN :currentlat - (:mydistance / 69.0 ) AND :currentlat + (:mydistance / 69.0 )),
  longitude BETWEEN :currentlon - (:mydistance / (111.045 * COS(RADIANS(lat)))) AND :currentlon + (:mydistance / (111.045 * COS(RADIANS(lat))))

AS distance

FROM $live_table
WHERE (companyname like :name
  OR
   contact LIKE :name
  OR
   yourcategory LIKE :name)
  AND 
   is_active != :active_switch
  HAVING distance < :mydistance ORDER BY client_id DESC ");

0 个答案:

没有答案