2012-01-16 86 views
0

我正在使用此查詢來獲取用戶附近的地方。SQLite查詢優化

query = [NSString stringWithFormat:@"SELECT place_index, place_name, place_category, place_address, place_phone, place_mobile, place_description, place_email, place_website, place_facebookId, place_neighborhood, place_rating, place_lat, place_long, place_is_closed, product_name, distance(place_lat, place_long, '%f', '%f') AS dist FROM (SELECT places.id as place_index, places.name as place_name, places.category as place_category, places.address as place_address, places.phone as place_phone, places.mobile as place_mobile, places.description as place_description, places.email as place_email, places.website as place_website, places.facebookId as place_facebookId, places.neighborhood as place_neighborhood, places.rating as place_rating, places.lat as place_lat, places.long as place_long, places.is_closed as place_is_closed, products.name as product_name FROM places LEFT JOIN products ON products.place_id = places.id UNION SELECT places.id as place_index, places.name as place_name, places.category as place_category, places.address as place_address, places.phone as place_phone, places.mobile as place_mobile, places.description as place_description, places.email as place_email, places.website as place_website, places.facebookId as place_facebookId, places.neighborhood as place_neighborhood, places.rating as place_rating, places.lat as place_lat, places.long as place_long, places.is_closed as place_is_closed, added_products.name as added_product_name FROM places LEFT JOIN added_products ON added_products.place_id = places.id) WHERE place_is_closed = 0 ", userLocation.coordinate.latitude, userLocation.coordinate.longitude]; 

基本上我已經選擇了聯合選擇與左聯接。該查詢用於搜索機制,因此每次用戶輸入內容時都會觸發UITextField。執行和獲取對象需要1秒鐘的時間,有沒有辦法以某種方式對其進行優化?

- 編輯

這是按要求格式化參數上面列出查詢的文本,但我不認爲他們是一個問題:

SELECT place_index, 
     place_name, 
     place_category, 
     place_address, 
     place_phone, 
     place_mobile, 
     place_description, 
     place_email, 
     place_website, 
     place_facebookid, 
     place_neighborhood, 
     place_rating, 
     place_lat, 
     place_long, 
     place_is_closed, 
     product_name, 
     Distance(place_lat, place_long, '%f', '%f') AS dist 
FROM (SELECT places.id   AS place_index, 
       places.name   AS place_name, 
       places.category  AS place_category, 
       places.address  AS place_address, 
       places.phone  AS place_phone, 
       places.mobile  AS place_mobile, 
       places.DESCRIPTION AS place_description, 
       places.email  AS place_email, 
       places.website  AS place_website, 
       places.facebookid AS place_facebookid, 
       places.neighborhood AS place_neighborhood, 
       places.rating  AS place_rating, 
       places.lat   AS place_lat, 
       places.long   AS place_long, 
       places.is_closed AS place_is_closed, 
       products.name  AS product_name 
     FROM places 
       LEFT JOIN products 
       ON products.place_id = places.id 
     UNION 
     SELECT places.id   AS place_index, 
       places.name   AS place_name, 
       places.category  AS place_category, 
       places.address  AS place_address, 
       places.phone  AS place_phone, 
       places.mobile  AS place_mobile, 
       places.DESCRIPTION AS place_description, 
       places.email  AS place_email, 
       places.website  AS place_website, 
       places.facebookid AS place_facebookid, 
       places.neighborhood AS place_neighborhood, 
       places.rating  AS place_rating, 
       places.lat   AS place_lat, 
       places.long   AS place_long, 
       places.is_closed AS place_is_closed, 
       added_products.name AS added_product_name 
     FROM places 
       LEFT JOIN added_products 
       ON added_products.place_id = places.id) 
WHERE place_is_closed = 0 
+0

你介意格式化查詢的方式,使我們可以真正閱讀它嗎? – 2012-01-16 08:56:03

+0

當然,請再看一遍。 – Eugene 2012-01-16 09:01:36

回答

0

使子查詢返回只有places表的標識列,並將新JOIN添加到標識列頂部的表中。這應該使子查詢計算更輕。

+0

謝謝,它將速度從0.7-1.1秒提高到0.3-0.06秒。你有沒有其他有用的提示?我也使用LIKE比較了很多,對於性能還有什麼不好? – Eugene 2012-01-16 13:21:56

+0

@Eugene,好吧,這取決於查詢。像它本身不是邪惡的,應該看看。我的技巧是閱讀一些關於SQL的書籍,它們比一些技巧提供了更多的理解。 – newtover 2012-01-16 13:29:24

+0

有時間閱讀書籍真的很棒,但是,在iOS開發世界裏,從來沒有像空閒時間那樣的東西。 :)但謝謝你 – Eugene 2012-01-16 13:34:12