mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-25 20:14:21 +01:00
Made a new PR based on @sivukhin 's PR #2869 that had a lot of conflicts. You can check out the PR description from there. ## The main idea is: Before, if we had an index on `x` and had a query like `WHERE x > 100 and x < 200`, the plan would be something like: ``` - Seek to first row where x > 100 - Then, for every row, discard the row if x >= 200 ``` This is highly wasteful in cases where there are a lot of rows where `x >= 200`. Since our index is sorted on `x`, we know that once we hit the _first_ row where `x >= 200`, we can stop iterating entirely. So, the new plan is: ``` - Seek to first row where x > 100 - Then, iterate rows until x >= 200, and then stop ``` This also improves the situation for multi-column indexes. Imagine index on `(x,y)` and a condition like `WHERE x = 100 and y > 100 and y < 200`. Before, the plan was: ``` - Seek to first row where x=100 and y > 100 - Then, iterate rows while x = 100 and discard the row if y >= 200 - Stop when x > 100 ``` This also suffers from a problem where if there are a lot of rows where `x=100` and `y >= 200`, we go through those rows unnecessarily. The new plan is: ``` - Seek to first row where x=100 and y > 100 - Then, iterate rows while x = 100 and y < 200 - Stop when either x > 100 or y >= 200 ``` Which prevents us from iterating rows like `x=100, y = 666` unnecessarily because we know the index is sorted on `(x,y)` - once we hit any row where `x>100` OR `x=100, y >= 200`, we can stop. Closes #3644