15.2 The MyISAM Storage Engine

'MyISAM' is based on the older (and no longer available) 'ISAM' storage engine but has many useful extensions.

MyISAM Storage Engine Features

Feature Support

B-tree indexes Yes

Backup/point-in-time recovery Yes (Implemented in the server, rather than
in the storage engine.)

Cluster database support No

Clustered indexes No

Compressed data Yes (Compressed MyISAM tables are supported only when using the compressed row format. Tables using the compressed row format with MyISAM are read only.)

Data caches No

Encrypted data Yes (Implemented in the server via encryption functions.)

Foreign key support No

Full-text search indexes Yes

Geospatial data type support Yes

Geospatial indexing support Yes

Hash indexes No

Index caches Yes

Locking granularity Table

MVCC No

Replication support (Implemented in the Yes server, rather than in the storage
engine.)

Storage limits 256TB

T-tree indexes No

Transactions No

Update statistics for data dictionary Yes

Each 'MyISAM' table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type. An '.frm' file stores the table format. The data file has an '.MYD' ('MYData') extension. The index file has an '.MYI' ('MYIndex') extension.

To specify explicitly that you want a 'MyISAM' table, indicate that with an 'ENGINE' table option:

 CREATE TABLE t (i INT) ENGINE = MYISAM;

In MySQL 5.7, it is normally necessary to use 'ENGINE' to specify the 'MyISAM' storage engine because 'InnoDB' is the default engine.

You can check or repair 'MyISAM' tables with the note 'mysqlcheck': mysqlcheck. client or note 'myisamchk': myisamchk. utility. You can also compress 'MyISAM' tables with note 'myisampack': myisampack. to take up much less space. See note mysqlcheck::, note myisamchk::, and note myisampack::.

'MyISAM' tables have the following characteristics:

'MyISAM' also supports the following features:

Additional Resources

 File: manual.info.tmp, Node: myisam-start, Next: key-space, Prev: myisam-storage-engine, Up: myisam-storage-engine

15.2.1 MyISAM Startup Options

The following options to note 'mysqld': mysqld. can be used to change the behavior of 'MyISAM' tables. For additional information, see note server-options::.

MyISAM Option and Variable Reference

Name Cmd-Line Option System Status Var Scope Dynamic File Var Var

bulk_insert_buffer_size

Yes Yes Yes Both Yes

concurrent_insert

Yes Yes Yes Global Yes

delay_key_write

Yes Yes Yes Global Yes

have_rtree_keys

                       Yes                     Global      No
                                                           

key_buffer_size

Yes Yes Yes Global Yes

log-isam

Yes Yes

myisam-block-size

Yes Yes

myisam_data_pointer_size

Yes Yes Yes Global Yes

myisam_max_sort_file_size

Yes Yes Yes Global Yes

myisam_mmap_size

Yes Yes Yes Global No

myisam_recover_options

Yes Yes Yes Global No

myisam_repair_threads

Yes Yes Yes Both Yes

myisam_sort_buffer_size

Yes Yes Yes Both Yes

myisam_stats_method

Yes Yes Yes Both Yes

myisam_use_mmap

Yes Yes Yes Global Yes

tmp_table_size

Yes Yes Yes Both Yes

The following system variables affect the behavior of 'MyISAM' tables. For additional information, see *note server-system-variables::.

Automatic recovery is activated if you start *note 'mysqld': mysqld. with the 'myisam_recover_options' system variable set. In this case, when the server opens a 'MyISAM' table, it checks whether the table is marked as crashed or whether the open count variable for the table is not 0 and you are running the server with external locking disabled. If either of these conditions is true, the following happens:

If the recovery wouldn't be able to recover all rows from previously completed statements and you didn't specify 'FORCE' in the value of the 'myisam_recover_options' system variable, automatic repair aborts with an error message in the error log:

 Error: Couldn't repair table: test.g00pages

If you specify 'FORCE', a warning like this is written instead:

 Warning: Found 344 of 354 rows when repairing ./test/g00pages

If the automatic recovery value includes 'BACKUP', the recovery process creates files with names of the form 'TBL_NAME-DATETIME.BAK'. You should have a 'cron' script that automatically moves these files from the database directories to backup media.

 File: manual.info.tmp, Node: key-space, Next: myisam-table-formats, Prev: myisam-start, Up: myisam-storage-engine

15.2.2 Space Needed for Keys

'MyISAM' tables use B-tree indexes. You can roughly calculate the size for the index file as '(key_length+4)/0.67', summed over all keys. This is for the worst case when all keys are inserted in sorted order and the table does not have any compressed keys.

String indexes are space compressed. If the first index part is a string, it is also prefix compressed. Space compression makes the index file smaller than the worst-case figure if a string column has a lot of trailing space or is a *note 'VARCHAR': char. column that is not always used to the full length. Prefix compression is used on keys that start with a string. Prefix compression helps if there are many strings with an identical prefix.

In 'MyISAM' tables, you can also prefix compress numbers by specifying the 'PACK_KEYS=1' table option when you create the table. Numbers are stored with the high byte first, so this helps when you have many integer keys that have an identical prefix.

 File: manual.info.tmp, Node: myisam-table-formats, Next: myisam-table-problems, Prev: key-space, Up: myisam-storage-engine

15.2.3 MyISAM Table Storage Formats

'MyISAM' supports three different storage formats. Two of them, fixed and dynamic format, are chosen automatically depending on the type of columns you are using. The third, compressed format, can be created only with the note 'myisampack': myisampack. utility (see note myisampack::).

When you use note 'CREATE TABLE': create-table. or note 'ALTER TABLE': alter-table. for a table that has no note 'BLOB': blob. or note 'TEXT': blob. columns, you can force the table format to 'FIXED' or 'DYNAMIC' with the 'ROW_FORMAT' table option.

See *note create-table::, for information about 'ROW_FORMAT'.

You can decompress (unpack) compressed 'MyISAM' tables using note 'myisamchk': myisamchk. '--unpack'; see note myisamchk::, for more information.

 File: manual.info.tmp, Node: static-format, Next: dynamic-format, Prev: myisam-table-formats, Up: myisam-table-formats

15.2.3.1 Static (Fixed-Length) Table Characteristics ....................................................

Static format is the default for 'MyISAM' tables. It is used when the table contains no variable-length columns (note 'VARCHAR': char, note 'VARBINARY': binary-varbinary, note 'BLOB': blob, or note 'TEXT': blob.). Each row is stored using a fixed number of bytes.

Of the three 'MyISAM' storage formats, static format is the simplest and most secure (least subject to corruption). It is also the fastest of the on-disk formats due to the ease with which rows in the data file can be found on disk: To look up a row based on a row number in the index, multiply the row number by the row length to calculate the row position. Also, when scanning a table, it is very easy to read a constant number of rows with each disk read operation.

The security is evidenced if your computer crashes while the MySQL server is writing to a fixed-format 'MyISAM' file. In this case, *note 'myisamchk': myisamchk. can easily determine where each row starts and ends, so it can usually reclaim all rows except the partially written one. 'MyISAM' table indexes can always be reconstructed based on the data rows.

Note:

Fixed-length row format is only available for tables without note 'BLOB': blob. or note 'TEXT': blob. columns. Creating a table with these columns with an explicit 'ROW_FORMAT' clause does not raise an error or warning; the format specification is ignored.

Static-format tables have these characteristics:

 File: manual.info.tmp, Node: dynamic-format, Next: compressed-format, Prev: static-format, Up: myisam-table-formats

15.2.3.2 Dynamic Table Characteristics ......................................

Dynamic storage format is used if a 'MyISAM' table contains any variable-length columns (note 'VARCHAR': char, note 'VARBINARY': binary-varbinary, note 'BLOB': blob, or note 'TEXT': blob.), or if the table was created with the 'ROW_FORMAT=DYNAMIC' table option.

Dynamic format is a little more complex than static format because each row has a header that indicates how long it is. A row can become fragmented (stored in noncontiguous pieces) when it is made longer as a result of an update.

You can use note 'OPTIMIZE TABLE': optimize-table. or note 'myisamchk -r': myisamchk. to defragment a table. If you have fixed-length columns that you access or change frequently in a table that also contains some variable-length columns, it might be a good idea to move the variable-length columns to other tables just to avoid fragmentation.

Dynamic-format tables have these characteristics:

 File: manual.info.tmp, Node: compressed-format, Prev: dynamic-format, Up: myisam-table-formats

15.2.3.3 Compressed Table Characteristics .........................................

Compressed storage format is a read-only format that is generated with the note 'myisampack': myisampack. tool. Compressed tables can be uncompressed with note 'myisamchk': myisamchk.

Compressed tables have the following characteristics:

Note:

While a compressed table is read only, and you cannot therefore update or add rows in the table, DDL (Data Definition Language) operations are still valid. For example, you may still use 'DROP' to drop the table, and *note 'TRUNCATE TABLE': truncate-table. to empty the table.

 File: manual.info.tmp, Node: myisam-table-problems, Prev: myisam-table-formats, Up: myisam-storage-engine

15.2.4 MyISAM Table Problems

The file format that MySQL uses to store data has been extensively tested, but there are always circumstances that may cause database tables to become corrupted. The following discussion describes how this can happen and how to handle it.

 File: manual.info.tmp, Node: corrupted-myisam-tables, Next: myisam-table-close, Prev: myisam-table-problems, Up: myisam-table-problems

15.2.4.1 Corrupted MyISAM Tables ................................

Even though the 'MyISAM' table format is very reliable (all changes to a table made by an SQL statement are written before the statement returns), you can still get corrupted tables if any of the following events occur:

Typical symptoms of a corrupt table are:

You can check the health of a 'MyISAM' table using the note 'CHECK TABLE': check-table. statement, and repair a corrupted 'MyISAM' table with note 'REPAIR TABLE': repair-table. When note 'mysqld': mysqld. is not running, you can also check or repair a table with the note 'myisamchk': myisamchk. command. See note check-table::, note repair-table::, and *note myisamchk::.

If your tables become corrupted frequently, you should try to determine why this is happening. The most important thing to know is whether the table became corrupted as a result of an unexpected server exit. You can verify this easily by looking for a recent 'restarted mysqld' message in the error log. If there is such a message, it is likely that table corruption is a result of the server dying. Otherwise, corruption may have occurred during normal operation. This is a bug. You should try to create a reproducible test case that demonstrates the problem. See note crashing::, and note debugging-mysql::.

 File: manual.info.tmp, Node: myisam-table-close, Prev: corrupted-myisam-tables, Up: myisam-table-problems

15.2.4.2 Problems from Tables Not Being Closed Properly .......................................................

Each 'MyISAM' index file ('.MYI' file) has a counter in the header that can be used to check whether a table has been closed properly. If you get the following warning from note 'CHECK TABLE': check-table. or note 'myisamchk': myisamchk, it means that this counter has gone out of sync:

 clients are using or haven't closed the table properly

This warning does not necessarily mean that the table is corrupted, but you should at least check the table.

The counter works as follows:

In other words, the counter can become incorrect only under these conditions:

 File: manual.info.tmp, Node: memory-storage-engine, Next: csv-storage-engine, Prev: myisam-storage-engine, Up: storage-engines