Simple query refactor - 100x faster
TL;DR - Recently set out to speed up a slow query behind a multi-column cursor paginated endpoint. A simple switch to represent the where filters as a tuple makes a huge difference in performance on the exact same data with the same indices. Eg - change where ( a > c or (a = c and b > d )) to where (a, b) > (c, d). In the “tuple” case postgres can use the index more efficiently and get more of the needed rows using the index condition, versus walking it, reading rows in and filtering.
Read more