2012-01-16 13 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]; 

基本的には、LEFTジョインによる選択の連合から選択します。このクエリは検索メカニズムで使用されるため、ユーザーが何かをUITextFieldに入力するたびにトリガーされます。オブジェクトを実行してフェッチするには1秒の時間がかかりますが、それをどうにか最適化する方法はありますか?

- EDIT

これは、書式設定のパラメータは、上に列挙されている要求されるように、クエリのテキストですが、私は、彼らが問題だとは思わない:

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

私たちが実際に読むことができるような形でクエリをフォーマットしてもよろしいですか? –

+0

確かに、もう一度ご覧ください。 – Eugene

答えて

0

サブクエリが返すようにplacesテーブルのID列のみを削除し、ID列の一番上にあるテーブルに新しいJOINを追加します。これにより、サブクエリ計算がより軽くなります。

+0

これは、速度を0.7〜1.1秒から0.3〜0.6秒に随時増加させました。おそらく他の役に立つヒントはありますか?私も好きなものを使っていますが、それは性能にも悪いですか? – Eugene

+0

@Eugene、それはクエリによって異なります。同じように悪いことではない、見るべきである。私のヒントは、SQLに関する書籍のいくつかを読むことです、彼らはいくつかのヒントよりも多くの理解を与える。 – newtover

+0

実際には本を読む時間があるのはすばらしいことですが、まあ、iOS開発の世界では、空き時間のようなものは決してありません。 :) ありがとう、結構です – Eugene