Index scan backward vs index scan
While troubleshooting a server with very high I/O wait, I notice there is a lot of I/O coming from queries which do SELECT max(x) FROM t WHERE y = ?
.
My index is btree (x, y)
.
I notice that the query plan does Index Scan Backward to get the max. Is that bad? Should I worry about that and perhaps add another index (reversed)? Or is there a better way to create an index suitable开发者_开发百科 for this type of queries?
No it's not bad, it takes the same amount of time to start with the first index page as it would take to start with the last index page. You can see the "difference" when creating an descending index, using DESC.
An index (y,x) would probably be better for this query.
The index is sorted, with the lowest value first. To find the max value, a backward index scan would find the maximum value first :).
I assume a SELECT min(x) would result in a normal index scan, does it?
精彩评论