(RPM) Types and pros and cons of MySQL indexes

Source: Internet
Author: User
Tags mysql in mysql index

Now, we introduce the database index, its advantages and disadvantages. The characteristics and application of MySQL index are described in detail. This paper analyzes how to avoid MySQL unusable, how to use explain to analyze query statements, and how to optimize the application of MySQL index. This digest is from section 8.9 of the "MySQL 5 authoritative Guide" (3rd).

An index is a special kind of file (an index on a InnoDB data table is an integral part of a table space), and they contain reference pointers to all records in the datasheet.
Note: [1] The index is not omnipotent! Indexing can speed up data retrieval operations, but it slows down data modification operations. Each time a data record is modified, the index must be refreshed once. In order to compensate for this flaw in some program, many SQL commands have a delay_key_write entry. The purpose of this option is to temporarily stop MySQL from updating the index immediately after each new record is inserted and after each modification of the command, the refresh of the index will wait until all records have been inserted/modified. The Delay_key_write option is very obvious when you need to insert many new records into a data table. [2] In addition, the index will occupy a considerable amount of space on the hard disk. Therefore, you should only index the most frequently queried and most frequently sorted columns of data. Note that if a data column contains a lot of duplicate content, indexing it does not have much practical effect.
Theoretically, you can create an index for each field in the data table, but MySQL limits the total number of indexes in the same table to 16.
1. Index of the INNODB data table
Indexes are significantly more important to INNODB data than MyISAM data tables. On the InnoDB data table, the index is much more important to the INNODB data table. On the InnoDB data table, the index will not only play a role in searching data records, but also the Acenaphthene and foundation of the data row-level locking mechanism. "Data row-level locking" means that the individual records being processed are locked during the execution of a transactional operation and are not accessible to other users. This lock will affect (but is not limited to) SELECT ... LOCK in SHARE MODE, SELECT ... The for UPDATE command and the INSERT, UPDATE, and delete commands.
For efficiency reasons, data row-level locking of INNODB data tables actually occurs on their indexes, not on the data table itself. Obviously, the data row-level locking mechanism works only when the relevant data table has an appropriate index to lock on.
2. Restrictions
If the Wehere clause has a non-equal sign in the query condition (WHERE coloum! =), MySQL will not be able to use the index.
Similarly, if a function is used in the query condition of the WHERE clause (where day (column) = ...), MySQL will not be able to use the index.
In a JOIN operation (when data needs to be extracted from multiple data tables), MySQL can use the index only if it has the same data type as the primary key and foreign key.
If you use the comparison operator like and regexp,mysql in the query condition of the WHERE clause, you can use the index only if the first character of the search template is not a wildcard. For example, if the query condition is like ' abc% ', MySQL will use the index, and if the query condition is like '%abc ', MySQL will not use the index.
In an order by operation, MySQL uses the index only if the sort condition is not a query-condition expression. (although, in the case of multiple data-table queries, those indexes have little effect in speeding up the ORDER by, even if there are indexes available)
If a data column contains many duplicate values, it does not have a good effect even if the index is indexed. For example, there is no need to create an index for a data column if it contains a list of values such as "0/1″ or" y/n.

Normal index, unique index, and primary index
1. General Index
The only task for a normal index (an index defined by the keyword key or index) is to speed up access to the data. Therefore, it should be only for those that most often appear in the query condition (where column = ...). or the data column in the sort condition (order by column) to create the index. Whenever possible, you should choose a data column that is the most tidy and compact data (such as an integer type of data column) to create an index.
2. Unique index
A normal index allows the indexed data column to contain duplicate values. For example, because a person might have the same name, it may occur two or more times in the same employee profile data table.
If you can determine that a data column will contain only values that are different from each other, you should define it as a unique index with the keyword unique when creating an index for that data column. The advantage of this is that it simplifies MySQL's management of the index, which makes it more efficient, and that MySQL automatically checks to see if the value of this field in the new record has already appeared in the field of a record when a new record is inserted into the data table; MySQL will refuse to insert that new record. In other words, a unique index guarantees the uniqueness of the data record. In fact, in many cases, the goal of creating a unique index is often not to improve access speed, but to avoid duplication of data.
3. Primary index
It has been repeated several times before: You must create an index for the primary key field, which is called the "primary Index". The only difference between a primary index and a unique index is that the first keyword used in the definition is primary rather than unique.
4. Foreign KEY Index
If you define a FOREIGN key constraint for a foreign key field, MySQL defines an internal index to help you manage and use foreign key constraints in the most efficient way.
5. Composite Index
An index can overwrite multiple data columns, such as indexes such as index (ColumnA, COLUMNB). The feature of this index is that MySQL can selectively use one such index. If the query operation only needs an index on the ColumnA data column, you can use the composite Index, index (ColumnA, COLUMNB). However, this usage applies only to combinations of data columns that are arranged in the composite index. For example, index (a, B, c) can be used as an index to a or (a, b), but not as an index to B, C, or (b, c).
6. Length of index
When you define an index for a data column of char and varchar type, you can limit the length of the index to a given number of characters (this number must be less than the maximum number of characters allowed for this field). The advantage of this is that you can generate an index file that is smaller in size and faster to retrieve. In most applications, the string data in the database is dominated by a variety of names, and the length of the index is set to 10~15 characters enough to narrow the search to a few data records.
When you create an index for a data column of blob and text type, you must limit the length of the index, and the maximum allowable index length for MySQL is 255 characters.

Full-Text Indexing
A normal index on a text field can only speed up the retrieval of the string that appears at the front of the field content, that is, the character at the beginning of the field content. If a field contains a large paragraph of text consisting of several or even multiple words, the normal index does not work. This kind of retrieval often appears in the form of like%word%, which is very complex for MySQL, and if the amount of data to be processed is large, the response time will be very long.
Such occasions are where full-text indexing (Full-text index) can take its place. When this type of index is generated, MySQL creates a list of all the words that appear in the text, and the query operation retrieves the relevant data records based on the list. A full-text index can be created with a data table, or it can be added using the following command when necessary later:
ALTER TABLE tablename ADD fulltext (Column1, Column2)
With full-text indexing, you can use the Select query command to retrieve data records that contain one or more given words. The following is the basic syntax for this type of query command:
SELECT * FROM TableName
WHERE MATCH (Column1, Column2) against (' word1′, ' word2′, ' word3′ ')
The above command will query all data records for Word1, Word2, and Word3 in the Column1 and Column2 fields.
Note: InnoDB data tables do not support full-text indexing.

optimization of queries and indexes
Only when there is enough test data in the database, its performance test results have practical reference value. If there are only hundreds of data records in the test database, they are often loaded into memory after executing the first query command, which will make subsequent query commands execute very quickly-regardless of whether the index is used or not. The performance test results of a database are meaningful only if the database has more than 1000 records and the total amount of data exceeds the total amount of memory on the MySQL server.
People can often get some help from the explain select command when they are unsure of which data columns to create indexes on. This simply adds a explain keyword as a prefix to an ordinary select command. With this keyword, MySQL is not going to execute that select command, but to parse it. MySQL will list information such as the execution of the query and the index (if any) that is used in the form of a table.
In the output of the explain command, the 1th column is the name of the data table read from the database, sorted by the order in which they were read. The Type column specifies the association relationship (JOIN) between this data table and other data tables. Among the various types of associations, the most efficient system, followed by const, EQ_REF, ref, range, index, and all, means that all records in the data table must be read once, corresponding to each record in the previous data table- This situation can often be avoided by an index).
The Possible_keys data column gives the various indexes that MySQL can use when searching for data records. The Key data column is the actual mysql-selected index, which is given in the Key_len data column for the length of the byte count. For example, for an index of an integer data column, the byte length would be 4. If a composite index is used, the Key_len data joins can also see which parts of MySQL it uses. As a general rule, the smaller the value in the Key_len data column, the better (meaning faster).
The Ref data column gives the name of the data column in the other data table in the association relationship. The row data column is the number of data rows that MySQL expects to read from this data table when it executes this query. The product of all the numbers in the row data column gives us a rough idea of how many combinations this query needs to handle.
Finally, the extra data column provides more information about the join operation, for example, if MySQL had to create a temporary data table when executing the query, it would see the word using temporary in the extra column.

(RPM) Types and pros and cons of MySQL indexes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.