哪个选择sql更快?

时间:2015-09-12 07:54:59

标签: sql postgresql

哪个sql更快?我想要做的是找出今天没有拍摄的所有基金。我每天拍摄快照并将其保存在此快照表中。

explain SELECT id from fund where id NOT IN (select id from fund_daily_snap where date_trunc('day',day) = CURRENT_DATE-1); 
                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
Seq Scan on fund  (cost=76961.83..97431.81 rows=11999 width=4)
  Filter: (NOT (hashed SubPlan 1))
  SubPlan 1
    ->  Seq Scan on fund_daily_snap  (cost=0.00..76932.96 rows=11550 width=4)
          Filter: (date_trunc('day'::text, day) = (current_date - 1))
OR 



explain SELECT count(id) from fund where id NOT IN (select id from fund_daily_snap where day >= CURRENT_DATE-1); 
                                             QUERY PLAN                                               
-------------------------------------------------------------------------------------------------------
Aggregate  (cost=959232109.01..959232109.02 rows=1 width=4)
  ->  Seq Scan on fund  (cost=0.00..959232079.01 rows=11999 width=4)
        Filter: (NOT (SubPlan 1))
        SubPlan 1
          ->  Materialize  (cost=0.00..78015.88 rows=770033 width=4)
                ->  Seq Scan on fund_daily_snap  (cost=0.00..71157.71 rows=770033 width=4)
                      Filter: (day >= (current_date - 1))

1 个答案:

答案 0 :(得分:0)

如果你有" day"列,第二个解决方案将通过数据增加变得更快。因为内置函数总是需要更多的成本来评估。