I was searching the internet for pointers on tuning my MySQL server at work and ran across 2 different tuning scripts. The first is this wonderful script from http://www.day32.com/MySQL/. Simply save it to your sql server and ensure it is executable. A brief description from the author's website is:

This script takes information from "SHOW STATUS LIKE..." and "SHOW VARIABLES LIKE..." to produce sane recomendations for tuning server variables. It is compatable with all versions of MySQL 3.23 and higher (including 5.1).

Currently it handles recomendations for the following:

  • Slow Query Log
  • Max Connections
  • Worker Threads
  • Key Buffer
  • Query Cache
  • Sort Buffer
  • Joins
  • Temp Tables
  • Table (Open & Definition) Cache
  • Table Locking
  • Table Scans (read_buffer)
  • Innodb Status
  • Below is showing the results after a few rounds of tuning on my server.

    [tethys]:/home/rnejdl/Scripts> ./tuning-primer.sh 
    mysqld is alive                                   
                 - By: Matthew Montgomery -                                         
    MySQL Version 5.0.75 i386
    Uptime = 7 days 2 hrs 29 min 21 sec
    Avg. qps = 1                       
    Total Questions = 638990           
    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:
    Visit http://www.mysql.com/products/enterprise/advisors.html                    
    for info about MySQL's Enterprise Monitoring and Advisory Service               
    The slow query log is NOT enabled.                                              
    Current long_query_time = 5 sec.                                                
    You have 0 out of 639011 that take longer than 5 sec. to complete               
    Your long_query_time seems to be fine                                           
    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
    Current thread_cache_size = 4                                                   
    Current threads_cached = 3                                                      
    Current threads_per_sec = 0                                                     
    Historic threads_per_sec = 0                                                    
    Your thread_cache_size is fine
    Current max_connections = 35                                                    
    Current threads_connected = 1                                                   
    Historic max_used_connections = 24                                              
    The number of used connections is 68% of the configured maximum.                
    Your max_connections variable seems to be fine.
    Max Memory Ever Allocated : 160 M                                               
    Configured Max Per-thread Buffers : 166 M                                       
    Configured Max Global Buffers : 46 M                                            
    Configured Max Memory Limit : 212 M                                             
    Physical Memory : 1.99 G                                                        
    Max memory limit seem to be within acceptable norms
    Current MyISAM index space = 39 M                                               
    Current key_buffer_size = 4 M                                                   
    Key cache miss rate is 1 : 4642                                                 
    Key buffer fill ratio = 8.00 %                                                  
    Your key_buffer_size seems to be too high.
    Perhaps you can use these resources elsewhere
    Query cache is enabled
    Current query_cache_size = 32 M                                                 
    Current query_cache_used = 8 K                                                  
    Current query_cache_limit = 2 K                                                 
    Current Query cache Memory fill ratio = .02 %                                   
    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
    Current sort_buffer_size = 4 M                                                  
    Current read_rnd_buffer_size = 256 K                                            
    Sort buffer seems to be fine
    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
    Current open_files_limit = 11095 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
    Current table_cache value = 750 tables
    You have a total of 608 tables
    You have 609 open tables.
    The table_cache value seems to be fine
    Current max_heap_table_size = 192 M
    Current tmp_table_size = 192 M
    Of 551 temp tables, 23% were created on disk
    Created disk tmp tables ratio seems fine
    Current read_buffer_size = 256 K
    Current table scan ratio = 22 : 1
    read_buffer_size seems to be fine
    Current Lock Wait ratio = 1 : 254749
    Your table locking seems to be fine

    I've tested this on my home server and it has successfully recommended a much smaller memory foot print (although the photo album can peak my SQL traffic). I've also tested on my server at work which averages at around 100 queries per second (qps). The suggested changes were all great and did measurably increase the speed of SQL or limit wasteful use of memory.

    The second is mysqltuner.pl which easily enough can be retrieved as such:

    wget mysqltuner.pl

    Very nice domain! Simply download it and then chmod it to 755 and execute it. Output looks as such:

    [tethys]:/home/rnejdl> ./mysqltuner.pl             
     >>  MySQLTuner 1.0.0 - Major Hayden 
     >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
     >>  Run with '--help' for additional options and output filtering         
    -------- General Statistics --------------------------------------------------
    [--] Skipped version check for MySQLTuner script                              
    [OK] Currently running supported MySQL version 5.0.77                         
    [OK] Operating on 32-bit architecture with less than 2GB RAM                  
    -------- Storage Engine Statistics -------------------------------------------
    [--] Status: -Archive +BDB -Federated +InnoDB -ISAM -NDBCluster               
    [--] Data in MyISAM tables: 54M (Tables: 590)                                 
    [!!] InnoDB is enabled but isn't being used                                   
    [!!] BDB is enabled but isn't being used                                      
    [!!] Total fragmented tables: 3                                               
    -------- Performance Metrics -------------------------------------------------
    [--] Up for: 1d 11h 13m 51s (168K q [1.325 qps], 13K conn, TX: 91M, RX: 12M)  
    [--] Reads / Writes: 89% / 11%                                                
    [--] Total buffers: 270.0M global + 4.8M per thread (35 max threads)          
    [OK] Maximum possible memory usage: 436.2M (21% of installed RAM)             
    [OK] Slow queries: 0% (0/168K)                                                
    [OK] Highest usage of available connections: 25% (9/35)                       
    [OK] Key buffer size / total MyISAM indexes: 4.0M/39.3M
    [OK] Key buffer hit rate: 99.7% (234K cached / 632 reads)
    [OK] Query cache efficiency: 81.5% (92K cached / 113K selects)
    [OK] Query cache prunes per day: 0
    [OK] Sorts requiring temporary tables: 0% (0 temp sorts / 2K sorts)
    [OK] Temporary tables created on disk: 25% (16 on disk / 63 total)
    [OK] Thread cache hit rate: 99% (40 created / 13K connections)
    [OK] Table cache hit rate: 89% (611 open / 679 opened)
    [OK] Open file limit used: 0% (82/11K)
    [OK] Table locks acquired immediately: 99% (29K immediate / 29K locks)
    -------- Recommendations -----------------------------------------------------
    General recommendations:
        Add skip-innodb to MySQL configuration to disable InnoDB
        Add skip-bdb to MySQL configuration to disable BDB
        Run OPTIMIZE TABLE to defragment tables for better performance
        Enable the slow query log to troubleshoot bad queries
        When making adjustments, make tmp_table_size/max_heap_table_size equal
        Reduce your SELECT DISTINCT queries without LIMIT clauses
    Variables to adjust:
        tmp_table_size (> 192M)
        max_heap_table_size (> 192M)

    This script advises differently from the first. First, it points to the number of tables that need to be defragmented. If you need to defragment your databases, this is the syntax I use:

    mysqlcheck -or -A -u USERNAME --password=PASSWORD

    The above will optimze and repair all of your databases as best as possible. Some other adjustments this had me make was to increase the size of my queries that can be cached as well as increase my table cache size. Both tools together seem to work well together. As I find more, I'll udpate this article with any more suggestions I find.