1

I have a date field on a large table that I mostly query and sort in DESC order. I have an index on that field with the default ASC order. I read that if an index is on a single field it does not matter if it is in ASC or DESC order since an index can be read from both directions. Will I benefit from changing my index to DESC?

2
  • 1
    Check the execution plan (use explain analyze) and you'll know. Commented Jan 5, 2015 at 15:14
  • You probably read here: Since an ordered index can be scanned either forward or backward, it is not normally useful to create a single-column DESC index — that sort ordering is already available with a regular index. postgresql.org/docs/current/static/… Commented Jan 5, 2015 at 15:38

1 Answer 1

3

operating systems are generally more efficient reading files in a forwards direction, so you may get a slight speed up by creating a DESC index.

For a big speed up create the DESC index and CLUSTER the table on it.

CLUSTER tablename USING indexname;

clustering on the ASC index will also give improvement, but it will be less.

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

2 Comments

I am dubious of the claim that sequential file handling by the OS will somehow affect the speed of a single-column index. It is after all handled as a binary search which does random access anyway. Do you have any docs or sources on this matter? I would be very interested
disk drives are faster for sequential reads, anything that promotes sequential reads will engance performance. if the table is and expected result set is large enough that an index scan is chosen for the query haveing the part of the query that reads the table do so mostly in a sequential order is going to boost performance.

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.