The CVM Algorithm
November 11, 2024
Everything you need to know about query planning can be understood from this query:
SELECT * FROM xy WHERE y = 3 ORDER BY x
Imagine we have two indexes, one ordered on y, and one ordered on x. Then two possible plans suggest themselves:
Plan 1: scan the y index, restricting it to the values of y such that y = 3, then sort the result by x.
Plan 2: scan the entire x index, selecting any rows having y = 3. The result will be "naturally" sorted by x, with no need for an extra sort....
Read more at buttondown.com