Home >Database >Mysql Tutorial >How to optimize performance of MySQL? Optimization Tips Sharing
When it comes to database performance optimization, the most important thing is to choose the right one. You should decide whether your application requires a relational or non-relational database. Even within one genre, you'll have multiple options to choose from. As well as relational databases, you may find Oracle, MySQL, SQL Server, and PostgreSQL. On the other hand, non-relational databases introduced MongoDB, Cassandra, and CouchDB.
You might want me to suggest using a non-relational database for faster read/write performance. However, with some improvements and tweaks, you can push a relational database beyond its known limits. So, in this article, I will introduce you some tips to make your MySQL database faster.
If you are specifically wondering why you should use MySQL, the answer is simple because it is free, open source, and very popular in the PHP community, while Oracle is not widely used due to its high price. Other options are less popular than MySQL.
- MySQL Server Configuration: \
Well, first you should know the location of the configuration file, depending on your operating system. On Linux systems, it is located in "/etc/mysql/my.cnf". \
Now it's time to choose your engine InnoDB and MyISAM. To make the choice easier, you should know that InnoDB became the default engine in MySQL 5.5 because it supports "row-level locking, foreign keys and transactions", while MyISAM does not support any of the mentioned features, which makes it rarely useful in modern applications program. \
After selecting the correct engine, it's time to set some configuration variables in the my.cnf file.
max_connection variable: \
The max_connection variable represents the number of connections allowed by the application. The default value is 151 connections, however, if you get the error message "MySQL error, too many connections..." you can easily increase this number\
最大连接数 = 170
innodb_buffer_pool_size variable: \
To speed things up, MySQL will cache the data in your server's memory. This variable tells MySQL how many GigaBytes it can use. This variable is useful if you save large blobs in the database. You can set this to 80–90% of the server's memory. So if your server has 16GB of memory, you can set it to 14GB.
innodb_buffer_pool_size = 14GB
innodb_io_capacity variable: \
This variable tells MySQL how many input/output operations it can use, and it depends on your disk. For example, a single 7200 RPM drive is limited to 200 I/O, while an enterprise SSD disk is limited to 50,000 I/O. You can easily find the input/output values from the command line on your operating system and set the variables to 90% of the available I/O so MySQL never uses too many I/O operations.
innodb_io_capacity = 21000
query_cache_limit and query_cache_size variables:\
MySQL also supports caching data in memory, but we cannot rely on it as a caching system, because every time your program requests When data is written to a database table, MySQL will rebuild the entire table's query cache. So if your program has a high load, the MySQL cache will be completely useless. In this case, it is better to set both variables to 0 to save the overhead of MySQL cache. Instead, you can use something like Redis to manage the cache.
query_cache_limit = 0 query_cache_size = 0
Slow query log:\
The slow query log will show you which of your queries exceed the threshold you define, eliminating the need to guess which query is slower. \
First, you must enable slow_query_log
in your configuration file. In the Linux server, open "/etc/mysql/my.cnf" or the equivalent file on your system. \
And add:
slow_query_log = 1 long_query_time = 1
Then these two options will enable slow query logging and log any query that takes more than one second. If you prefer to view the logs in a table instead of a file, you can add:
log_output = 'TABLE'
Then you can find your logs in the "slow_log" table. There you can see information about all slow queries that performed for more than one second. This information includes the exact execution time and number of rows affected by the query, as well as which user executed it.
Query Optimization\
After you get all the slow queries, you need a way to optimize them to make them faster. Therefore, you can add the "explain" keyword in front of the query statement to obtain detailed information about the relevant query, for example: explain select * from users where active=1;
「Explanation ” keywords help you define which indexes your query hits and the number of rows you query to get the data. This information can tell you whether you need to create more indexes or restructure the database tables.
Denormalization and constraints: \
Denormalization is the process of improving read performance by adding redundant data or grouping them. For example, if you have a "Products" table and a "Category" table, and every time you query the "Products" table, you also need to get the "Category Name" of each product. In this case, you can use "join" to retrieve "category_name". However, this means that every time the user opens the product page, a complex join query is executed. Therefore, you may consider adding a "Category Name" to the "Products" table. Despite the redundant data, the increase in read performance is worth it.
The denormalization method may cause the "Category Name" in the "Product" table to be outdated. So you need to define a "foreign key" constraint, but you need to be aware that a "foreign key" will make write performance slightly slower because MySQL needs to check the constraint before writing the data. So it is always your task to make the best choice.
English original address: https://codeburst.io/database-performance-optimization-8d8407808b5b
Translation address: https://learnku.com/mysql/t/71571
[Related recommendations: mysql video tutorial]
The above is the detailed content of How to optimize performance of MySQL? Optimization Tips Sharing. For more information, please follow other related articles on the PHP Chinese website!