MySQL Tuning Scripts

MySQL tuning is essential if you are running high volume websites that run on MySQL. You will need this two scripts to help your MySQL tuning.

I’m running a lot of high volume WordPress websites and apart from getting the advantages from caching with APC or memcached I’m tuning my MySQL server from time to time. For that purpose I’m using this two scripts:

ADVERTISEMENT

tuning-primer.sh – MySQL Tuning Primer Script

You can get this script from launchpad https://launchpad.net/mysql-tuning-primer

Running tuning-primer.sh will gave you this output:

MySQL Version 5.1.63-0+squeeze1-log x86_64

Uptime = 1 days 8 hrs 11 min 3 sec
Avg. qps = 23
Total Questions = 2736077
Threads Connected = 3

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.1/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 enabled.
Current long_query_time = 2.000000 sec.
You have 511 out of 2736106 that take longer than 2.000000 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.1/en/point-in-time-recovery.html

WORKER THREADS
Current thread_cache_size = 128
Current threads_cached = 11
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine

MAX CONNECTIONS
Current max_connections = 110
Current threads_connected = 2
Historic max_used_connections = 13
The number of used connections is 11% of the configured maximum.
Your max_connections variable seems to be fine.

INNODB STATUS
Current InnoDB index space = 75 M
Current InnoDB data space = 398 M
Current InnoDB buffer pool free = 57 %
Current innodb_buffer_pool_size = 1.00 G
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory

MEMORY USAGE
Max Memory Ever Allocated : 2.75 G
Configured Max Per-thread Buffers : 2.16 G
Configured Max Global Buffers : 2.50 G
Configured Max Memory Limit : 4.66 G
Physical Memory : 4.86 G
Max memory limit exceeds 90% of physical memory

KEY BUFFER
Current MyISAM index space = 437 M
Current key_buffer_size = 768 M
Key cache miss rate is 1 : 30252
Key buffer free ratio = 72 %
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 = 768 M
Current query_cache_used = 753 M
Current query_cache_limit = 8 M
Current Query cache Memory fill ratio = 98.15 %
Current query_cache_min_res_unit = 4 K
However, 2990 queries have been removed from the query cache due to lack of memory
Perhaps you should raise query_cache_size
MySQL won't cache query results that are larger than query_cache_limit in size

SORT OPERATIONS
Current sort_buffer_size = 8 M
Current read_rnd_buffer_size = 2 M
Sort buffer seems to be fine

JOINS
Current join_buffer_size = 2.00 M
You have had 419459 queries where a join could not use an index properly
You have had 221564 joins without keys that check for key usage after each row
You should enable "log-queries-not-using-indexes"
Then look for non indexed joins in the slow query log.
If you are unable to optimize your queries you may want to increase your
join_buffer_size to accommodate larger joins in one pass.
Note! This script will still suggest raising the join_buffer_size when
ANY joins not using indexes are found.

OPEN FILES LIMIT
Current open_files_limit = 32888 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_open_cache = 16384 tables
Current table_definition_cache = 16384 tables
You have a total of 1517 tables
You have 2447 open tables.
The table_cache value seems to be fine

TEMP TABLES
Current max_heap_table_size = 512 M
Current tmp_table_size = 512 M
Of 1109845 temp tables, 16% were created on disk
Created disk tmp tables ratio seems fine

TABLE SCANS
Current read_buffer_size = 8 M
Current table scan ratio = 15020 : 1
read_buffer_size seems to be fine

TABLE LOCKING
Current Lock Wait ratio = 1 : 565
You may benefit from selective use of InnoDB.

I need to do this:

As you can see there are a few things i need to change. My MEMORY USAGE is too high Max memory limit exceeds 90% of physical memory I can fix this with lowering my max_connections because as i can see i’m using only 11% of the value The number of used connections is 11% of the configured maximum. 

Also i need to increase the query_cache_size because it’s almost full Current Query cache Memory fill ratio = 98.15 %

I have also a lot of joins that are not using indexes but I will not increase the join_buffer_size because I know that this is from some bad query from one Joomla website with bad plugins and this error will continue.

mysqltuner.pl – High Performance MySQL Tuning Script

You can get this script from github: https://raw.github.com/major/MySQLTuner-perl/master/mysqltuner.pl

Debian users can also get it using APT

apt-get install mysqltuner

Running mysqltuner.pl will gave you this output:

-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.63-0+squeeze1-log
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 460M (Tables: 1225)
[--] Data in InnoDB tables: 398M (Tables: 260)
[--] Data in MEMORY tables: 0B (Tables: 9)
[!!] Total fragmented tables: 324

-------- Performance Metrics -------------------------------------------------
[--] Up for: 1d 8h 17m 11s (2M q [23.770 qps], 76K conn, TX: 33B, RX: 1B)
[--] Reads / Writes: 76% / 24%
[--] Total buffers: 3.0G global + 20.1M per thread (110 max threads)
[!!] Maximum possible memory usage: 5.2G (106% of installed RAM)
[OK] Slow queries: 0% (517/2M)
[OK] Highest usage of available connections: 12% (14/110)
[OK] Key buffer size / total MyISAM indexes: 768.0M/437.1M
[OK] Key buffer hit rate: 100.0% (1B cached / 48K reads)
[OK] Query cache efficiency: 67.9% (1M cached / 2M selects)
[!!] Query cache prunes per day: 2542
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 701K sorts)
[!!] Joins performed without indexes: 644113
[OK] Temporary tables created on disk: 16% (215K on disk / 1M total)
[OK] Thread cache hit rate: 99% (14 created / 76K connections)
[!!] Table cache hit rate: 16% (2K open / 15K opened)
[OK] Open file limit used: 9% (3K/32K)
[OK] Table locks acquired immediately: 99% (3M immediate / 3M locks)
[OK] InnoDB data size / buffer pool: 398.2M/1.0G

-------- Recommendations -----------------------------------------------------
General recommendations:
 Run OPTIMIZE TABLE to defragment tables for better performance
 Reduce your overall MySQL memory footprint for system stability
 Increasing the query_cache size over 128M may reduce performance
 Adjust your join queries to always utilize indexes
 Increase table_cache gradually to avoid file descriptor limits
Variables to adjust:
 *** MySQL's maximum memory usage is dangerously high ***
 *** Add RAM before increasing MySQL buffer variables ***
 query_cache_size (> 768M) [see warning above]
 join_buffer_size (> 2.0M, or always use indexes with joins)
 table_cache (> 16384)

I need to look at the Recommendations:

As you can see I’m getting the same warning and recommendations plus that i need to increase my table_cache this is because my table hit rate is low: [!!] Table cache hit rate: 16% (2K open / 15K opened).

Also i have fragmented tables that i need to OPTIMIZE.

By looking at the output of this two scripts you can do your MySQL Tuning more effectively. Just don’t forget that on every change you need to run your MySQL server for at least one day(recommended 48 hours)

MySQL Tuning Primer Scripthttps://launchpad.net/mysql-tuning-primer
High Performance MySQL Tuning Scripthttps://raw.github.com/major/MySQLTuner-perl/master/mysqltuner.pl