Home > Database > Mysql Tutorial > body text

Mysql indexing and optimization

黄舟
Release: 2017-02-06 10:19:29
Original
1063 people have browsed it

1. Select the data type of the index

MySQL supports many data types. Choosing the appropriate data type to store data has a great impact on performance. Generally speaking, some guidelines can be followed:

(1) Smaller data types are usually better: Smaller data types usually require less space on disk, memory, and CPU cache. Processing is faster.
(2) Simple data types are better: Integer data has less processing overhead than characters, because the comparison of strings is more complex. In MySQL, you should use the built-in date and time data types instead of strings to store time; and use integer data types to store IP addresses.
(3) Try to avoid NULL: the column should be specified as NOT NULL, unless you want to store NULL. In MySQL, columns containing null values ​​are difficult to optimize queries because they complicate indexes, index statistics, and comparison operations. You should replace null values ​​with 0, a special value, or an empty string.

1.1. Select identifier
It is very important to choose the appropriate identifier. When choosing, you should not only consider the storage type, but also how MySQL performs operations and comparisons. Once a data type is selected, it should be ensured that all related tables use the same data type.
(1) Integer: Usually the best choice as an identifier, because it can be processed faster and can be set to AUTO_INCREMENT.

(2) String: Try to avoid using strings as identifiers, they consume more space and are slower to process. Also, in general, strings are random, so their position in the index is also random, which can lead to page splits, random access to disk, and clustered index splits (for storage engines using clustered indexes).

2. Introduction to Indexes
For any DBMS, indexes are the most important factor for optimization. For a small amount of data, the impact of not having a suitable index is not great, but as the amount of data increases, the performance will drop sharply.
If multiple columns are indexed (combined index), the order of the columns is very important. MySQL can only perform an effective search on the leftmost prefix of the index. For example:
Assume that there is a combined index it1c1c2(c1,c2), and the query statement select * from t1 where c1=1 and c2=2 can use this index. The query statement select * from t1 where c1=1 can also use this index. However, the query statement select * from t1 where c2=2 cannot use this index because there is no leading column of the combined index. That is, if you want to use column c2 for search, c1 must be equal to a certain value.

2.1. Type of index
Index is implemented in the storage engine, not in the server layer. Therefore, the indexes of each storage engine are not necessarily exactly the same, and not all storage engines support all index types.
2.1.1, B-Tree Index
Assume there is the following table:

CREATE TABLE People (
     last_name varchar(50)    not null,
     first_name varchar(50)    not   null,
     dob        date           not null,
     gender     enum('m', 'f') not   null,
     key(last_name, first_name, dob)
);
Copy after login

Its index contains the last_name, first_name and dob columns of each row in the table. Its structure is roughly as follows:

Mysql indexing and optimization

#The values ​​stored in the index are arranged in the order in the index column. You can use the B-Tree index to query all keywords, keyword ranges and keyword prefixes. Of course, if you want to use the index, you must ensure that you query by the leftmost prefix of the index.
(1)Match the full value: Specify specific values ​​for all columns in the index. For example, the index in the picture above can help you find Cuba Allen who was born on 1960-01-01.
(2) Match a leftmost prefix: You can use the index to find the person whose last name is Allen, using only the first column in the index.
(3) Match a column prefix: For example, you can use the index to find people whose last name starts with J. This only uses the first column in the index.
(4) Match a range of values ​​query: You can use the index to find people whose last name is between Allen and Barrymore, and only use the first column in the index.
(5) Match one part exactly and match a range on another part (Match one part exactly and match a range on another part): You can use the index to find people whose last name is Allen and whose first name starts with the letter K.
(6) Index-only queries: If the queried columns are all in the index, there is no need to read the value of the tuple.
Since the nodes in the B-tree are stored sequentially, you can use the index to search (find certain values), or you can ORDER BY the query results. Of course, there are some limitations to using B-tree indexes:
(1) The query must start from the leftmost column of the index. This point has been mentioned many times. For example, you cannot use an index to find people born on a certain day.
(2) An index column cannot be skipped. For example, you cannot use an index to find a person whose last name was Smith and who was born on a certain day.
(3) The storage engine cannot use the columns to the right of the range condition in the index. For example, if your query statement is WHERE last_name="Smith" AND first_name LIKE 'J%' AND dob='1976-12-23', the query will only use the first two columns in the index because LIKE is a range query .

The above is the content of Mysql indexing and optimization. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template