8 ways to optimize Mysql database

1. Create an index
For applications where queries dominate, indexes are particularly important. Many times performance problems are simply caused by forgetting to add an index, or not adding a more efficient index. If no index is added, a full table scan will be performed to find even a specific piece of data. If a table has a large amount of data and few qualified results, then no indexing will cause fatal performance degradation. . However, it is not necessary to build an index under all circumstances. For example, there may only be two values ​​for gender. Building an index not only has no advantages, but also affects the update speed. This is called excessive indexing.
2. For example, a composite index
has a statement like this: select * from users where area='beijing' and age=22;
if we create a single index on area and age respectively, because mysql query can only use one index at a time Index, so although this has improved a lot of efficiency compared to full table scan without index, it will bring higher efficiency if a composite index is created on the area and age columns. If we create a composite index of (area, age, salary), then it is actually equivalent to creating three indexes (area, age, salary), (area, age), (area), which is called the best left prefix characteristic. Therefore, when we create a composite index, we should place the columns that are most commonly used as constraints on the leftmost and decrease in turn.
3. The index will not contain columns with NULL values.
As long as the columns contain NULL values, they will not be included in the index. As long as there is a column with NULL values ​​in the composite index, this column is invalid for this composite index. So we don't let the default value of the field be NULL when designing the database.
4. Use the short index
to index the string column, and specify a prefix length if possible. For example, if you have a CHAR(255) column, do not index the entire column if most of the values ​​are unique within the first 10 or 20 characters. Short indexes can not only improve query speed but also save disk space and I/O operations.
5. Sorted index problem
MySQL query uses only one index, so if the index has been used in the where clause, the column in the order by will not use the index. Therefore, do not use the sorting operation if the default sorting of the database can meet the requirements; try not to include sorting of multiple columns, and create composite indexes for these columns if necessary.
6. Like statement operation
In general, the use of like operation is not encouraged. If it must be used, how to use it is also a problem. like "%aaa%" will not use the index while like "aaa%" will use the index.
7. Do not perform operations on columns
select * from users where YEAR(adddate)<2007;
will perform operations on each row, which will cause the index to fail and perform a full table scan, so we can change it to
select * from users where adddate<'2007-01-01';
8. Do not use NOT IN and <> operations
NOT IN and <> operations will not use indexes and will perform a full table scan. NOT IN can be replaced by NOT EXISTS, and id<>3 can be replaced by id>3 or id<3. <!--endmain-->

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326349366&siteId=291194637