Skip to main content

InnoDB Performance Tuning Tips


Storage Layout Tips
  • In InnoDB, having a long PRIMARY KEY wastes a lot of disk space because its value must be stored with every secondary index record. (SeeSection 14.2.10, “InnoDB Table and Index Structures”.) Create anAUTO_INCREMENT column as the primary key if your primary key is long.
  • Use the VARCHAR data type instead of CHAR if you are storing variable-length strings or if the column may contain many NULL values. A CHAR(N)column always takes N characters to store data, even if the string is shorter or its value is NULL. Smaller tables fit better in the buffer pool and reduce disk I/O.
    When using COMPACT row format (the default InnoDB format in MySQL 5.0) and variable-length character sets, such as utf8 or sjisCHAR(N) will occupy a variable amount of space, at least N bytes.
    Transaction Management Tips
    • Wrap several modifications into a single transaction to reduce the number of flush operations. InnoDB must flush the log to disk at each transaction commit if that transaction made modifications to the database. The rotation speed of a disk is typically at most 167 revolutions/second (for a 10,000RPM disk), which constrains the number of commits to the same 167th of a second if the disk does not “fool” the operating system.
    • If you can afford the loss of some of the latest committed transactions if a crash occurs, you can set theinnodb_flush_log_at_trx_commit parameter to 0. InnoDB tries to flush the log once per second anyway, although the flush is not guaranteed.
      Disk I/O Tips
      • innodb_buffer_pool_size specifies the size of the buffer pool. If your buffer pool is small and you have sufficient memory, making the pool larger can improve performance by reducing the amount of disk I/O needed as queries access InnoDB tables. For more information about the pool, see Section 8.6.2, “The InnoDB Buffer Pool”.
      • Beware of big rollbacks of mass inserts: InnoDB uses the insert buffer to save disk I/O in inserts, but no such mechanism is used in a corresponding rollback. A disk-bound rollback can take 30 times as long to perform as the corresponding insert. Killing the database process does not help because the rollback starts again on server startup. The only way to get rid of a runaway rollback is to increase the buffer pool so that the rollback becomes CPU-bound and runs fast, or to use a special procedure. See Section 14.2.6.2, “Forcing InnoDB Recovery”.
      • Beware also of other big disk-bound operations. Use DROP TABLE and CREATE TABLE to empty a table, notDELETE FROM tbl_name.
      • In some versions of GNU/Linux and Unix, flushing files to disk with the Unix fsync() call (which InnoDB uses by default) and other similar methods is surprisingly slow. If you are dissatisfied with database write performance, you might try setting the innodb_flush_method parameter to O_DSYNC. The O_DSYNC flush method seems to perform slower on most systems, but yours might not be one of them.
      • When using the InnoDB storage engine on Solaris 10 for x86_64 architecture (AMD Opteron), it is important to use direct I/O for InnoDB-related files. Failure to do so may cause degradation of InnoDB's speed and performance on this platform. To use direct I/O for an entire UFS file system used for storing InnoDB-related files, mount it with the forcedirectio option; see mount_ufs(1M). (The default on Solaris 10/x86_64 is not to use this option.) Alternatively, as of MySQL 5.0.42 you can set innodb_flush_method = O_DIRECT if you do not want to affect the entire file system. This causes InnoDB to call directio() instead of fcntl(). However, settinginnodb_flush_method to O_DIRECT causes InnoDB to use direct I/O only for data files, not the log files.
        When using the InnoDB storage engine with a large innodb_buffer_pool_size value on any release of Solaris 2.6 and up and any platform (sparc/x86/x64/amd64), a significant performance gain might be achieved by placingInnoDB data files and log files on raw devices or on a separate direct I/O UFS file system using theforcedirectio mount option as described earlier (it is necessary to use the mount option rather than settinginnodb_flush_method if you want direct I/O for the log files). Users of the Veritas file system VxFS should use the convosync=direct mount option. You are advised to perform tests with and without raw partitions or direct I/O file systems to verify whether performance is improved on your system.
        Other MySQL data files, such as those for MyISAM tables, should not be placed on a direct I/O file system. Executables or libraries must not be placed on a direct I/O file system.
      • If the Unix top tool or the Windows Task Manager shows that the CPU usage percentage with your workload is less than 70%, your workload is probably disk-bound. Maybe you are making too many transaction commits, or the buffer pool is too small. Making the buffer pool bigger can help, but do not set it equal to more than 80% of physical memory.
      Logging Tips
      • Make your log files big, even as big as the buffer pool. When InnoDB has written the log files full, it must write the modified contents of the buffer pool to disk in a checkpoint. Small log files cause many unnecessary disk writes. The disadvantage of big log files is that the recovery time is longer.
      • Make the log buffer quite large as well (on the order of 8MB).
      Bulk Data Loading Tips
      • When importing data into InnoDB, make sure that MySQL does not have autocommit mode enabled because that requires a log flush to disk for every insert. To disable autocommit during your import operation, surround it withSET autocommit and COMMIT statements:
        SET autocommit=0;... SQL import statements ...
        COMMIT;
        
        If you use the mysqldump option --opt, you get dump files that are fast to import into an InnoDB table, even without wrapping them with the SET autocommit and COMMIT statements.
      • If you have UNIQUE constraints on secondary keys, you can speed up table imports by temporarily turning off the uniqueness checks during the import session:
        SET unique_checks=0;... SQL import statements ...
        SET unique_checks=1;
        
        For big tables, this saves a lot of disk I/O because InnoDB can use its insert buffer to write secondary index records in a batch. Be certain that the data contains no duplicate keys.
        • If you have FOREIGN KEY constraints in your tables, you can speed up table imports by turning the foreign key checks off for the duration of the import session:
          SET foreign_key_checks=0;... SQL import statements ...
          SET foreign_key_checks=1;
          
          For big tables, this can save a lot of disk I/O.
        Other Tips
        • Unlike MyISAMInnoDB does not store an index cardinality value in its tables. Instead, InnoDB computes a cardinality for a table the first time it accesses it after startup. With a large number of tables, this might take significant time. It is the initial table open operation that is important, so to “warm up” a table for later use, access it immediately after startup by issuing a statement such as SELECT 1 FROM tbl_name LIMIT 1.
        • Use the multiple-row INSERT syntax to reduce communication overhead between the client and the server if you need to insert many rows:
          INSERT INTO yourtable VALUES (1,2), (5,5), ...;
          This tip is valid for inserts into any table, not just InnoDB tables.
        • If you often have recurring queries for tables that are not updated frequently, enable the query cache:
          [mysqld]
          query_cache_type = 1
          query_cache_size = 10M

Comments

Popular posts from this blog

About MySQL - Database history Support and Versioning

About MySQL: MySQL was created by a Swedish company. David Axmark (left) and Michael "Monty" Widenius,  MySQL AB, founded by David Axmark, Allan Larsson and Michael "Monty" Widenius. Original development of MySQL by Widenius and Axmark began in 1994. The first version of MySQL appeared on 23 May 1995. It was initially created for personal usage from mSQL based on the low-level language ISAM, which the creators considered too slow and inflexible. They created a new SQL interface, while keeping the same API as mSQL. By keeping the API consistent with the mSQL system, many developers were able to use MySQL instead of the (proprietarily licensed) mSQL antecedent. Support: The MySQL server software itself and the client libraries use dual-licensing distribution. They are offered under GPL version 2, or a proprietary license. MySQL AB owns the copyright to the MySQL source code. This means that MySQL AB can distribute MySQL under several different l...

MySQL thread error code of 1032- Translate binlog completely to avoid error

Could not execute Update_rows event on table db.table; Can't find record in 'table', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's   master log mysql-bin.262297, end_log_pos 1983208 As of my knowledge the causing stop of slave SQL thread error code of 1032, due to lack of sync between Master - Slave. In our case, slave got an Update event form master, but that particular record was not available in the slave. (Of course, that was deleted on Master n Slave separately) You would get this error only if the binlog_format is set to ROW_BASED or MIXED mode. So, now check that particular binlog at that position where replication stopped with this error. When you convert the binlog with mysqlbinlog command, you may see some junk characters   where you were expecting some DMLs which caused the error(In our case its an Update statement). So you can use below command to translate binlog completely: mysqlbinlog --base64-output=DECO...

MySQL Architecture -Client Server Architecture

Client/Server Overview The MySQL database system operates using a client/server architecture. The server is a central program that manages database contents, and client programs connect to the server to retrieve or modify the data. MySQL also includes non-client utility programs and scripts. MySQL Server:   This is the mysqld program that manages database and tables. Most users choose binary MySQL distribution that includes a server ready to run with the capabilities they need, but it's also possible to compile MySQL from source. Client Programs:  These are programs that communicate with the server by sending requests to it over a network connection. The server acts on each request and returns a response to the client. For example you can use the mysql client to send queries to the server, and the server returns the query results. A client program can connect locally to a server running on the same machine or remotely to a server running on a different mac...