哪种选择sql更快?

哪种选择sql更快?,sql,postgresql,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

哪种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))

如果您有indeks on day列,则第二个解决方案将随着数据的增加而变得更快。因为内置函数总是有更多的成本需要评估。

您不能自己比较成本吗?一个选择id'd,另一个选择count*,一个选择基于=天,一个选择基于>=truncday。您对哪个更改感兴趣?第一个查询没有count*。这是错误的吗?您必须使用“当天没有索引”。如果可能,请创建索引并重新运行快照。