MYSQL index and query optimizer _ MySQL

Source: Internet
Author: User
MYSQL index and query optimization program database is a collection of data, which is closely related to the mathematical set theory.

To increase the query speed, we can:

Add an index to a data table to accelerate search;

Use programming skills to maximize the use of indexes;

Optimize query statements so that the server can respond to requests from multiple customers as quickly as possible.

Study the hardware processing process to reduce physical constraints.

2.1. index
Indexing is the most important technology in relational data query. To improve database performance, index optimization should be considered first. Because it can maximize the performance of our database.

Advantages of indexes:

A table without indexes is a set of unordered data. to query data, you need to perform a full table scan. An indexed table sorts data tables on an index column. you can use an index to quickly locate records. In MyISAM and ISAM data tables, data rows are stored in data files, and indexes are stored in index files. BDB and InnoDB data tables place data and indexes in the same file.

In multi-table join queries, indexes play a greater role. If no index exists, in the worst case, the number of full table scans may be the combination of data rows in each table, which may be an astronomical number. Such queries are destructive and may cause database paralysis.

For queries using the MIN () or MAX () function, if the related data column has an index, MySQL can directly find the rows with the maximum and minimum values, you do not need to check data rows one by one.

The index accelerates the operation of the order by and group by clauses.

When you query data on a numeric data column and the column has an index, the index allows MySQL to read data rows directly from the index.

Disadvantages of indexing:

The index occupies disk space.

The index slows down the insert, delete, and modify operations on the index data column.

Selection of index columns

Indexes should be created on the data columns involved in operations such as search, sorting, and grouping. That is to say, in the where clause, data columns that appear in the from clause, order by, or group by clause in the association search are most suitable for creating indexes.

Try to use a unique index, which can make the index take the best effect.

Use a shorter value for indexing. When you index a string, you should specify a prefix length, such as sorting the first 10 or 20 characters of the string, instead of using dozens of characters in the entire string for index sorting. This reduces disk I/O and improves processing speed. The most important thing is that the shorter the key value, the more key values are contained in the index buffer, and the more indexes MySQL stores in the memory at the same time, the higher the index buffer hit rate. Of course, it does not make sense to index the first character of a data column.

Make full use of the leftmost prefix. The leftmost prefix is the index column with the most edges in the composite index. Such as composite index (a, B, c), where a is the leftmost prefix. It is the index with the highest usage and needs to be carefully selected.

Do not create too many indexes. indexes consume system resources.

Indexes are mainly used for comparison operations such as <, <=, =, >=,>, and BETWEEN. Therefore, indexes should be created on the data columns related to such operations.

You can use the slow query log to find the query with poor performance. you can use mysqldumpslow to view the log. Indexes can be used to accelerate queries with poor performance.

2.2. query optimization program
When we send a query command, MySQL analyzes it to optimize the query. Put the explain language name before the query to display the query execution route, which provides useful information for the optimization query. The following principles help the system select and use indexes:

Try to compare columns of the same type. For example, VARCHAR (5) and VARCHAR (5) are of the same type, and CHAR (5) and VARCHAR (5) are of different types.

Try to make the index data column appear in the comparison expression separately. do not include it in a function or complex expression. Otherwise, the index does not work.

Do not use wildcards at the beginning of LIKE mode. For example, % string %.

For MyISAM and BDB data tables, use the analyze table statement to ANALYZE the distribution of index key values, providing more valuable information for the optimization program. Another method is to use the myisamchk -- analyze (for MyISAM tables) or isamchk -- analyze (for ISAM tables) command.

Use the EXPLAIN statement to analyze the execution efficiency of the query statement. Check whether the index used in the query can quickly exclude non-conforming data rows. if not, try to use STRAIGHT_JOIN to force relevant data tables to be joined in the specified order.

Different query methods are used to compare the running conditions.

Do not abuse the automatic conversion of MySQL types. Automatic conversion slows down queries and invalidates related indexes.

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.