7 hours 36 minutes
Hi. Welcome back to you. Module three sequel programming this is less than five tables were moving into sub lesson 5.6 indexes. So what is an index? And index is a very important feature off most database engines that will allow, said database engine to very quickly search for records
and this importance or this feature kidnap. The importance of this feature cannot be overstated.
As the application grows in size, some applications, if not properly indexed, will almost outright fail in the eye of the users. Now, something to realize is that when you make a column, a primary I D.
It becomes indexed.
Now you might be saying, Well, okay, that's good. I'm done. I haven't index to column. Well, not necessarily.
You have to think of how the users and how the programmers are going to be searching for data. For example, consider a call recording program that stores the call recorded calls in a database. Well, they're gonna have an I D field,
but what are users going to use to search for calls?
And you probably guessed right, the phone number, they're going to search by the phone number. So in such an example, would be a very, very good idea to make sure the phone number is indexed. I once worked with a call recording application that recorded about 10 to 30,000 calls a day. So within a year you would have
a few 1,000,000 recorded phone call records to search through
and, if not properly, index
the database could take is long as
15 to 30 minutes, depending on the computer hardware to search for a phone call. And that would just be unacceptable for that. When properly indexed, that same search can resolve in
a second or two, maybe less, very quick. So how do indexes work? Well, indexes can get complicated. They can rely on data structures to go be on the scope of this course, such as binary trees.
But one easy example. Oven away. An index could work that really highlights how fast it can cut down the search. Time is, for example, consider a table where you have 10 million phone calls
Now what is a phone number? It's a number so we can put those in ascending order. So let's say we put those phone calls in ascending order. Now let's say we have to search those timid and calls for one phone number.
What can we do? We can say, Okay, let's go to the middle of that power. Love the five million Call.
And let's ask if this phone number is less than or equal to that phone number. That's in that five million. It's a parole, that five million rope.
And if it's true or false, we can immediately cut off the bottom or top five million. So what have we done in that one comparison? By being able to jump to the middle? Because we know that those are in ascending order.
We were able to immediately dismiss five million of the records and cut it down from 10 million to five million. If you don't have an index, the database is forced to search every record row by row so he can't cut out five million. It has to search those five million.
Now, if you get very, very lucky and you happen to be, you know, it positioned one for records and that happens to be the phone number you're searching for, then you know, it might appear to be very quick, but if you get unlucky and you have to search for
Ah, the phone call at the nine millionth record. You're probably going to be waiting a while now. How do we establish an index
in this database? Was very easy.
For example, let's go to the dogs table. Now we know that the idea is the primary key and its index. But what other
comb might we need to search by?
Well, we have very few comes so honestly,
we might need to search by all of these. In fact, I would probably index all of these, but we're not going to one. That's obvious
is the person I d. So let's go ahead and make that an index now. Andi Beaver. We'll just click on the index tab.
We can see that the primary key and the foreign keys are already indexed,
so it already index the person I D
and index the idea, which is good.
Those were kind of the obvious indexes. So if you set up your foreign keys on your part, MCI's you're already getting a lot of your index thing done.
So let's create a new index, though
let's say breed because we want to be ableto quickly. Search for different breeds.
we're not gonna mess with any of the different types. Binary tree or be tree is usually good enough and we're going ahead and click. Okay,
We're gonna save that,
and we can see the sequel code that creates the index. It's not very complicated by itself. It's great. Index on
Create Index. The index name using the the object type, which will be be treated, stands for buying territory on database table name
on this column. Breathe
so I'll go ahead and click. Persist.
Now what the database has to do is have to go and put those in order so that it respects the rules off thea
So that can take a little bit if you already have a lot of records
in your database,
and now we can see that we have that
and we're done.
So that brings this listen to close. And this lesson we discuss indexes and their importance. They help keep an application speedy. If you see a slow application that uses the database and index is a very good place to start looking when trying to figure out why it might be running slow
with that said, I hope you were able to get your index inserted into your table, and I hope to see you in the next lesson. Thank you.