Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to index a date field in postgres?

I have a few tables with about 17M rows that all have a date column I would like to be able to utilize frequently for searches. I am considering either just throwing an index on the column and see how things go or sorting the items by date as a one time operation and then inserting everything into a new table so that the primary key ascends as the date ascends.

Since these are both pretty time consuming I thought it might be worth it to ask here first for input.

The end goal is for me to load sql queries into pandas for some analysis if that is relevant here.

like image 964
massphoenix Avatar asked Oct 29 '16 15:10

massphoenix


People also ask

Should you index a date field?

Adding an index on date column definitely increases performance.

Which index is best in PostgreSQL?

B-tree indexes B-tree is the default index in Postgres and is best used for specific value searches, scanning ranges, data sorting or pattern matching.

Should we create index on timestamp?

Yes, with the new unique constraint you're good. If clustering by the timestamp is important the cost may be worth it.

Are unique indexes faster?

A unique index guarantees that the table won't have more than one row with the same value. It's advantageous to create unique indexes for two reasons: data integrity and performance. Lookups on a unique index are generally very fast.


1 Answers

The index on a date column makes sense when you are going to search the table for a given date(s), e.g.:

select * from test
where the_date = '2016-01-01';
-- or

select * from test
where the_date between '2016-01-01' and '2016-01-31';
-- etc

In these queries there is no matter whether the sort order of primary key and the date column are the same or not. Hence rewriting the data to the new table will be useless. Just create an index.

However, if you are going to use the index only in ORDER BY:

select * from test
order by the_date;

then a primary key integer index may be significantly (2-4 times) faster then an index on a date column.

like image 57
klin Avatar answered Sep 25 '22 10:09

klin