I have the following query:
select count(*), date_trunc('day', updated_at) from test group by date_trunc('day', updated_at);
and the when explaining I have the following:
GroupAggregate (cost=213481.83..223749.85 rows=245009 width=8)
-> Sort (cost=213481.83..215883.63 rows=960720 width=8)
Sort Key: (date_trunc('day'::text, updated_at))
-> Index Only Scan using updatedat on test (cost=0.00..91745.26 rows=960720 width=8)
As you can see it has a high cost, and the query time is 6231.58 ms.
Is there a way to improve this? What should be the best index to create for this kind of count/group/date_trunc mix.
date_trunc('day', updated_at')
(postgresql allows to create index on expressions, not only on columns)? postgresql.org/docs/current/sql-createindex.htmlexplain (analyze, buffers) ...
(not just a simple explain).