I welcome back to modulate. This is less than 8.8 challenge three and this challenge. We're going to examine the provided quarry for optimization problems, so we're definitely continuing. That trend of analyzing queries are analyzing the provided queries for opportunities to optimize them.
This is definitely a good module, good lessons to pay attention to,
because if you get good at optimizing queries that you're going to be excellent at just writing queries in general now. That said, let's head over to the requirements document and see what we need to do to solve this challenge. All right, here we are at the requirements document. So let's go ahead and read through this. It does look like it's short, concise and to the point,
which you know is nice sometimes.
So the requirements are as follows utilizing the automatic A database and the provided query. Optimize the query.
Programmer instructions are. Examine the provided quarry. Remove full table scans, implement appropriate index use wind possible add indexes if necessary.
All right, well, go ahead and pause the video. Take a look at the provided query and see if you can solve the optimization problems with the query on your own before reviewing the solution.
Now let's go ahead and review the query and see if we can't work through a solution.
Okay, here we are at the quarry. So the first thing I'm gonna do is just run the query to see what kind of results I get.
Next, I'm going to go ahead and tag in the explain command just to see what's going on.
You see that we have what looks like to be three
and there's also something else that's pretty bad here. Is it using foul sort? Using temporary is also considered bad, but sometimes it's necessary, especially when you have ah, group by statements. But we'll see what we can do about this. I know we're going to need to get rid of the file, sort,
because that is considered a bad thing to have. That means they cannot sort.
You cannot use the index to start the results, so it's ah,
it's a time consuming process.
With that said, let's go ahead and move deeper into this query. I'll go ahead and put explain on this query.
We already know this is bad. We shouldn't be doing a sub query
Teoh in a filter expression.
run the explain. We'll see that there's a sub query in the select type. You want to avoid those those air time consuming. This one is probably not too bad. But in general, you definitely want Teoh avoid using sub queries in your filters when possible.
So the easiest way to do that is move that into a joint.
So because it's in the filtering section, I'm going to go ahead and use an inner join.
keystrokes right here on C the company I d
he peed a company idea,
All right. And because that is the apparently the source table for companies, there's not gonna be another key to use.
And we're gonna use it in the filter.
Go ahead and get rid of this.
So we can see that I have
full table scans in this and I still have full
So one thing I can say for sure, and you do see this from time to time and it's definitely a good idea to practice it, because the more you practice, the more you get used to write enquiries and working in sequel and you'll notice things that just simply don't belong. For example, this order by that's not doing anything for us because we're using this derive table
in the query. So this order by has no positive impact for us. Let's go ahead and get rid of that run. Explain again.
so I'm pretty sure this company CD is not an index, which gives us a choice. Can you make it in index, or can you move to a index field?
I have a feeling that we want to use the name here again. Its preference. There's no,
um, necessarily answer. That's much better than the other. We could switch to the company I 82 but then we won't get the word. So that's less intuitive. Toe what that means. I'm gonna go ahead and make this company CD and index.
So property create new Index
I'm gonna save that. Persist that
and go back and run the explain on this query again.
Okay, We still got one more all type
and that is on DP. And what is DP DP is et department
using the description.
I don't believe that's going to be an index.
So again, that's not an index.
If we look at the data, we can see the description is finance and the department ideas also finance because they're so similar. I'm just gonna go ahead and switch to the I D.
Okay, go ahead and run. Explain on this one again.
Using where? Using where? Index. Okay, so this query, this table expression seems to be as good as it gets. So I'm gonna go ahead and get explain out of they're gonna run the whole explain again, see if anything changes.
And it already looks like we're making a lot of progress. We only have one more type of all to get rid off.
So let's see what we can do here.
No, go ahead and explain this.
The second table expression.
See, we got any problems in here.
Get a type all file sore, temporary. Go ahead and get rid of this order by again. That's not doing anything positive for us.
Let's go ahead and do it. Explain
using temporary. We might need that just because we have the group by statements and with table expressions mangling it mangled into the whole query. Could be really tough getting rid of that used temporary in that extra column. But we do have type. All.
So what can we do about that?
This from PM Tran. We're focused on the company I d and the B account I d Let's go ahead and see if those are index fields.
and go to properties. We goto indexes.
So we're looking for a B account. I D and company i d.
So I'm just gonna go ahead and old. I'm going to go ahead and add it.
be count. I d. Okay, Save persist,
since I already have that name. So I will just put a three
Run that explain again.
Okay, so now we got a type of index, which is better.
someone go ahead and leave that as is gonna run the explain on the full query again.
So we do have a type of index down here,
so that's a little bit better than a full table scan,
but I think we could make it. Ah, make it more efficient.
So I'm gonna get rid of this. Where be account ideas? No.
Let's see what explains says after that. Still, the same thing we're gonna do is I'm gonna go ahead and use this,
um, table expression at the top within the transactions table expression.
So I'm going to join
on e dot company I d
eat at the account. I d
on. We go ahead and run that again.
Let's see what we have.
So we don't have any
Any full table scans were now using the indexes. All this type columns looks much better. The only issue I see here is using the temporary, but that's coming from the group by statement
would be really tough to get rid off. So we're gonna leave that as is,
and I think that solves our problem. All right. I hope you were able to get that working. I hope you got all those index problems solved. The next challenge will be looking at partitions, so there'll be a little extra in there that completes this challenge. And I hope to see you in the next. Thank you