In this tutorial, we are going to show you some techniques for tuning MySQL performance.
First thing first, we need to know how to check MySQL config value from a console to perform various tasks below.
To check MySQL config value from the console, we can use the command below:
mysqld --help --verbose | grep xxxx
Replace xxxx
with the desired configuration name, for example, slow_query_log
.
As an application grows, its data size grows along. Sooner or later, the application's query speed will slow down. This is when we can use the procedures below to debug a slow query from MySQL.
mysql -u root -p
use my_database
SET GLOBAL slow_query_log = 'ON';
X
here will limit the engine to log query exceeding the 'X' seconds:
SET GLOBAL long_query_time = X;
show variables like '%slow%’;
SELECT SLEEP(X);
Now you are ready to debug slow queries from your application. And when the debugging is done, make sure you turn off the slow query log by following the steps below:
SET GLOBAL slow_query_log = 'OFF';
show variables like '%slow%’;
You will not want to leave the log open as it will result in a performance issue.
MySQL cache query results to improve its performance. We want to turn this cache off when debugging an application.
You can use the methods below to turn its cache off:
my.cnf
to:query_cache_limit = 0;
query_cache_size = 0;
The trick here is the keyword SQL_NO_CACHE
when running a query.
select SQL_NO_CACHE * from mytable where 1 limit 1;