MySQL Optimizations Query Monitoring and Tuning
MySQL drives most dynamic web content in the modern age of websites WordPress, Joomla, Drupal and most other application utilize the robust features of MySQL. Your VPS will generally come with a 'vanilla' or default install of MySQL and each site and VPS server should tune or optimize the MySQL server to their needs.
Their are many tools available either through scripts or your package manager (yum or apt) that provide MySQL information we will look at a few.
mytop
mytop is mimics the native top application in Linux but is designed for MySQL information open it gives various status pages and updates every few seconds to install the application run your package installer from SSH
Sample output:
MySQL on localhost (5.1.66-0+squeeze1) up 27+06:02:27 [19:27:23]
Queries: 3.0 qps: 0 Slow: 0.0 Se/In/Up/De(%): 5133/00/00/00
Key Efficiency: 100.0% Bps in/out: 0.0/ 0.0
Id User Host/IP DB Time Cmd Query or State
-- ---- ------- -- ---- --- --------------
292 root localhost 0 Query show full processlist
The tool will show slow queries, how many quires in real time.
MySQL Tuner
This is a script that was developed in Perl which outputs MySQL configuration information and recommends actions for performance and stability. the script can be located here MySQL Tuner
Sample output
./mysqltune.pl
>> MySQLTuner 1.2.0 - Major Hayden <major@mhtx.net>
>> Bug reports, feature requests, and downloads at http://mysqltuner.com/
>> Run with '--help' for additional options and output filtering
[OK] Logged in using credentials from debian maintenance account.
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.66-0+squeeze1
[OK] Operating on 64-bit architecture
-------- Storage Engine Statistics -------------------------------------------
[--] Status: +Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 100M (Tables: 232)
[--] Data in InnoDB tables: 1M (Tables: 52)
[!!] Total fragmented tables: 61
-------- Security Recommendations -------------------------------------------
-------- Performance Metrics -------------------------------------------------
[--] Up for: 27d 6h 5m 47s (2K q [0.001 qps], 297 conn, TX: 771K, RX: 87M)
[--] Reads / Writes: 21% / 79%
[--] Total buffers: 58.0M global + 2.7M per thread (151 max threads)
[OK] Maximum possible memory usage: 463.8M (15% of installed RAM)
[OK] Slow queries: 0% (0/2K)
[OK] Highest usage of available connections: 0% (1/151)
[OK] Key buffer size / total MyISAM indexes: 16.0M/108.5M
[OK] Key buffer hit rate: 100.0% (26M cached / 25 reads)
[OK] Query cache efficiency: 24.6% (154 cached / 625 selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 4 sorts)
[OK] Temporary tables created on disk: 24% (164 on disk / 679 total)
[OK] Thread cache hit rate: 99% (1 created / 297 connections)
[!!] Table cache hit rate: 1% (64 open / 3K opened)
[OK] Open file limit used: 9% (96/1K)
[OK] Table locks acquired immediately: 100% (1K immediate / 1K locks)
[OK] InnoDB data size / buffer pool: 1.1M/8.0M
-------- Recommendations -----------------------------------------------------
General recommendations:
Run OPTIMIZE TABLE to defragment tables for better performance
Enable the slow query log to troubleshoot bad queries
Increase table_cache gradually to avoid file descriptor limits
Variables to adjust:
table_cache (> 64)
MySQL Tuning Primer Script
Finally we have MySQL Tuning Primer Script this script takes 'Show status like' and 'Show variable like' and then attempts to provide recommendations to your MySQL Configuration. the script can be downloaded here MySQL Tuning Primer Script to run this tool you would also need bc installed through your package manager eg apt-get install bc
Sample output
./primer.sh all
-- MYSQL PERFORMANCE TUNING PRIMER --
- By: Matthew Montgomery -
MySQL Version 5.1.66-0+squeeze1 x86_64
Uptime = 27 days 6 hrs 24 min 46 sec
Avg. qps = 0
Total Questions = 3008
Threads Connected = 1
Server has been running for over 48hrs.
It should be safe to follow 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 NOT enabled.
Current long_query_time = 10.000000 sec.
You have 0 out of 3029 that take longer than 10.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 = 8
Current threads_cached = 0
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine
MAX CONNECTIONS
Current max_connections = 151
Current threads_connected = 1
Historic max_used_connections = 1
The number of used connections is 0% of the configured maximum.
You are using less than 10% of your configured max_connections.
Lowering max_connections could help to avoid an over-allocation of memory
See "MEMORY USAGE" section to make sure you are not over-allocating
INNODB STATUS
Current InnoDB index space = 1 M
Current InnoDB data space = 1 M
Current InnoDB buffer pool free = 64 %
Current innodb_buffer_pool_size = 8 M
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 : 44 M
Configured Max Per-thread Buffers : 405 M
Configured Max Global Buffers : 42 M
Configured Max Memory Limit : 447 M
Physical Memory : 3.00 G
Max memory limit seem to be within acceptable norms
KEY BUFFER
Current MyISAM index space = 108 M
Current key_buffer_size = 16 M
Key cache miss rate is 1 : 1053872
Key buffer free ratio = 81 %
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 = 16 M
Current query_cache_used = 69 K
Current query_cache_limit = 1 M
Current Query cache Memory fill ratio = .42 %
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 = 1024 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 = 64 tables
Current table_definition_cache = 256 tables
You have a total of 307 tables
You have 64 open tables.
Current table_cache hit rate is 1%
, while 100% of your table cache is in use
You should probably increase your table_cache
You should probably increase your table_definition_cache value.
TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 16 M
Of 802 temp tables, 25% were created on disk
Perhaps you should increase your tmp_table_size and/or max_heap_table_size
to reduce the number of disk-based temporary tables
Note! BLOB and TEXT columns are not allow in memory tables.
If you are using these columns raising these values might not impact your
ratio of on disk temp tables.
TABLE SCANS
Current read_buffer_size = 128 K
Current table scan ratio = 37 : 1
read_buffer_size seems to be fine
TABLE LOCKING
Current Lock Wait ratio = 0 : 3284
Your table locking seems to be fine
Optimizations
Now you can edit your my.cnf file and add really any value or setting, the thing to keep in mind is to be very careful as some incorrect settings can cause more problems than solutions.