I have a query like this
explain analyze
SELECT user_id, project_id, office_id, SUM(duration) AS tDuration
FROM users
WHERE date(start_datetime at TIME ZONE 'UTC') = '2020-05-01'
GROUP BY project_id, user_id, office_id;
and i have created index on table like this
CREATE INDEX i1_users on users (date(start_datetime at TIME ZONE 'UTC'), project_id, user_id, office_id) include (duration);
but its not doing index scan scan as all the data needed are present in index itself
the explain result is as follows
GroupAggregate (cost=7.80..7.82 rows=1 width=36) (actual time=5.672..11.735 rows=298 loops=1)
Group Key:project_id, user_id, office_id
-> Sort (cost=7.80..7.80 rows=1 width=32) (actual time=5.632..7.527 rows=298 loops=1)
Sort Key: project_id, user_id, office_id
Sort Method: quicksort Memory: 48kB
-> Index Scan using i2_users on users (cost=0.56..7.79 rows=1 width=32) (actual time=0.034..2.616 rows=298 loops=1)
Index Cond: (date(timezone('UTC'::text, start_datetime)) = '2020-05-01'::date)
Planning Time: 2.070 ms
Execution Time: 13.991 ms
I have tried vacuum analyze users as well but no luck. And when ther is lage data in the table its doing sequence scan and sorting but since in index there is sorted data why not just use that?
i2_usersas the index. Can you provide the definition ofi2_users?