MySQL simple performance optimization

View performance parameters

Some performance parameters of the database can be seen through the following sql:

show status like 'param'

The values ​​that param can get are:

The number of times Connections connects to the mysql server

Uptime server online time

Slow_queries the number of slow queries

The number of Com_select query operations

Com_insert number of insert operations

Com_update number of update operations

Com_delete Number of delete operations

The impact of indexes on queries

1. Use like fuzzy query

If you use like fuzzy query in the where condition, the like '%abc' index will not work, only the index will work if % is not in the first position.

2. Use a joint index query

Pay attention to the leftmost prefix rule, that is, if field a, field b, and field c are joint indexes, it will only work when the where condition contains field a.

3. Use the or keyword to query

When the two conditions before and after or are both indexes, the index will work, and the query efficiency will be improved.

Optimize subqueries

When querying, try to avoid using subqueries, because subqueries will generate the results to a temporary table first, and then perform the main query. Joins can be used instead of subqueries.

Optimize database structure

The database structure involves the library table

1. Do not design too many fields in the library table. If the domain object has too many attributes, it can be split into multiple tables

2. For tables that are often queried jointly, an intermediate table can be established to improve efficiency

For example, there is a class id in the student table. If you need to query the user name and class name, you need a join operation. In this case, you can create a new table to store the student id, student name, and class name. When querying, you don't need to join, just query the table directly.

3. Add redundant fields

For example, the order table originally contains the product ID, but the product name is also redundant, so that the product name can be displayed when the order is displayed.

Analysis Table Check Table Optimization Table

1. Analysis table

analyze [local | no_write_to_binlog] table table_name

The parameters in [] have the same function, which means that the log is not written when analyzing the table.

2. Checklist

check table table_name

3. Optimize the table

optimize  [local | no_write_to_binlog] table table_name

Mainly used to eliminate file fragmentation caused by deletions and updates.

 

Guess you like

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