15 Useful MySQL/MariaDB Performance Tuning and Optimization Tips - Part 3
Abstract: 8. Configure MySQL query_cache_size If you have many repetitive queries and your data does not change often – use query cache. People often do not und
If you have many repetitive queries and your data does not change often – use query cache. People often do not understand the concept behind the query_cache_size
and set this value to gigabytes, which can actually cause degradation in the performance.
The reason behind that is the fact that threads need to lock the cache during updates. Usually value of 200-300 MB should be more than enough. If your website is relatively small, you can try giving the value of 64M and increase in time.
You will have to add the following settings in the MySQL configuration file:
query_cache_type = 1 query_cache_limit = 256K query_cache_min_res_unit = 2k query_cache_size = 80M9. Configure tmp_table_size and max_heap_table_size
Both directives should have the same size and will help you prevent disk writes. The tmp_table_size
is the maximum amount of size of internal in-memory tables. In case the limit in question is exceeded the table will be converted to on-disk MyISAM table.
This will affect the database performance. Administrators usually recommend giving 64M for both values for every GB of RAM on the server.
[mysqld] tmp_table_size= 64M max_heap_table_size= 64M10. Enable MySQL Slow query Logs
Logging slow queries can help you determine issues with your database and help you debug them. This can be easily enabled by adding the following values in your MySQL configuration file:
slow-query-log = 1 slow-query-log-file = /var/lib/mysql/mysql-slow.log long_query_time = 1
The first directive enables the logging of slow queries, while the second one tells MySQL where to store the actual log file. Use long_query_time
to define the amount of time that is considered long for MySQL query to be completed.
Idle connections consume resources and should be interrupted or refreshed when possible. Such connections are in 「sleep」 state and usually stay that way for long period of time. To look for idled connections you can run the following command:
# mysqladmin processlist -u root -p | grep 「Sleep」
This will show you list of processes that are in sleep state. The event appears when the code is using persistent connection to the database. When using PHP this event can appear when using mysql_pconnect which opens the connection, after that executes queries, removes the authentication and leaves the connection open. This will cause any per-thread buffers to be kept in memory until the thread dies.
The first thing you would do here is to check the code and fix it. If you don’t have access to the code that is being ran, you can change the wait_timeout
directive. The default value is 28800 seconds, while you can safely decrease it to something like 60:
wait_timeout=60Pages: 1 2 3 4