SQLITE Complex select语句。帮助我加快速度

时间:2012-02-15 17:51:52

标签: database sqlite

有些人可能会或可能不会认为这是一个复杂的陈述,但对我来说(因为我只做了大约一个月的陈述)这个是。下面的语句返回了我正在寻找的确切结果,但我的问题是在iMac上运行需要95秒以上。我需要这个声明才能在iPhone上运行。有没有更好(更快)的方法来做到这一点?

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 个答案:

答案 0 :(得分: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