Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
MySQL is by far the most popular database management system for small- to medium-sized web projects. In this course, Bill Weinman provides clear, concise tutorials that guide you through creating and maintaining a MySQL database of your own. Bill explores the basic syntax, using SQL statements to create, insert, update, and delete data from your tables. He also covers creating a new database from scratch, as well as data types, transactions, subselects, views, and stored routines. Plus, learn about the multi-platform PHP PDO interface that will help you connect your database to web applications.
An index is a data structure that optimizes searches, sequential access, insertions, and deletions. Like many database systems MySQL uses indexes to improve performance, and to support features that require ordered data. An index keeps a logical order to a set of data. This is a cornerstone of relational database technology. If I want to find a particular row in a table, access to a fast lookup of a unique index key is going to make that very easy to do.
Internally, the data base organizes its indexes as B-trees. Most data base indexes, including those used by My SQL, use a form of B-tree to organize the data. As an example of how this works, here's a simple B-tree for looking up letters of an alphabet. Without a B-tree, if I just had a list of letters and I wanted to find the letter u, I would have to start at one end of the list, compare the first letter to the letter u. And then if it didn't match I would look at the next letter and compare it to the letter u.
And on and on until I find the letter I'm looking for. This could take up to 26 steps, assuming equal distribution of the letters it would average 13 steps. With a B-tree index, I start with the node at the top of the tree, and compare it to the letter u. If the letter u is less than the node I continue on the left side of the tree. If it's greater than the node, I continue to the right side of the tree. And with a B-Tree, I have a maximum of four steps before I find the letter I'm looking for. This is a very efficient structure for searching, as well for inserting and deleting nodes.
So, let's open up SID, and see how indexes are implemented in MySQL. We'll use the scratch database for this exercise. And I'm going to come over here to my text editor and from the chapter three exercise file, I'm going to copy and paste, you see this around line 43. I'm just going to copy and paste this whole from DROP TABLE all the way to SHOW INDEXES. And paste that into SID here. You notice I start with a DROP TABLE, that's because as we do this exercise over and over, and change a little thing here and there, I want to be able to create the table over and over again, and in order to do that I need to drop the table first.
The table is very simple, it has three columns, an INTEGER and two VARCHARs. And then I insert three rows into it. I select DESCRIBE the table and use this SHOW INDEXES statement to look at the indexes. Of course, at first we don't have any. So when I press Go, you'll see we don't actually have any indexes down there. So let's go ahead and add an index. I'm going to come up here after this b column and I need to put in a comma because these commas are separators in the create table syntax.
And then I'll type the keyword index. And in parentheses, I give it the name of the column that I want to index. And again, there's no comma after this one because it's the last item in the list. Be careful of those comma's, they will give you syntax errors. As a matter of fact, I'm just going to go ahead and put the comma in here so you see what happens. I press go, and you see we get a syntax error, and then everything else in the script doesn't work. So I need to take that comma out. And likewise if I forget to add the comma before it I'll get the same result.
It's just a different error message but it's the same result. The commas need to be exactly correct in order for this to work. And there it is. And you notice that now our last statement here, which is the SHOW INDEXES FROM test is now giving us a result. And so. We have one indexing test. It has a key name of a and a column name of a. It's a non-unique index and so, we have some other information there about it. You'll notice also, in the describe statement.
Now in that result, under key for column a, we have a multiple indication, which means that this allows multiple keys in this index. So we do have an index there. You can also create an index that operates on a combination of columns. So over here I can say a comma b and press Go. And now you notice it still just shows in the describe statement result the one multiple key index there.
But it shows two different indexes in the show index statement. And so this is an implementation detail. What we asked for, was one index that operates on both the a column and the b column, so we sort first on a. And within the a's, there would be the b's in order as well. But how that's implemented behind the scenes is with two separate axis and that's all that that means. I can name my index, and if I give it a name here, I just call this index ab like that.
And press Go. Now you notice that the key name is ab, and that's what I would use if I want to delete the index. So if I come down here and use the drop statement. DROP INDEX ab ON test and then I'll copy these two after it so we have another describe and another SHOW INDEXES after that and you can see what happens. So there is our describe and our show index is that has the index. And then query number nine, I drop the index.
And now you notice that our describe does not have the key, and our show indexes no longer has the indexes. It's also worth noting here that this SHOW INDEXES, you can just say SHOW INDEX and get exactly the same result. So there's that. My SQL has a few aliases like that just to make things convenient for you. So creating indexes is an important part of creating a table. There are other ways to create indexes, that also work with constraints, and I'll cover that in the lesson on constraints later in this chapter.
For now, we need to drop this table one more time to return our database to its original condition for the rest of the course.
There are currently no FAQs about MySQL Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.