9 hours 41 minutes
I welcome back to modulate. This is less than 8.9 challenge for and this challenge. We're going to be implementing partitions as well as fixing the usual problems, which is the correct index targeting removing full table scans. So let's go ahead and take a look at that requirements document
to get a better idea of what we're doing.
All right, here we are at the requirements document. This is, ah, modulate course challenge. It's a challenge for optimized query and database architecture. Utilizing the axiomatic in database and the provided query optimized the query and database structure.
The instructions are as follows. Examine the provided quarry. Remove full table scans, implement appropriate index use when possible, add partitions at all levels, partition in order off module and then company. So that's referring. Teoh the two columns. So if you have the column
with the module keyword in it,
use that column.
If you do not use the company column,
for example, if both are available, use the Module column.
Make the number of partitions equal to the unique values. So if you have five different module types on that table, make five petitions.
My sequel unfortunately, does not support foreign keys with partitions.
Remove foreign keys If they get in the way. All levels of the query should be using partitions. So at the end of this challenge, your query or the equity that's been provided should be optimized to exclude all full table scans,
implementing index use where possible and should be using partitions at all levels.
Now go ahead and pause the video, Review the query and see if you can come up with a solution on your own.
Now, with that said, let's go ahead and take a look at the query and see if you can't work through it to come up with a solution that satisfies this requirement document.
All right, here we are at the problem. Query. Let's go ahead and get started. First thing I'm going to do is run the query to see what kind of results I'm looking at. This kind of, Ah, I go off course really bad. I'll be I'll know because the results will look really bad. Okay, so it looks like
it looks like there has to be a value and either the cash amount or the debit amount.
Let's go ahead and run the explain command,
and it's like we have two full table scans and an index scan,
and it's using a temporary table. We'll see if we can get rid of that as we work through it. You do want to try to avoid using temporary if you can, but depending on database architecture and query requirements, sometimes that's just not really possible.
But we'll see if we could get rid of it.
first thing I'm going to do is copy this query. So I have a reference to what it used to be.
It's always a good idea.
I'm gonna separate this out, and I'm actually gonna bring these two and joined them with the batch table. And that will give me three joins instead of the two joints or the 1 to 2 additional joints, as opposed to just the one joint. I don't think we're getting much out of this type of table expression,
so let me go ahead and just raise that.
Don't say join
se Geo, Jill Tran
Call that G. L
on a deal. That company I d equals B, that company I D
Yale. That batch number
equals b dot bash number
equals B That module,
and we've joined the jail Tran table. So let's join the sea. A Tran Table
On c t That company I d equals B that company i D.
And C t batch number
equals B that batch number
and C t that. I think this one's original module equals B dot module.
All right, so we're going to have to change this.
So that's going to cash waas on the sea. A trans table so C t dot
And that means the debit was on the other table.
So it'll be g l dot I'm referencing the table expression to check with the column. Should be looks like debit amount,
We go ahead and run this. Make sure I'm still working correctly here
on that appears to be working just fine.
And let's put in that where
equals AP going. Not use D like statement because that's a bad style, especially when we know this is a two character field.
It doesn't make sense to use this,
and we can tell that because we go on, we look at the module, see It's two characters.
So what was this person thinking? Putting that, like, wild card and then the value? Um, we may never know what continuing on.
All right, so we're getting some results here,
And what do what else do we need to do? We need to do the company.
And I'm gonna
reverse this because I like to use the company for us. That's part of the
I think models part of the key to,
but I like to do it this way.
So good and run that.
let's go ahead and explain that. See what it looks like? Hopefully we didn't make it worse.
Okay, it looks like we don't have any full table scans, which is good,
right? We still have a temporary
now in the in. This instance, the temporary is being caused by the grouping, so we need to make sure that we're using a
A a index of company I D and batch number.
They're all using keys.
So be is using the primary key
and C T
is using the original Doc key.
What is that?
So the C T original Doc is original module.
The modules, part of that company ideas Part of that, but we're missing something.
So it's kind of like a partial index being used
because it has this and it has company, but it doesn't have the bash number. Let's go ahead and implement an index. That's all three of these fields
on the sea, a Tran table. And I think that'll fix that issue of that temporary table
company I D original module and batch number.
Let's see where that is. That's right there. Okay, save. Persist.
Let's go back and run that explain command again.
And we did. We got rid of that using temporary. So that successful got rid of all the
the full table scans. All we're missing is the partitions, and we need petitions on all the levels. So let's start with the batch table first.
So the batch table has the module,
um, field. So we're supposed to prioritize that as a partition. So that's what we're gonna dio.
So I'm gonna say, alter
And this is, of course, just getting the, um,
the syntax right, because creating a partition is actually pretty easy.
Now there is one thing that I need to mention and that is, you do need to get rid of the foreign keys, so I've already done that in this database. So what you'll do is you'll go to the batch table and you'll click the foreign keys and delete them
and then save it. If you do not do that, you will get in there. That indicates that you can't partition a table with foreign keys.
So I've already done that, so I didn't need to do that.
So altar table batch partition
So I believe module is a key on the table, and it is
Now we're supposed to add as many partitions is, there is. There is value. So let's were about to take a look at that.
group by module.
So hopefully that gives us an easy answer.
And it did. There's nine. So partitions nine.
It's my calling
good, and we're on this command.
Give it a second to run because this has to copy all the data into a new zone on the hard drive, and then it deletes the old zone, which takes a little bit more time. So let's run the explain command on this again,
and you can see that this is working pretty well. There's nine partitions, and it knows that it needs to go to partition three.
So this is more efficient. It's going to a smaller data set.
So let's go ahead and move onto the next table, which is the G l table.
So, gl Tran,
have you got any foreign keys on here? I don't see any. If you see any, you will need to get rid of those. We do have the module as an option to partition by so same things. Like module from
See how many we have? It's probably gonna be nine again, though.
So we're just gonna switch that keyword batch of this command to G. L. Tran.
We're on the partition command,
but that finish
it must have been in some extra transactions in that one. That would seem to take a little bit longer.
Yep. Is that right? Yeah, that's a lot of rose,
Oh, that looks good again. We're going to one. Partition happens to be P three again,
and that just leaves the C T table, which is the sea? A trans able Okay. Now working with C A Tran table is actually gonna present an issue. Unfortunately, we won't be a bubble Teoh. You also need to,
um, delete the foreign key.
I've already done that.
But if it's there for you, still you will need to delete that or this won't work.
We cannot use the original model because it's not a number, and it's not part of the key.
So we're going to use the company I D. Instead.
So altar table
see a tram
partitioned by key company.
Now I know there's only one company here, so this partition isn't going to make sense, but it's gonna meet the requirements. One.
Let's go ahead and run that
company. I d got a swell the field, right?
All right, run that it's running. It's done. Let's go ahead and explain.
Okay, so we have simple, select type. That's good. We're using partitions. That's good.
And the types are good.
We have a using temporary
on C T.
See if we can resolve that So temporary C company I. D. Batch number original module.
So it's using the original Doc key.
I think if we switch the key back to the um,
this one where we tell it to use this key. Then we'll get rid of the using temporary.
Okay, so let's see if we can get
rid of that using temporary by telling the execution plan which key we want to use.
I need to put it right here.
Needs it happened before your joints and your filters. If you place us in the wrong area, will not work.
So it should be used index
I was gonna go ahead and copy this out
on Tell it that I specifically want to use this index, which is the index I made that matches the keys that we're using in the joint.
Let's go in and run this and see if that fixes the issue.
It looks like it might off.
And yes, it did. OK,
so now we've implemented partitions.
Our types look good, are select types look good
and our extras look good as well.
So let's take a look at the results.
Make sure those look reasonable.
Andi, I think we're good to go. That completes the requirements of this challenge. So I hope you were able to work through that. Get it working for you. And just as a note again, it's not recommended that you really partition your table
until you have over a 1,000,000 records on the table. So it's not something you definitely you definitely don't want to be doing it on small tables.
Part of this challenge was to get you used to implementing partitions and know how they function and how to implement them. Using the explain command to guide you along the way. And we accomplish that, we solved the requirements document and I hope to see in the next lesson. Thank you.