我有一箇舊版innodb表,其中包含一個包含緯度/經度的業務。給定一個輸入緯度/經度(低於51.2167/4.41667),查詢將按照鄰近度(公里)的順序返回第一個啓用,啓用,未刪除的30個業務。與賬戶表的連接被用於檢查列表的有效性。使用經度/緯度索引mysql表進行地理查找
select
listing.*
from
listing listing ,
account account
where
listing.account_id = account.id
and listing.active = 1
and listing.approved = 1
and listing.deleted = 0
and listing.enabled = 1
and account.enabled = 1
and account.activated_by_user = 1
group by
listing.id
having
111.222569*degrees(acos(sin(radians(listing.latitude))*sin(radians(51.2167)) +cos(radians(listing.latitude))*cos(radians(51.2167))*cos(radians(listing.longitude - 4.41667)))) < 250
order by
111.222569*degrees(acos(sin(radians(listing.latitude))*sin(radians(51.2167)) +cos(radians(listing.latitude))*cos(radians(51.2167))*cos(radians(listing.longitude - 4.41667))))
limit 30;
下表列出並考慮每個包含超過50,000行,但查詢仍然平均需要24秒才能運行。如果沒有順序,它需要17秒。
我已經嘗試設置一些活動,批准,刪除,啓用索引。我是否可以重寫查詢或添加特定索引來有效執行此查詢 - 而不更改表結構?
+----+-------------+---------+-------------+-------------------------------------------------------------------------------------------------+-----------------------------------------------------------------+---------+------------------------+------+--------------------------------------------------------------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------------+-------------------------------------------------------------------------------------------------+-----------------------------------------------------------------+---------+------------------------+------+--------------------------------------------------------------------------------------------------------------------------------+
| 1 | SIMPLE | listing | index_merge | FKB4DC521D9306A80C,listing_active,listing_approved,listing_enabled,listing_deleted,index_test_1 | listing_active,listing_approved,listing_enabled,listing_deleted | 1,1,1,1 | NULL | 3392 | Using intersect(listing_active,listing_approved,listing_enabled,listing_deleted); Using where; Using temporary; Using filesort |
| 1 | SIMPLE | account | eq_ref | PRIMARY,account_enabled,account_activated_by_user,index_test_2 | PRIMARY | 8 | ctm.listing.account_id | 1 | Using where |
+----+-------------+---------+-------------+-------------------------------------------------------------------------------------------------+-----------------------------------------------------------------+---------+------------------------+------+--------------------------------------------------------------------------------------------------------------------------------+
任何幫助,非常感謝。
嗨Ollie,謝謝你的明確解釋。但是,到目前爲止,我沒有太多的運氣通過縮小搜索範圍來加快查詢速度,即使是在緯度上的索引。我會再試驗一下。 – javacoder 2011-05-09 22:47:47
加快速度的是改變select:SELECT id,latitude,longitude而不是SELECT列表*,然後在列表上執行查詢以獲取其餘數據。 – javacoder 2011-05-09 22:48:51
我想知道是否值得創建一個單獨的MyISAM表來存儲列表ID和用於搜索目的的GEOMETRY。有沒有人有這些空間索引速度的經驗? – javacoder 2011-05-09 22:54:42