2012-02-15 42 views
1

有些人可能會認爲這不是一個複雜的陳述,但對我來說(因爲我只做了約一個月的陳述),這是一個。以下語句返回我正在查找的確切結果,但我的問題是,它需要95秒才能在iMac上運行。我需要這個聲明在iPhone上運行。任何人都可以以更好(更快)的方式來做到這一點?SQLITE複雜選擇語句。幫助我加快它

select categories.category 
from categories join categories_listings 
where categories_listings.category_id = categories.id 
    and categories.association_id = 1 
    and (select count(*) 
     from (select (
        select categories.category 
        from categories left join categories_listings 
        where categories_listings.category_id = categories.id 
         and categories.association_id = 1 
         and listings.id = categories_listings.listing_id) as region 
      from listings left join chamber_specifics 
       on chamber_specifics.listing_id=listings.id 
      where region = categories.category 
       and listings.association_id=1 
       and listings.status = 1 
       and downtown='Y')) >0 
group by categories.category; 

讓我知道是否需要更多信息。

謝謝!

+1

試圖編輯那個怪物後,我會說:是的,這很複雜。你的第一個'join'沒有'ON'子句...我不認爲你需要'count(*)'子句 - 只需選擇所有記錄並進行一次'IN'檢查。 – Rudu 2012-02-15 18:02:05

+0

對不起,怪物。我感謝你的意見。我現在正在修改它,以包含一個ON子句並執行IN檢查。我會讓你知道結果如何。謝謝! – KevinM 2012-02-15 18:54:52

回答

1

我想出了一個聲明,使我獲得與我的問題相同的結果,但在完成約0.062秒時快得多。下面是我的聲明:(謝謝Rudu的方向!)

select (
     select categories.category 
     from categories left join categories_listings 
     where categories_listings.category_id = categories.id 
     and categories.association_id = 1 
     and listings.id = categories_listings.listing_id) as region 
    from listings left join chamber_specifics 
    on chamber_specifics.listing_id=listings.id 
    where listings.association_id=1 
    and listings.status = 1 
    and downtown='Y' 
group by region 
+0

不客氣:) – Rudu 2012-02-16 20:41:17