mysql tutorial for beginners (6/8) : Indexes
As things stand, the table students works and can be searched without problem by MySQL—until it grows to more than a couple of hundred rows,
At that point, database accesses will get slower and slower with every new row added, because MySQL has to search through every row whenever a query is issued.
This is like searching through every book in a library whenever you need to look something up.
Of course, you don’t have to search libraries that way, because they have either a card index system or, most likely, a database of their own.
The way to achieve fast searches is to add an index, either when creating a table or at any time afterward.
But the decision is not so simple. For example, there are different index types such as a regular INDEX, PRIMARY KEY, and FULLTEXT.
Also, you must decide which columns require an index, a judgment that requires you to predict whether you will be searching any of the data in that column.
And even when you’ve decided that, you still have the option of reducing index size by limiting the amount of each column to be indexed.
If we imagine the searches that may be made on the students table, it becomes apparent that all of the columns may need to be searched.
Anyway, go ahead and add an index to each of the columns, using the commands:
ALTER TABLE students ADD INDEX(name(3));
An alternative to using ALTER TABLE to add an index is to use the CREATE INDEX command.
They are equivalent, except that CREATE INDEX cannot be used for creating a PRIMARY KEY
CREATE INDEX surname ON students (surname(5));
These commands create indexes on both the name and surname columns, limiting name index to only the first 3 characters, and surname index to the first 5 characters.
For instance, when MySQL indexes the following name:
It will actually store in the index only the first 3 characters:
This is done to minimize the size of the index, and to optimize database access speed.
DESCRIBE command shows the key MUL for each column.
This key means that multiple occurrences of a value may occur within that column, which is exactly what we want, as name or surname may appear many times.
You don’t have to wait, until after creating a table to add indexes. In fact, doing so can be time-consuming, as adding an index to a large table can take a very long time.
Therefore, let’s look at a command that creates the table students with indexes already in place.
CREATE TABLE students (
Id_studnet SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(10) NOT NULL,
surname VARCHAR(20) NOT NULL,
email VARCHAR(30) NOT NULL,
Another important index, PK, its single unique key for each student to enable instant accessing of a row. The importance of having a key with a unique value for each row will come up when we start to combine data from different tables.
You can add PK, while you create the table at the first time, or later by issuing the following command:
ALTER TABLE students ADD PRIMARY KEY(id_student);
The last important index, FULLTEXT index
Unlike a regular index, MySQL’s FULLTEXT allows super-fast searches of entire columns of text.
It stores every word in every data string in a special index that you can search using “natural language,” in a similar manner to using a search engine.
It’s not strictly true that MySQL stores all the words in a FULLTEXT index, because it has a built-in list of more than 500 words that it chooses to ignore because they are so common that they aren’t very helpful for searching anyway. This list, called stopwords, includes the, as, is, of, and so on.
The list helps MySQL run much more quickly when performing a FULLTEXT search and keeps database sizes down.
FULLTEXT indexes can be created for CHAR, VARCHAR, and TEXT columns only.
A FULLTEXT index definition can be given in the CREATE TABLE statement when a table is created, or added later using ALTER TABLE (or CREATE INDEX).
Adding a FULLTEXT index to the table students for the columns name and surname
ALTER TABLE classics ADD FULLTEXT(name,surname);
this index is in addition to the ones already created and does not affect them
You can now perform FULLTEXT searches across this pair of columns.
If you find that MySQL is running slower than you think it should be when accessing your database, the problem is usually related to your indexes. Either you don’t have an index where you need one, or the indexes are not optimally designed. Tweaking a table’s indexes will often solve such a problem.
In the next tutorial, we will learn about, using FOREIGN KEY Constraints and how to join tables together.
Subscribe for more:
SWE.Safaa Al-Hayali - saf3al2a