I welcome back to modulate This is less than 8.6 challenge one. Now we've already gone over the top bullet points, but the bottom to identify what where we are going to do in this challenge, we're going to use explain to identify problems in the provided query,
and it's also gonna be our goal to remove all full table scans
Now. With that, let's go ahead and head over to the requirements document and look at the details of what we need to dio. All right, here we are at the requirements document. This is intermediate sequel Challenge one. Optimize quarry, no full table scans, and this is challenged one for modulate.
So the sequel program requirements are as follows, utilizing the axiomatic a database and the provided query. Remove any and all full table scans. Senior programmer instructions
Examine the providing query.
We want to ensure that code we push out is at very least optimized to a reasonable standard.
Remove any and all full table skins. You may add indexes if necessary. You can also rearrange the query and two equivalent expressions.
Patterns observed in the data can be assumed toe always be true. Now if you are in a production area, you, of course, will want to confirm that a pattern is always true. But here we could just assume that we if we observe a pattern, it's always true.
So the things that we will need to solve this problem include the explain command,
new indexes and the sub string function.
So go ahead and Paul's the video and see if you can solve it on your own before reviewing the answer.
Okay, let's head over and see how we solve this problem.
All right, we have the query in front of us. Let's see if we can work through this and find the full table scans and remove them to get started with. I'm just gonna run the query with the explain at the top.
I can see that I have to levels and they are both of type. All which is not good. It means doing a full table scan on both levels.
let's jump in the middle here on this derive table and put the explain command in there and just run this portion.
We can see that we still have a type of all.
Let's see if we can resolve that somewhere. Somehow. That's coming from the PM Tran table, and it's using the company I D and batch number. So let's go look at that table
and see if we haven't any indexes that have those two columns
and I seek plenty of company I ds. But I don't see any batch numbers. So let's go ahead and add that to create a new index company I D
and batch number. I can find it.
We put in alphabetical order. That should make it easier.
There it is
and hit. OK,
I didn't put on unique. I'm pretty sure it's not a unique configuration. So I'm going to save that.
It looks like I have an index with a knit with the same name already.
So I'm just gonna go ahead and type in an underscore to here,
and I'm just going to save that.
now I'm gonna run explain on this query again.
Now I have a reference
that's not as good as it could be.
I know this wild card is dangerous on both ends because it causes the sequel engine to have to do a lot of checks. So let's get rid of that on the left and see if that improves things even more.
So now we have a range, which is better than a breath,
but I think we can even make it even even better.
Let's see if we can do this. Let's use the sub string command
and I'm gonna copy this field out,
started position one and go to out,
and that is required to equal
AP because that's obviously what we're looking for.
And I've made that determination
because if I look at the pattern of the table
on the batch number, I can see that AP starts at Position one and goes to Ford,
and we're allowed to assume that these patterns are always true. So
I'm gonna use the substrate command.
So let's see, that improves things again.
So we're back to reference.
Okay, I double checked and references actually better than range, so that's a better condition to be in.
Now. Let's get rid of this, explain command here and rerun the whole query
and see if we have any improvements
and we do. We can see that one of the layers is still referenced.
However, it appears that the outer layer batch is not doing a full table scan. Now we're using the columns company I D and posted.
Let's go look at the batch table and see if we can improve that by targeting an index or creating an index.
So we need company I D and posted.
Okay, again, we have plenty of company I d. But no posted. Let's go ahead and create a new index.
So a company I d
I'm confident they aren't unique.
I'm gonna hit okay
and save it.
let's go back.
Run the whole query again.
And they are both of type reference.
So we have successfully removed all the table scans and that completes the requirement document.
So the way we solve this is that we implemented some new indexes and we made it a little bit better by using the sub string function to check the value of a field as opposed to using a wild card, which would take just a tad bit more computation to ah, do all those checks. So I hope you're able to get that working, and I hope to see in the next lesson.