I have a table with a timestamp field. The table has millions of entries from several years, and I expected to have a query by date.
It was not indexed by date, so I did it like this:
CREATE INDEX dt_crea_day_idx
ON my_table (date(dt_crea at TIME ZONE 'UTC'));
After that, the next query is taking the same time than before the index:
SELECT dt_crea::date as dt_custom, field1, field2
FROM my_table
WHERE field1='some_value'
AND dt_crea::date = '2020-04-23'
ORDER BY dt_custom desc, field2
What can I do to improve the performance of a query like this by date?
Edit: the analysis pifor asked:
Sort (cost=9616582.37..9616585.03 rows=1064 width=27) (actual time=290355.874..290355.906 rows=670 loops=1)
Sort Key: field2
Sort Method: quicksort Memory: 77kB
-> Seq Scan on my_table (cost=0.00..9616528.88 rows=1064 width=27) (actual time=72308.452..290355.232 rows=670 loops=1)
Filter: (((field1)::text = 'some_value'::text) AND ((dt_crea)::date = '2020-04-23'::date))
Rows Removed by Filter: 255195339
Planning time: 0.086 ms
Execution time: 290355.951 ms
ANALYZE my_table? Please post output ofEXPLAIN ANALYZE <your query>and date(dt_crea at TIME ZONE 'UTC') = ...