Storage Layout Tips
- In
InnoDB, having a longPRIMARY KEYwastes a lot of disk space because its value must be stored with every secondary index record. (SeeSection 14.2.10, “InnoDBTable and Index Structures”.) Create anAUTO_INCREMENTcolumn as the primary key if your primary key is long. - Use the
VARCHARdata type instead ofCHARif you are storing variable-length strings or if the column may contain manyNULLvalues. ACHAR(column always takesN)Ncharacters to store data, even if the string is shorter or its value isNULL. Smaller tables fit better in the buffer pool and reduce disk I/O.When usingCOMPACTrow format (the defaultInnoDBformat in MySQL 5.0) and variable-length character sets, such asutf8orsjis,CHAR(will occupy a variable amount of space, at leastN)Nbytes.Transaction Management Tips- Wrap several modifications into a single transaction to reduce the number of flush operations.
InnoDBmust 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 the
innodb_flush_log_at_trx_commitparameter to 0.InnoDBtries to flush the log once per second anyway, although the flush is not guaranteed.Disk I/O Tipsinnodb_buffer_pool_sizespecifies 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 accessInnoDBtables. For more information about the pool, see Section 8.6.2, “TheInnoDBBuffer Pool”.- Beware of big rollbacks of mass inserts:
InnoDBuses 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, “ForcingInnoDBRecovery”. - Beware also of other big disk-bound operations. Use
DROP TABLEandCREATE TABLEto empty a table, notDELETE FROM.tbl_name - In some versions of GNU/Linux and Unix, flushing files to disk with the Unix
fsync()call (whichInnoDBuses by default) and other similar methods is surprisingly slow. If you are dissatisfied with database write performance, you might try setting theinnodb_flush_methodparameter toO_DSYNC. TheO_DSYNCflush method seems to perform slower on most systems, but yours might not be one of them. - When using the
InnoDBstorage engine on Solaris 10 for x86_64 architecture (AMD Opteron), it is important to use direct I/O forInnoDB-related files. Failure to do so may cause degradation ofInnoDB's speed and performance on this platform. To use direct I/O for an entire UFS file system used for storingInnoDB-related files, mount it with theforcedirectiooption; seemount_ufs(1M). (The default on Solaris 10/x86_64 is not to use this option.) Alternatively, as of MySQL 5.0.42 you can setinnodb_flush_method = O_DIRECTif you do not want to affect the entire file system. This causesInnoDBto calldirectio()instead offcntl(). However, settinginnodb_flush_methodtoO_DIRECTcausesInnoDBto use direct I/O only for data files, not the log files.When using theInnoDBstorage engine with a largeinnodb_buffer_pool_sizevalue on any release of Solaris 2.6 and up and any platform (sparc/x86/x64/amd64), a significant performance gain might be achieved by placingInnoDBdata files and log files on raw devices or on a separate direct I/O UFS file system using theforcedirectiomount option as described earlier (it is necessary to use the mount option rather than settinginnodb_flush_methodif you want direct I/O for the log files). Users of the Veritas file system VxFS should use theconvosync=directmount 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 forMyISAMtables, 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
toptool 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
InnoDBhas 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 autocommitandCOMMITstatements:SET autocommit=0;
... SQL import statements ...COMMIT;If you use the mysqldump option--opt, you get dump files that are fast to import into anInnoDBtable, even without wrapping them with theSET autocommitandCOMMITstatements. - If you have
UNIQUEconstraints 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 becauseInnoDBcan 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 KEYconstraints 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
MyISAM,InnoDBdoes not store an index cardinality value in its tables. Instead,InnoDBcomputes 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 asSELECT 1 FROM.tbl_nameLIMIT 1 - Use the multiple-row
INSERTsyntax 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 justInnoDBtables. - 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
Post a Comment