Indexes are the primary means of accelerating queries, especially for queries involving multiple tables. In this section, you will describe the role of the index, its characteristics, and the syntax for creating and deleting indexes.
Optimizing Queries with Indexes
The index is a technique for quickly locating data, first of all by an example to understand its meaning and function, please refer to chapter 14th for a detailed introduction.
1. Index example
Suppose there are no indexes on each table for the tables built in section 10.3, and there is no regularity in the arrangement of the data, as shown in table 13.3.
Table 13.3 Students table with no indexes
Sid |
Sname |
Sgender |
Sage |
52 |
Zhang |
M |
21st |
22 |
Wang |
M |
22 |
33 |
Li |
F |
19 |
41 |
Zhao |
M |
20 |
... |
... |
... |
When looking for a student's information, you must review each row in the table students in order to check if it matches the desired value, which requires scanning the whole table and is inefficient.
Table 13.4 shows the students table with the index added to the Name column.
The index is sorted on name. Now, when looking for a student's information, you don't need to search the full table line by row, you can use the index for ordered lookups (such as binary lookup), and quickly navigate to matching values to save a lot of search time.
2. Index function
On the index column, the query efficiency can be greatly improved by using a variety of fast location technologies in addition to the ordered lookups mentioned above. In particular, when the amount of data is very large and the query involves more than one table, using an index can often speed up the query by tens of thousands of times.
For example, there are 3 unindexed tables T1, T2, T3, each containing only columns C1, C2, and C3, each containing 1000 rows of data, referring to the value of 1~1000, and the query that looks for the equivalent row of values is shown below.
SELECT c1,c2,c3 from T1,t2,t3 WHERE c1=c2 and C1=C3
The result of this query should be 1000 rows with 3 equal values per row. To process this query without indexing, you must look for all the combinations of 3 tables in order to derive those rows that match the WHERE clause. The number of possible combinations is 1000x1000x1000 (1 billion), and obviously the query will be very slow.
If you index each table, you can greatly speed up the query process. Queries using the index are handled as follows.
(1) Select the first row from the table T1 to see the data that this row contains.
(2) Use the Index on table T2 to directly locate the row in the T2 that matches the value of T1. Similarly, use the index on table T3 to directly locate rows in T3 that match the values from T1.
(3) Scan the next line of the table T1 and repeat the previous procedure until all the rows in the T1 are traversed.
In this case, a full scan is still performed on table T1, but the ability to index lookups on tables T2 and T3 directly takes rows from those tables, 1 million times times faster than unused indexes.
Using the index, MySQL accelerates the search where the clause satisfies the criteria row, while in a multi-table join query, it speeds up matching rows in other tables when the connection is executed.
3 Creating an Index
You can create an index when you execute the CREATE TABLE statement, or you can add indexes to the table by using the CREATE INDEX or ALTER TABLE alone.
(1) ALTER TABLE
ALTER table is used to create a normal index, a unique index, or a primary key index.
ALTER TABLE table_name ADD INDEX index_name (column_list)
ALTER TABLE table_name ADD UNIQUE (column_list)
ALTER TABLE table_name ADD PRIMARY KEY (column_list)
Where table_name is the name of the table to increase the index, column_list indicates which columns to index, and columns are separated by commas. Index name index_name optional, by default, MySQL assigns a name based on the first indexed column. In addition, ALTER TABLE allows you to change multiple tables in a single statement, so you can create multiple indexes at the same time.
(2) CREATE INDEX
CREATE Index to add a normal or unique index to a table.
CREATE INDEX index_name on table_name (column_list)
CREATE UNIQUE INDEX index_name on table_name (column_list)
TABLE_NAME, index_name, and column_list have the same meaning as in the ALTER TABLE statement, and the index name is not selectable. In addition, the primary key index cannot be created with the CREATE INDEX statement.
(3). Index type
When you create an index, you can specify whether the index can contain duplicate values. If not included, the index should be created as a primary key or a unique index. For single-column uniqueness indexes, this guarantees that a single column does not contain duplicate values. For multi-column uniqueness indexes, the combination of multiple values is guaranteed to be distinct.
The PRIMARY key index is very similar to a unique index. In fact, the PRIMARY key index is only a unique index with the name PRIMARY. This means that a table can contain only one primary KEY because it is not possible to have two indexes with the same name in a table.
The following SQL statement adds a primary key index to the students table on the SID.
ALTER TABLE Students ADD PRIMARY KEY (SID)
4 Deleting an index
You can use the ALTER TABLE or DROP INDEX statement to delete an index. Similar to the CREATE INDEX statement, DROP Index can be handled as a statement inside ALTER TABLE, with the following syntax.
DROP INDEX index_name on Talbe_name
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY
Where the first two statements are equivalent, delete the index index_name in table_name.
The 3rd statement is only used when deleting the primary key index, because a table may have only one primary key index, so you do not need to specify the index name. If the primary key index is not created, but the table has one or more unique indexes, MySQL deletes the first unique index.
If a column is removed from the table, the index is affected. For multiple-column combinations of indexes, if one of the columns is deleted, the column is also removed from the index. If you delete all the columns that make up the index, the entire index is deleted.
Use of indexes in MySQL