mysql database optimization

1. Choose the right storage engine

Taking MySQL as an example, there are two storage engines MyISAM and InnoDB, each of which has advantages and disadvantages.

MyISAM is suitable for some applications that require a lot of queries, but it is not very good for a lot of write operations. Even if you only need to update a field, the entire table will be locked, and other processes, even the read process, cannot operate until the read operation is completed. Also, MyISAM is super fast for calculations like SELECT COUNT(*).

InnoDB tends to be a very complex storage engine, and for some small applications, it will be slower than MyISAM. He is that it supports "row lock", so it will be better when there are more write operations. And, he also supports more advanced applications, such as: transactions.

2. Optimize the data type of the field

Remember as a rule that smaller columns are faster. For most database engines, disk operations are probably the most significant bottleneck. So, compacting your data can be very helpful in this situation, as it reduces access to the hard drive.

If a table has only a few columns (such as dictionary table, configuration table), then we have no reason to use INT as the primary key, it will be more economical to use MEDIUMINT, SMALLINT or smaller TINYINT. If you don't need to keep track of time, using DATE is much better than DATETIME. Of course, you also need to leave enough room for expansion.

3. Add an index to the search field

Indexes are not necessarily for primary keys or unique fields. If there is a field in your table that you will always use for searching, it is best to build an index for it, unless the field you want to search is a large text field, then you should build a full-text index.

4. Avoid using Select*

The more data is read from the database, the slower the query becomes. And, if your database server and web server are two separate servers, it will also increase the load of network transmission. Even if you want to query all the fields of the data table, try not to use * wildcards. It may be more convenient to make good use of the built-in field exclusion definitions.

5. Use ENUM instead of VARCHAR

The ENUM type is very fast and compact. In reality, it holds a TINYINT, but appears to be a string. In this way, using this field to do some list of options becomes quite perfect. For example, fields such as gender, ethnicity, department, and state have limited and fixed values, so you should use ENUM instead of VARCHAR.

6. Use NOT NULL as much as possible

Unless you have a very specific reason to use NULL values, you should always keep your fields NOT NULL. NULL actually requires extra space, and, when you compare, your program will be more complicated. Of course, this does not mean that you can't use NULL. The reality is very complicated. There are still some cases where you need to use NULL.

7. Fixed-length tables will be faster

If all fields in a table are "fixed-length", the entire table is considered "static" or "fixed-length". For example, there are no fields of the following types in the table: VARCHAR, TEXT, BLOB. As long as you include one of these fields, the table is not a "fixed-length static table", so the MySQL engine will handle it in a different way.

Fixed-length tables will improve performance, because MySQL will search faster, because these fixed lengths are easy to calculate the offset of the next data, so the read will naturally be fast. And if the field is not fixed length, then, every time you want to find the next item, you need the program to find the primary key.

Also, fixed-length tables are easier to cache and rebuild. However, the only side effect is that fixed-length fields will waste some space, because fixed-length fields are allocated that much space whether you use them or not.

Using the "vertical split" technique, you can split your table into two, one of fixed length and one of variable length.

8. Vertical division

"Vertical splitting" is a method of changing the table in the database into several tables by column, which can reduce the complexity of the table and the number of fields, so as to achieve the purpose of optimization.

For example: there is a field in the User table that is home address, this field is an optional field, in contrast, and you do not need to read or rewrite this field frequently except personal information when you are operating in the database. So, why not put him in another table? This will make your table have better performance. Think about it, a lot of times, for the user table, only the user ID, user name, password, user role, etc. will be used frequently. A smaller table will always have good performance.

In addition, what you need to pay attention to is that the table formed by these separated fields, you will not join them frequently, otherwise, the performance will be worse than when not divided, and it will be a pole number grade decline.

9. EXPLAIN your SELECT query;

Using the EXPLAIN keyword lets you know how MySQL handles your SQL statement. This can help you analyze the performance bottleneck of your query statement or table structure. EXPLAIN query results will also tell you how your index primary keys are used, how your data tables are searched and sorted...etc, etc.

Usually we can add the keyword EXPLAIN to the front of the more complex SELECT statement, especially involving multiple tables. You can use phpmyadmin to do this.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327057753&siteId=291194637