8.6 Optimizing for MyISAM Tables

The note 'MyISAM': myisam-storage-engine. storage engine performs best with read-mostly data or with low-concurrency operations, because table locks limit the ability to perform simultaneous updates. In MySQL, note 'InnoDB': innodb-storage-engine. is the default storage engine rather than 'MyISAM'.

 File: manual.info.tmp, Node: optimizing-queries-myisam, Next: optimizing-myisam-bulk-data-loading, Prev: optimizing-myisam, Up: optimizing-myisam

8.6.1 Optimizing MyISAM Queries

Some general tips for speeding up queries on 'MyISAM' tables:

 File: manual.info.tmp, Node: optimizing-myisam-bulk-data-loading, Next: repair-table-optimization, Prev: optimizing-queries-myisam, Up: optimizing-myisam

8.6.2 Bulk Data Loading for MyISAM Tables

These performance tips supplement the general guidelines for fast inserts in *note insert-optimization::.

 File: manual.info.tmp, Node: repair-table-optimization, Prev: optimizing-myisam-bulk-data-loading, Up: optimizing-myisam

8.6.3 Optimizing REPAIR TABLE Statements

note 'REPAIR TABLE': repair-table. for 'MyISAM' tables is similar to using note 'myisamchk': myisamchk. for repair operations, and some of the same performance optimizations apply:

Suppose that a *note 'myisamchk': myisamchk. table-repair operation is done using the following options to set its memory-allocation variables:

 --key_buffer_size=128M --myisam_sort_buffer_size=256M
 --read_buffer_size=64M --write_buffer_size=64M

Some of those *note 'myisamchk': myisamchk. variables correspond to server system variables:

*note 'myisamchk': myisamchk. System Variable Variable

'key_buffer_size' 'key_buffer_size'

'myisam_sort_buffer_size' 'myisam_sort_buffer_size'

'read_buffer_size' 'read_buffer_size'

'write_buffer_size' none

Each of the server system variables can be set at runtime, and some of them ('myisam_sort_buffer_size', 'read_buffer_size') have a session value in addition to a global value. Setting a session value limits the effect of the change to your current session and does not affect other users. Changing a global-only variable ('key_buffer_size', 'myisam_max_sort_file_size') affects other users as well. For 'key_buffer_size', you must take into account that the buffer is shared with those users. For example, if you set the note 'myisamchk': myisamchk. 'key_buffer_size' variable to 128MB, you could set the corresponding 'key_buffer_size' system variable larger than that (if it is not already set larger), to permit key buffer use by activity in other sessions. However, changing the global key buffer size invalidates the buffer, causing increased disk I/O and slowdown for other sessions. An alternative that avoids this problem is to use a separate key cache, assign to it the indexes from the table to be repaired, and deallocate it when the repair is complete. See note multiple-key-caches::.

Based on the preceding remarks, a note 'REPAIR TABLE': repair-table. operation can be done as follows to use settings similar to the note 'myisamchk': myisamchk. command. Here a separate 128MB key buffer is allocated and the file system is assumed to permit a file size of at least 100GB.

 SET SESSION myisam_sort_buffer_size = 256*1024*1024;
 SET SESSION read_buffer_size = 64*1024*1024;
 SET GLOBAL myisam_max_sort_file_size = 100*1024*1024*1024;
 SET GLOBAL repair_cache.key_buffer_size = 128*1024*1024;
 CACHE INDEX TBL_NAME IN repair_cache;
 LOAD INDEX INTO CACHE TBL_NAME;
 REPAIR TABLE TBL_NAME ;
 SET GLOBAL repair_cache.key_buffer_size = 0;

If you intend to change a global variable but want to do so only for the duration of a *note 'REPAIR TABLE': repair-table. operation to minimally affect other users, save its value in a user variable and restore it afterward. For example:

 SET @old_myisam_sort_buffer_size = @@GLOBAL.myisam_max_sort_file_size;
 SET GLOBAL myisam_max_sort_file_size = 100*1024*1024*1024;
 REPAIR TABLE tbl_name ;
 SET GLOBAL myisam_max_sort_file_size = @old_myisam_max_sort_file_size;

The system variables that affect *note 'REPAIR TABLE': repair-table. can be set globally at server startup if you want the values to be in effect by default. For example, add these lines to the server 'my.cnf' file:

 [mysqld]
 myisam_sort_buffer_size=256M
 key_buffer_size=1G
 myisam_max_sort_file_size=100G

These settings do not include 'read_buffer_size'. Setting 'read_buffer_size' globally to a large value does so for all sessions and can cause performance to suffer due to excessive memory allocation for a server with many simultaneous sessions.

 File: manual.info.tmp, Node: optimizing-memory-tables, Next: execution-plan-information, Prev: optimizing-myisam, Up: optimization