Today, there is almost no application that does not use some kind of storage. Experts say that if you use NoSQL, usually 80% of the data goes to NoSQL but around 20% goes to RDBMS. There are a lot of systems where 100% of the data goes to RDBMS. MySQL is one of the most used databases in the world. To take full advantage of ACID, InnoDB is often used. In order to gain better performance few parameters needs to be changed after installation. Of course there are a lot of parameters that can be tuned, but only few are crucial. Here is an architectural view if InnoDB table.
Before you start it is very important to understand how linux and swappiness works. The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. If swappiness=0 it will avoid swapping until the memory is full. If swappiness=100 it will aggressively swap processes even if the memory is NOT full based on the memory usage. The default is 60. We do not like this. You do not want the RAM memory (innodb_buffer_pool_size, caches and other) to be moved on swap when the memory is not full because of memory competition. If you do not change swappiness default value you will end up having worse performance. We need to set it to 0, so the linux will move the memory from RAM to swap only if the RAM is full. To change it please use:
<br /> echo 0 > /proc/sys/vm/swappiness<br />
Make notice that the code above will only make it temporarily. For permanent change edit /etc/sysctl.conf and add vm.swappiness = 0
Tune MySQl Parameters
After installing MySQL we need to tune a few parameters. In the future if you need more tuning, after making some analysis tune other parameters. But it is very important to understand the parameters. Here are a few very important parameters.
Note that the parameters and the suggested values below are for educational purposes. Exact values depends on your scenario.
This is one of the most important parameters to be set. From MySql website: “The size in bytes of the memory buffer InnoDB uses to cache data and indexes of its tables. The larger you set this value, the less disk I/O is needed to access data in tables. On a dedicated database server, you may set this to up to 80% of the machine physical memory size.”
But how much size to give to this parameter depends on your database, and the usage of the data. Some recommendations say the same amount as your database size including indexes, but less than 80% of the system memory. Here are some guidelines how to tune it.
First set it to the size of the database including indexes plus 40% growth (depends on your scenario).
<br /> SELECT CEILING(Total_InnoDB_Bytes*1.4/POWER(1024,3)) RIBPS FROM (SELECT SUM(data_length+index_length) Total_InnoDB_Bytes FROM information_schema.tables WHERE engine='InnoDB') A;<br />
RIBPS is “Recommended InnoDb Buffer Pool Size” in GB. Set it in my.cnf and restart the database. Wait for few days. Then check what is the actual usage:
<br /> SELECT (PagesData*PageSize)/POWER(1024,3) DataGB FROM (SELECT variable_value PagesData FROM information_schema.global_status WHERE variable_name='Innodb_buffer_pool_pages_data') A, (SELECT variable_value PageSize FROM information_schema.global_status WHERE variable_name='Innodb_page_size') B;<br />
With the last query you will see how much data is exactly cached, so you can correct the value of innodb_buffer_pool_size. You must have at least 10% more than this value.
Very important if you use MyISAM tables. Set up to 30-40% of available memory (also take in consideration the size of the database) if you use MyISAM tables exclusively. Right size depends on amount of indexes, data size and workload – remember MyISAM uses OS cache to cache the data so you need to leave memory for it as well, and data can be much larger than indexes in many cases. The parameter key_buffer_size is deprecated.
If your application is read-intensive and you do not have application level caches this can be of great help. But using together innodb_buffer_pool_size sometimes can slow down the system. Use it with caution. Use application level cache instead. If you need to use it use 32M up to 100M.
The size of each query result that can be cached. The queries that exceed this value will not be cached. The default is 1MB.
Innodb_log_file_sizewill be a good balance between reasonable recovery time and good performance. Innodb log file makes crash recovery possible. Generally the size does not need to exceed around 100M even for systems with high number of inserts. For most cases the default value is good enough. However, some blogs that recommend this approach:
<br /> mysql> pager grep sequence<br /> mysql> show engine innodb status\G select sleep(60); show engine innodb status\G<br />
From the second result subtract the first and divide it by 1024 * 1024 to find MB in minute. Multiply by hour and that is the value for this parameter. For my scenario, the default value is good enough. But be careful. Changing this parameter can cause problems. So, when applying you need to follow the instructions.
The default one is good enough.
Table_cache is number of opened tables for all threads. From MySql documentation:
“For example, for 200 concurrent running connections, you should have a table cache size of at least 200 * N, where N is the maximum number of tables per join in any of the queries which you execute. You must also reserve some extra file descriptors for temporary tables and files.”
If you have tables with several millions rows and more, then you probably need to partition the table in several, thus splitting index tree and improve performance. Partitioning is a complex task, please find more information in reference sources listed bellow.
There are softwares for tune recommendations. One of them is MySQLTuner. More comprehensive tool is is
Because from MySQl 5.6.8 they have removed sample my.cnf files, here is one that can set MySQL to take 4GB or RAM (or more). Here is a sample configuration so you can read more about available variables and their description instead of focusing on the proposed values, because as we said they depend on the specific scenario.
<br /> #BEGIN CONFIG INFO<br /> #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries<br /> #TYPE: SYSTEM<br /> #END CONFIG INFO</p> <p>#<br /> # This is a MySQL example config file for systems with 4GB of memory<br /> # running mostly MySQL using InnoDB only tables and performing complex<br /> # queries with few connections.<br /> #<br /> # MySQL programs look for option files in a set of<br /> # locations which depend on the deployment platform.<br /> # You can copy this option file to one of those<br /> # locations. For information about these locations, see:<br /> # http://dev.mysql.com/doc/mysql/en/option-files.html<br /> #<br /> # In this file, you can use all long options that a program supports.<br /> # If you want to know which options a program supports, run the program<br /> # with the "--help" option.<br /> #<br /> # More detailed information about the individual options can also be<br /> # found in the manual.<br /> #</p> <p>#<br /> # The following options will be read by MySQL client applications.<br /> # Note that only client applications shipped by MySQL are guaranteed<br /> # to read this section. If you want your own MySQL client program to<br /> # honor these values, you need to specify it as an option during the<br /> # MySQL client library initialization.<br /> #<br /> [client]<br /> #password = [your_password]<br /> port = 3306<br /> socket = /tmp/mysql.sock</p> <p># *** Application-specific options follow here ***</p> <p>#<br /> # The MySQL server<br /> #<br /> [mysqld]</p> <p># generic configuration options<br /> port = 3306<br /> socket = /tmp/mysql.sock</p> <p># back_log is the number of connections the operating system can keep in<br /> # the listen queue, before the MySQL connection manager thread has<br /> # processed them. If you have a very high connection rate and experience<br /> # "connection refused" errors, you might need to increase this value.<br /> # Check your OS documentation for the maximum value of this parameter.<br /> # Attempting to set back_log higher than your operating system limit<br /> # will have no effect.<br /> back_log = 50</p> <p># Don't listen on a TCP/IP port at all. This can be a security<br /> # enhancement, if all processes that need to connect to mysqld run<br /> # on the same host. All interaction with mysqld must be made via Unix<br /> # sockets or named pipes.<br /> # Note that using this option without enabling named pipes on Windows<br /> # (via the "enable-named-pipe" option) will render mysqld useless!<br /> #skip-networking</p> <p># The maximum amount of concurrent sessions the MySQL server will<br /> # allow. One of these connections will be reserved for a user with<br /> # SUPER privileges to allow the administrator to login even if the<br /> # connection limit has been reached.<br /> max_connections = 100</p> <p># Maximum amount of errors allowed per host. If this limit is reached,<br /> # the host will be blocked from connecting to the MySQL server until<br /> # "FLUSH HOSTS" has been run or the server was restarted. Invalid<br /> # passwords and other errors during the connect phase result in<br /> # increasing this value. See the "Aborted_connects" status variable for<br /> # global counter.<br /> max_connect_errors = 10</p> <p># The number of open tables for all threads. Increasing this value<br /> # increases the number of file descriptors that mysqld requires.<br /> # Therefore you have to make sure to set the amount of open files<br /> # allowed to at least 4096 in the variable "open-files-limit" in<br /> # section [mysqld_safe]<br /> table_open_cache = 2048</p> <p># Enable external file level locking. Enabled file locking will have a<br /> # negative impact on performance, so only use it in case you have<br /> # multiple database instances running on the same files (note some<br /> # restrictions still apply!) or if you use other software relying on<br /> # locking MyISAM tables on file level.<br /> #external-locking</p> <p># The maximum size of a query packet the server can handle as well as<br /> # maximum query size server can process (Important when working with<br /> # large BLOBs). enlarged dynamically, for each connection.<br /> max_allowed_packet = 16M</p> <p># The size of the cache to hold the SQL statements for the binary log<br /> # during a transaction. If you often use big, multi-statement<br /> # transactions you can increase this value to get more performance. All<br /> # statements from transactions are buffered in the binary log cache and<br /> # are being written to the binary log at once after the COMMIT. If the<br /> # transaction is larger than this value, temporary file on disk is used<br /> # instead. This buffer is allocated per connection on first update<br /> # statement in transaction<br /> binlog_cache_size = 1M</p> <p># Maximum allowed size for a single HEAP (in memory) table. This option<br /> # is a protection against the accidential creation of a very large HEAP<br /> # table which could otherwise use up all memory resources.<br /> max_heap_table_size = 64M</p> <p># Size of the buffer used for doing full table scans.<br /> # Allocated per thread, if a full scan is needed.<br /> read_buffer_size = 2M</p> <p># When reading rows in sorted order after a sort, the rows are read<br /> # through this buffer to avoid disk seeks. You can improve ORDER BY<br /> # performance a lot, if set this to a high value.<br /> # Allocated per thread, when needed.<br /> read_rnd_buffer_size = 16M</p> <p># Sort buffer is used to perform sorts for some ORDER BY and GROUP BY<br /> # queries. If sorted data does not fit into the sort buffer, a disk<br /> # based merge sort is used instead - See the "Sort_merge_passes"<br /> # status variable. Allocated per thread if sort is needed.<br /> sort_buffer_size = 8M</p> <p># This buffer is used for the optimization of full JOINs (JOINs without<br /> # indexes). Such JOINs are very bad for performance in most cases<br /> # anyway, but setting this variable to a large value reduces the<br /> # performance impact. See the "Select_full_join" status variable for a<br /> # count of full JOINs. Allocated per thread if full join is found<br /> join_buffer_size = 8M</p> <p># How many threads we should keep in a cache for reuse. When a client<br /> # disconnects, the client's threads are put in the cache if there aren't<br /> # more than thread_cache_size threads from before. This greatly reduces<br /> # the amount of thread creations needed if you have a lot of new<br /> # connections. (Normally this doesn't give a notable performance<br /> # improvement if you have a good thread implementation.)<br /> thread_cache_size = 8</p> <p># This permits the application to give the threads system a hint for the<br /> # desired number of threads that should be run at the same time. This<br /> # value only makes sense on systems that support the thread_concurrency()<br /> # function call (Sun Solaris, for example).<br /> # You should try [number of CPUs]*(2..4) for thread_concurrency<br /> thread_concurrency = 8</p> <p># Query cache is used to cache SELECT results and later return them<br /> # without actual executing the same query once again. Having the query<br /> # cache enabled may result in significant speed improvements, if your<br /> # have a lot of identical queries and rarely changing tables. See the<br /> # "Qcache_lowmem_prunes" status variable to check if the current value<br /> # is high enough for your load.<br /> # Note: In case your tables change very often or if your queries are<br /> # textually different every time, the query cache may result in a<br /> # slowdown instead of a performance improvement.<br /> query_cache_size = 64M</p> <p># Only cache result sets that are smaller than this limit. This is to<br /> # protect the query cache of a very large result set overwriting all<br /> # other query results.<br /> query_cache_limit = 2M</p> <p># Minimum word length to be indexed by the full text search index.<br /> # You might wish to decrease it if you need to search for shorter words.<br /> # Note that you need to rebuild your FULLTEXT index, after you have<br /> # modified this value.<br /> ft_min_word_len = 4</p> <p># If your system supports the memlock() function call, you might want to<br /> # enable this option while running MySQL to keep it locked in memory and<br /> # to avoid potential swapping out in case of high memory pressure. Good<br /> # for performance.<br /> #memlock</p> <p># Table type which is used by default when creating new tables, if not<br /> # specified differently during the CREATE TABLE statement.<br /> default-storage-engine = MYISAM</p> <p># Thread stack size to use. This amount of memory is always reserved at<br /> # connection time. MySQL itself usually needs no more than 64K of<br /> # memory, while if you use your own stack hungry UDF functions or your<br /> # OS requires more stack for some operations, you might need to set this<br /> # to a higher value.<br /> thread_stack = 192K</p> <p># Set the default transaction isolation level. Levels available are:<br /> # READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE<br /> transaction_isolation = REPEATABLE-READ</p> <p># Maximum size for internal (in-memory) temporary tables. If a table<br /> # grows larger than this value, it is automatically converted to disk<br /> # based table This limitation is for a single table. There can be many<br /> # of them.<br /> tmp_table_size = 64M</p> <p># Enable binary logging. This is required for acting as a MASTER in a<br /> # replication configuration. You also need the binary log if you need<br /> # the ability to do point in time recovery from your latest backup.<br /> log-bin=mysql-bin</p> <p># binary logging format - mixed recommended<br /> binlog_format=mixed</p> <p># If you're using replication with chained slaves (A->B->C), you need to<br /> # enable this option on server B. It enables logging of updates done by<br /> # the slave thread into the slave's binary log.<br /> #log_slave_updates</p> <p># Enable the full query log. Every query (even ones with incorrect<br /> # syntax) that the server receives will be logged. This is useful for<br /> # debugging, it is usually disabled in production use.<br /> #log</p> <p># Print warnings to the error log file. If you have any problem with<br /> # MySQL you should enable logging of warnings and examine the error log<br /> # for possible explanations.<br /> #log_warnings</p> <p># Log slow queries. Slow queries are queries which take more than the<br /> # amount of time defined in "long_query_time" or which do not use<br /> # indexes well, if log_short_format is not enabled. It is normally good idea<br /> # to have this turned on if you frequently add new queries to the<br /> # system.<br /> slow_query_log</p> <p># All queries taking more than this amount of time (in seconds) will be<br /> # trated as slow. Do not use "1" as a value here, as this will result in<br /> # even very fast queries being logged from time to time (as MySQL<br /> # currently measures time with second accuracy only).<br /> long_query_time = 2</p> <p># *** Replication related settings</p> <p># Unique server identification number between 1 and 2^32-1. This value<br /> # is required for both master and slave hosts. It defaults to 1 if<br /> # "master-host" is not set, but will MySQL will not function as a master<br /> # if it is omitted.<br /> server-id = 1</p> <p># Replication Slave (comment out master section to use this)<br /> #<br /> # To configure this host as a replication slave, you can choose between<br /> # two methods :<br /> #<br /> # 1) Use the CHANGE MASTER TO command (fully described in our manual) -<br /> # the syntax is:<br /> #<br /> # CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,<br /> # MASTER_USER=, MASTER_PASSWORD= ;<br /> #<br /> # where you replace , , by quoted strings and<br /> # by the master's port number (3306 by default).<br /> #<br /> # Example:<br /> #<br /> # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,<br /> # MASTER_USER='joe', MASTER_PASSWORD='secret';<br /> #<br /> # OR<br /> #<br /> # 2) Set the variables below. However, in case you choose this method, then<br /> # start replication for the first time (even unsuccessfully, for example<br /> # if you mistyped the password in master-password and the slave fails to<br /> # connect), the slave will create a master.info file, and any later<br /> # changes in this file to the variable values below will be ignored and<br /> # overridden by the content of the master.info file, unless you shutdown<br /> # the slave server, delete master.info and restart the slaver server.<br /> # For that reason, you may want to leave the lines below untouched<br /> # (commented) and instead use CHANGE MASTER TO (see above)<br /> #<br /> # required unique id between 2 and 2^32 - 1<br /> # (and different from the master)<br /> # defaults to 2 if master-host is set<br /> # but will not function as a slave if omitted<br /> #server-id = 2<br /> #<br /> # The replication master for this slave - required<br /> #master-host =<br /> #<br /> # The username the slave will use for authentication when connecting<br /> # to the master - required<br /> #master-user =<br /> #<br /> # The password the slave will authenticate with when connecting to<br /> # the master - required<br /> #master-password =<br /> #<br /> # The port the master is listening on.<br /> # optional - defaults to 3306<br /> #master-port =</p> <p># Make the slave read-only. Only users with the SUPER privilege and the<br /> # replication slave thread will be able to modify data on it. You can<br /> # use this to ensure that no applications will accidently modify data on<br /> # the slave instead of the master<br /> #read_only</p> <p>#*** MyISAM Specific options</p> <p># Size of the Key Buffer, used to cache index blocks for MyISAM tables.<br /> # Do not set it larger than 30% of your available memory, as some memory<br /> # is also required by the OS to cache rows. Even if you're not using<br /> # MyISAM tables, you should still set it to 8-64M as it will also be<br /> # used for internal temporary disk tables.<br /> key_buffer_size = 32M</p> <p># MyISAM uses special tree-like cache to make bulk inserts (that is,<br /> # INSERT ... SELECT, INSERT ... VALUES (...), (...), ..., and LOAD DATA<br /> # INFILE) faster. This variable limits the size of the cache tree in<br /> # bytes per thread. Setting it to 0 will disable this optimisation. Do<br /> # not set it larger than "key_buffer_size" for optimal performance.<br /> # This buffer is allocated when a bulk insert is detected.<br /> bulk_insert_buffer_size = 64M</p> <p># This buffer is allocated when MySQL needs to rebuild the index in<br /> # REPAIR, OPTIMIZE, ALTER table statements as well as in LOAD DATA INFILE<br /> # into an empty table. It is allocated per thread so be careful with<br /> # large settings.<br /> myisam_sort_buffer_size = 128M</p> <p># The maximum size of the temporary file MySQL is allowed to use while<br /> # recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.<br /> # If the file-size would be bigger than this, the index will be created<br /> # through the key cache (which is slower).<br /> myisam_max_sort_file_size = 10G</p> <p># If a table has more than one index, MyISAM can use more than one<br /> # thread to repair them by sorting in parallel. This makes sense if you<br /> # have multiple CPUs and plenty of memory.<br /> myisam_repair_threads = 1</p> <p># Automatically check and repair not properly closed MyISAM tables.<br /> myisam_recover</p> <p># *** INNODB Specific options ***</p> <p># Use this option if you have a MySQL server with InnoDB support enabled<br /> # but you do not plan to use it. This will save memory and disk space<br /> # and speed up some things.<br /> #skip-innodb</p> <p># Additional memory pool that is used by InnoDB to store metadata<br /> # information. If InnoDB requires more memory for this purpose it will<br /> # start to allocate it from the OS. As this is fast enough on most<br /> # recent operating systems, you normally do not need to change this<br /> # value. SHOW INNODB STATUS will display the current amount used.<br /> innodb_additional_mem_pool_size = 16M</p> <p># InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and<br /> # row data. The bigger you set this the less disk I/O is needed to<br /> # access data in tables. On a dedicated database server you may set this<br /> # parameter up to 80% of the machine physical memory size. Do not set it<br /> # too large, though, because competition of the physical memory may<br /> # cause paging in the operating system. Note that on 32bit systems you<br /> # might be limited to 2-3.5G of user level memory per process, so do not<br /> # set it too high.<br /> innodb_buffer_pool_size = 2G</p> <p># InnoDB stores data in one or more data files forming the tablespace.<br /> # If you have a single logical drive for your data, a single<br /> # autoextending file would be good enough. In other cases, a single file<br /> # per device is often a good choice. You can configure InnoDB to use raw<br /> # disk partitions as well - please refer to the manual for more info<br /> # about this.<br /> innodb_data_file_path = ibdata1:10M:autoextend</p> <p># Set this option if you would like the InnoDB tablespace files to be<br /> # stored in another location. By default this is the MySQL datadir.<br /> #innodb_data_home_dir =</p> <p># Number of IO threads to use for async IO operations. This value is<br /> # hardcoded to 8 on Unix, but on Windows disk I/O may benefit from a<br /> # larger number.<br /> innodb_write_io_threads = 8<br /> innodb_read_io_threads = 8</p> <p># If you run into InnoDB tablespace corruption, setting this to a nonzero<br /> # value will likely help you to dump your tables. Start from value 1 and<br /> # increase it until you're able to dump the table successfully.<br /> #innodb_force_recovery=1</p> <p># Number of threads allowed inside the InnoDB kernel. The optimal value<br /> # depends highly on the application, hardware as well as the OS<br /> # scheduler properties. A too high value may lead to thread thrashing.<br /> innodb_thread_concurrency = 16</p> <p># If set to 1, InnoDB will flush (fsync) the transaction logs to the<br /> # disk at each commit, which offers full ACID behavior. If you are<br /> # willing to compromise this safety, and you are running small<br /> # transactions, you may set this to 0 or 2 to reduce disk I/O to the<br /> # logs. Value 0 means that the log is only written to the log file and<br /> # the log file flushed to disk approximately once per second. Value 2<br /> # means the log is written to the log file at each commit, but the log<br /> # file is only flushed to disk approximately once per second.<br /> innodb_flush_log_at_trx_commit = 1</p> <p># Speed up InnoDB shutdown. This will disable InnoDB to do a full purge<br /> # and insert buffer merge on shutdown. It may increase shutdown time a<br /> # lot, but InnoDB will have to do it on the next startup instead.<br /> #innodb_fast_shutdown</p> <p># The size of the buffer InnoDB uses for buffering log data. As soon as<br /> # it is full, InnoDB will have to flush it to disk. As it is flushed<br /> # once per second anyway, it does not make sense to have it very large<br /> # (even with long transactions).<br /> innodb_log_buffer_size = 8M</p> <p># Size of each log file in a log group. You should set the combined size<br /> # of log files to about 25%-100% of your buffer pool size to avoid<br /> # unneeded buffer pool flush activity on log file overwrite. However,<br /> # note that a larger logfile size will increase the time needed for the<br /> # recovery process.<br /> innodb_log_file_size = 256M</p> <p># Total number of files in the log group. A value of 2-3 is usually good<br /> # enough.<br /> innodb_log_files_in_group = 3</p> <p># Location of the InnoDB log files. Default is the MySQL datadir. You<br /> # may wish to point it to a dedicated hard drive or a RAID1 volume for<br /> # improved performance<br /> #innodb_log_group_home_dir</p> <p># Maximum allowed percentage of dirty pages in the InnoDB buffer pool.<br /> # If it is reached, InnoDB will start flushing them out agressively to<br /> # not run out of clean pages at all. This is a soft limit, not<br /> # guaranteed to be held.<br /> innodb_max_dirty_pages_pct = 90</p> <p># The flush method InnoDB will use for Log. The tablespace always uses<br /> # doublewrite flush logic. The default value is "fdatasync", another<br /> # option is "O_DSYNC".<br /> #innodb_flush_method=O_DSYNC</p> <p># How long an InnoDB transaction should wait for a lock to be granted<br /> # before being rolled back. InnoDB automatically detects transaction<br /> # deadlocks in its own lock table and rolls back the transaction. If you<br /> # use the LOCK TABLES command, or other transaction-safe storage engines<br /> # than InnoDB in the same transaction, then a deadlock may arise which<br /> # InnoDB cannot notice. In cases like this the timeout is useful to<br /> # resolve the situation.<br /> innodb_lock_wait_timeout = 120</p> <p>[mysqldump]<br /> # Do not buffer the whole result set in memory before writing it to<br /> # file. Required for dumping very large tables<br /> quick</p> <p>max_allowed_packet = 16M</p> <p>[mysql]<br /> no-auto-rehash</p> <p># Only allow UPDATEs and DELETEs that use keys.<br /> #safe-updates</p> <p>[myisamchk]<br /> key_buffer_size = 512M<br /> sort_buffer_size = 512M<br /> read_buffer = 8M<br /> write_buffer = 8M</p> <p>[mysqlhotcopy]<br /> interactive-timeout</p> <p>[mysqld_safe]<br /> # Increase the amount of open files allowed per process. Warning: Make<br /> # sure you have set the global system limit high enough! The high value<br /> # is required for a large number of opened tables<br /> open-files-limit = 8192<br />