It’s not just for SQLite, Aaron is a veritable fount of SQL knowledge. The tips and pointers given in this course will surely help you decide what to handle on the db vs the app side.Hans Verschooten
Build with and integrate SQLite into your production applications.
I've made my test data available for you to use and follow along.
Left to right no skipping stops at the first range. You thought you were free, it's gonna come back to haunt you over and over and over. Once you unlock part of an index, you can use it for anything. So if we had select star from users where first name equals Aaron, now we have access to first name and to last name to do ordering. So if we were to order by last name descending, let's put a throw a limit on that.
You'll see search using index multi first name equals question mark. But we don't see last name in there so how do we know how do we know that last name, was How do we know that the index was used to order by last name? Well, unfortunately, it doesn't tell us that but we can, we can kinda back into it. So instead of doing by last name, let's order by birthday. And according to our rules, birthday is not accessible because it's got that last name in the middle.
We got first name, last name and then birthday and we're trying to use birthday. There you go. Using a temp b tree for order by. So in this case, SQLite is telling us, man, I wasn't able to order it straight out of the index, so I'm gonna construct my own b tree. If we were to do this again and use something that's not in the index at all, let's just do created at, you'll see the exact same thing.
Use temp b tree for order by. This means that once and, last name equals Francis, this means that once the birthday is unlocked we can then order by it, order by birthday descending. And you see that temporary b tree is gone. So we've put a strict equality on first name and on last name and now we're able to order by first name, last name, or birthday. And in this case, we chose birthday.
So let's try this again and say last name is less than Francis which is a little bit silly but, it's good for this demonstration. So we'll say order by last name. You see, it's still not using that temporary b tree because we've unlocked the ability to use last name out of the index and so it's just going to order by in fact, it's already ordered. The data is already ordered over in the index and so it's just gonna read it out of the index. Left to right, no skipping.
Always the rule. And once an index part is unlocked or has become available, you can use that for ordering and you still get to use the index advantaged method rather than constructing the temporary b tree. So remember, left to right, no skipping stops at the first range. Once those parts are free, you can use them for ordering.