如何按date_trunc索引分组(' second',date)

时间:2017-12-19 14:11:09

标签: postgresql

如何通过date_trunc对索引进行分组(' second',date')。 date_trunc(' second',date)的成本非常高,即使我添加了以下索引。

select c.name, t.code ,count(*) count_second,sum(volume) sum_volume from 
tradedetail t  inner join company c on c.code=t.code and country = 'MY'
      where date::date=current_date and t.buysell='S' group by t.code, 
c.name,date_trunc('second',date) having sum(volume) >=4000 

CREATE INDEX tradedetail_sbuysell
ON tradedetail
USING btree
(code COLLATE pg_catalog."default", date_trunc('second',date), buysell COLLATE pg_catalog."default")
where buysell = 'S';


"GroupAggregate  (cost=4094738.36..4108898.28 rows=435690 width=30)"
"  Filter: (sum(t.volume) >= 4000::numeric)"
"  ->  Sort  (cost=4094738.36..4095827.58 rows=435690 width=30)"
"        Sort Key: t.code, c.name, (date_trunc('second'::text, t.date))"
"        ->  Nested Loop  (cost=0.01..4033076.58 rows=435690 width=30)"
"              ->  Seq Scan on company c  (cost=0.00..166.06 rows=4735 width=14)"
"                    Filter: ((country)::text = 'MY'::text)"
"              ->  Index Scan using tradedetail_index5 on tradedetail t  (cost=0.01..849.77 rows=172 width=22)"
"                    Index Cond: (((code)::text = (c.code)::text) AND ((date)::date = ('now'::cstring)::date) AND ((buysell)::text = 'S'::text))"

0 个答案:

没有答案