1

我有三张桌子

奥特莱斯(shopId、shopName、shopLatitude、shopLongitude、g)(450 行)

g 是几何类型并包含值点(纬度经度)

优惠(offerId、offername、offerDescription)(450 行)

优惠_奥特莱斯

(offerId,shopId) (503 行)

我想获取特定半径内的所有优惠及其商店详细信息,我正在按照本教程实施空间查询。

这是我正在使用的查询,它对几百条记录运行良好,但现在每个表中的项目数量都在上面提到,它需要大约 34 秒才能返回结果。我怎样才能有效地编写这个查询?

select DISTINCT     
       ofr.offerId,ofr_otl.shopid,ofr.isdeleted,ofr.offer_title,ofr.offer_icon,
       ofr.offer_description,ofr.CategoryId,ofr.offer_terms,
       ofr.start_date,ofr.end_date,
       ofr.price_description,
       otl.shop_name,otl.shop_address,otl.shop_city,
       otl.shop_phone,otl.shop_icon,
       otl.shop_latitude,otl.shop_longitude,otl.shop_country,otl.shop_zip,
       get_distance(x(g),y(g),8.4901831,76.9558434) as distance,
       otl.shop_weblink,
       ofr.off_Angle,ofr.rating
  from offers as ofr,outlets as otl,off_outlets as ofr_otl 
 where ofr.offerId = ofr_otl.offid 
   and otl.shop_id = ofr_otl.shopid
   and st_within(g,envelope(linestring(
                   point(8.039914120289854, 76.5005853263206), 
                   point(8.940452079710145, 77.41110147367941))))
   and ofr.isdeleted = 0
 order by offer_title
 LIMIT 300 ;
4

1 回答 1

0

尝试这个:

SELECT ... FROM 
(
  SELECT * FROM
  (
    SELECT * FROM outlets a
    WHERE a.shopLatitude IS BETWEEN ( ... ) AND a.shopLongitude IS BETWEEN ( ... )
  ) t1
 WHERE st_within(t1.g, ... )
) otl
LEFT JOIN off_outlets as oo ON otl.shop_id = oo.shopid
LEFT JOIN offers as ofr ON ofr.offerId = oo.offid 
WHERE ofr.isdeleted = 0
ORDER BY offer_title
LIMIT 300 ;

它基本上迫使 mysql首先在纬度/经度矩形内过滤商店,然后在 st_within 上过滤,然后再做其余的。

于 2014-11-09T02:37:38.713 回答