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?
1 Answer
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.
2 Comments
David Chan
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
Jasen
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.
explain analyze) and you'll know.