Background
My blog that uses MySQL as the database backend suddenly got real performance issues. The symptoms where:
- All my Nagios test failed from time to time with NRPE timeout
- The performence of my dynamic webpages was terrible slow.
- The load on the system showed very heavy load, more then 10 for 1, 5, and 15 minutes.
- When using “top” the CPU load on both CPUs was almost 100% and it was almost only httpd processes.
- According to Firefox plugin Firebug it took about 12 seconds to load the startpage with me as the only user.
This bad behavior did not show up when I pointed my webbrowser to static http pages so my conclusion was that I had som problem with MySQL.
The numbers of unique visitors are about 100 / day and 250 page views /day and the machine is:
- Dual PIII 1Ghz
- 4 Gb ram
- SCA 10k RPM disk.
So the hardware should be able to handle the load without any problems.
Solution
The natural way of finding a solution to this was to Google and see if anybody else has run into this problem. The easiest way seemed to be to turn of WordPress plugins I use to collect statistics. I found a couple of webpages describing howto tune MySQL, but they where assuming deeper knowledge in MySQL then I have. I also tried the MySQL manuals and I came to the conclusion that I probably must spend quite much time to understand the behavior and how to tune MySQL.
In my opinion my MySQL implementation is quite simple and the need to tune it seemed overhelming, until I find a very neat script that helped my to set MySQL parameters:
http://www.day32.com/MySQL/tuning-primer.sh
After tuning MySQL with the hints I got from the script the system started to behave totally differently:
- The load changed to load average to less then 0.15
- “top” shows more or less 100% idle
- Load a blog webpage changed from 12 seconds down to 1,2 seconds
- No more NRPE timeout problems from Nagios
A run of the script looks like this AFTER tuning:
[root@dull mysql-tuning]# ./tuning-primer.sh mysqld is alive -- MYSQL PERFORMANCE TUNING PRIMER -- - By: Matthew Montgomery - MySQL Version 5.0.45 i686 Uptime = 0 days 0 hrs 14 min 28 sec Avg. qps = 6 Total Questions = 5926 Threads Connected = 9 Warning: Server has not been running for at least 48hrs. It may not be safe to use these recommendations To find out more information on how each of these runtime variables effects performance visit: http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html Visit http://www.mysql.com/products/enterprise/advisors.html for info about MySQL's Enterprise Monitoring and Advisory Service SLOW QUERIES The slow query log is NOT enabled. Current long_query_time = 3 sec. You have 0 out of 5947 that take longer than 3 sec. to complete Your long_query_time seems to be fine BINARY UPDATE LOG The binary update log is NOT enabled. You will not be able to do point in time recovery See http://dev.mysql.com/doc/refman/5.0/en/point-in-time-recovery.html WORKER THREADS Current thread_cache_size = 10 Current threads_cached = 2 Current threads_per_sec = 0 Historic threads_per_sec = 0 Your thread_cache_size is fine MAX CONNECTIONS Current max_connections = 60 Current threads_connected = 9 Historic max_used_connections = 11 The number of used connections is 18% of the configured maximum. Your max_connections variable seems to be fine. MEMORY USAGE Max Memory Ever Allocated : 103 M Configured Max Per-thread Buffers : 161 M Configured Max Global Buffers : 74 M Configured Max Memory Limit : 235 M Physical Memory : 3.39 G Max memory limit seem to be within acceptable norms KEY BUFFER Current MyISAM index space = 4 M Current key_buffer_size = 32 M Key cache miss rate is 1 : 101 Key buffer fill ratio = 1.00 % Your key_buffer_size seems to be too high. Perhaps you can use these resources elsewhere QUERY CACHE Query cache is enabled Current query_cache_size = 32 M Current query_cache_used = 288 K Current query_cache_limit = 32 M Current Query cache Memory fill ratio = .87 % Current query_cache_min_res_unit = 4 K Your query_cache_size seems to be too high. Perhaps you can use these resources elsewhere MySQL won't cache query results that are larger than query_cache_limit in size SORT OPERATIONS Current sort_buffer_size = 2 M Current read_rnd_buffer_size = 256 K Sort buffer seems to be fine JOINS Current join_buffer_size = 132.00 K You have had 0 queries where a join could not use an index properly Your joins seem to be using indexes properly OPEN FILES LIMIT Current open_files_limit = 2470 files The open_files_limit should typically be set to at least 2x-3x that of table_cache if you have heavy MyISAM usage. Your open_files_limit value seems to be fine TABLE CACHE Current table_cache value = 1200 tables You have a total of 115 tables You have 122 open tables. The table_cache value seems to be fine TEMP TABLES Current max_heap_table_size = 128 M Current tmp_table_size = 128 M Of 518 temp tables, 24% were created on disk Created disk tmp tables ratio seems fine TABLE SCANS Current read_buffer_size = 128 K Current table scan ratio = 78 : 1 read_buffer_size seems to be fine TABLE LOCKING Current Lock Wait ratio = 1 : 3415 You may benefit from selective use of InnoDB.
3 Responses to “MySQL Tuning”
Leave a Reply
You must be logged in to post a comment.
March 4th, 2009 at 3:01 pm
I found another script that might help tuning MySQL:
http://wiki.mysqltuner.com/MySQLTuner
/Peter
April 5th, 2009 at 8:16 am
Connection id: 124890
Current database: *****aa
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ”
Using delimiter: ;
Server version: 4.1.20
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 32 days 21 hours 50 min 23 sec
Threads: 1 Questions: 3439860 Slow queries: 161 Opens: 56965 Flush tables: 1 Open tables: 64 Queries per second avg: 1.210
————–
Can Anybody Help me . How to reduce slow queries????