0

I was wondering, when dealing with large tables (> 100 million rows)

  1. Does Postgres handle work well with indexing timestamps
    (e.g: 2018-10-09T12:41:20.276846Z)?

  2. Will it improve performance if I truncate the date to the seconds if I don't care about that much precision when selecting?

2
  • Are you looking for an exact value match, or for a range of values? Can you give some examples of the queries you will be using? Commented Oct 9, 2018 at 15:23
  • Welcome to Stack Overflow! At StackOverflow, we are helping developers to get stronger. However, we won't never let company get rid of them by requesting free jobs on this forum. So in order to distinct yourself from these sharks, show us some effort, pieces of code, some bugs, or other... And then we will help you! Please take the tour and read "What types of questions should I avoid asking?" help center, "What topics can I ask about here?" help center and minimal reproducible example! Commented Oct 9, 2018 at 15:35

1 Answer 1

3

Indexes on timestamps work very well in PostgreSQL, because timestamps have a total ordering.

No matter what precision your timestamp value has, it will always occupy 4 bytes, so the precision has no influence on the index size or performance.

You have to make sure that your index can be used with the condition in your queries. For that, the condition must be of the form

<indexed expression> <operator> <constant>

where <constant> need not really be a constant, but have the same value for the duration of the index scan.

<operator> can be =, <, >, <= or >=.

Sign up to request clarification or add additional context in comments.

2 Comments

More info on date/time storage requirements: postgresql.org/docs/9.2/static/…
a common rewrite I find myself doing is using ts < now() - '30 days' instead of ts + interval '30 days' < now()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.