Hi. Welcome back to modulate. This is less than 8.7 challenge to and our new bullet points on the slide. Our that we need to examine the provided query four Problems with indexes, and we must also implement improve coding style if possible.
Now with that, let's go ahead and take a look at the requirements document. They get a more precise look at what we need to accomplish. So here we are. We're at sequel Challenge to Indexes and joins. This is a modulate course challenge, and the sequel program requirements are as follows utilizing the axiomatic a database and the provided query.
Remove any full table scans.
Also, remove any bad programming styles,
and the instructions are as follows.
Examine the provided query. The query is not using available indexes. Alter the query to use available indexes.
Do not add any indexes to the database architecture.
The programmer that wrote the query was also newer and used a sub query when a regular table join would have worked just fine.
Remove the sub query and use a regular table joint to achieve the same results. And for this solution, you will need the explain command.
OKay, go ahead and pause the video and see if you can take a look at the provided query and provide a solution before reviewing the answer.
All right, let's go ahead and head over to the query and see if we can implement a solution for this requirements. Document.
All right, here we are at the problem. Query. Let's go ahead and work through this and see if we can't reach a solution that satisfies the requirement document.
So the first thing I'm going to do is just run the query, see what my results look like.
All right. Next, I'm gonna put an explain committed in front of the query and see what those results look like.
I could see that we have three layers and one of those layers. See, A has a full table scan involved, because we can see that it's a type all and see a is the cash account.
So we're gonna go ahead and work through this so that we get rid of that for sure.
And I also see the sub query here,
which is probably better implemented as a table join.
So which problems should we go at first? Well, I'm gonna go ahead and remove the sub query because it's making it a little more confusing to look at in general. And I think implementing a solution to that first will make it look a little bit cleaner.
So I'm going to do a left join.
And the reason I'm going to go with the left joint is because the previous programmer used a sub query. It would lead me to believe that he thinks that there's a possibility that there might not be a result
for the filtering.
And with the South Korea you'll get back. No. So to facilitate that same approach, I'm going to use a left join. And then if there's no result on that table, I'll also get back no again.
So I'm less join Sisi Processing Center. Gonna alias that with a c p
on c p dot company I d
c t dot company idea and
C p dot cash account idea
equals c t dot cash account I d.
So now that we have that, I should be able to erase this
and just type in CPI dot name
and then I will, of course, have toe add it to the grouping expression.
Otherwise I'll get in air
on, Let's go ahead and just run that. Make sure we're still working.
And we are.
So we've removed that sub query and we've moved into just the table joint,
and then I'm gonna wanna explain command to see if that's changed at all.
Okay, it looks like we might have made the situation a tab bit worse because now we have two full table scans. Well, let's go ahead and continue resolving the issues and see if we can make that better.
So I'm pretty confident that this is a bad thing where we're using the description as a filter.
So let's bring up that table cash account
when I go ahead and open that up
and we can see that the Description
credit card account
is actually related to the i D
So let's go ahead and implement that instead.
So we're gonna do
dot cash account. I D.
Is equal to 9 78
That's using a index now, I believe
so. We can double check that by looking at the properties going to the indexes.
We could see that the primary is the cash account i d and the company, I d
we could have potentially made the description
a index, and sometimes you will see programmers do that because then in the code,
it's much easier to tell what the name is than what the number is. If you come back and you look at this query after you've worked on it and it's some time has passed, you're not likely going to remember what 978 means. But if you've indexed the description of the title, you will be able to read it and immediately
probably understand what it means.
Well, with that said, Let's go in and it's run the explain command again and see if we fixed any problems.
Okay, so this is much better. We have a const, which is the best when you can get in the the The other two are references, which is good. It's definitely not not a table. Full table scan. So we've definitely improved this query
and we've we've optimized the code by removing a unnecessary sub query and using a join.
So that satisfies the requirements document. I hope you were able to follow along and get that working, and I hope to see in the next lesson. Thank you