Chapter 106. MyISAM Tables

Table of Contents

Space Needed for Keys
MyISAM Table Formats
Static (Fixed-length) Table Characteristics
Dynamic Table Characteristics
Compressed Table Characteristics
MyISAM Table Problems
Corrupted MyISAM Tables
Clients is using or hasn't closed the table properly

MyISAM is the default table type in MySQL Version 3.23. It's based on the ISAM code and has a lot of useful extensions.

The index is stored in a file with the .MYI (MYIndex) extension, and the data is stored in a file with the .MYD (MYData) extension. You can check/repair MyISAM tables with the myisamchk utility. See Crash recovery. You can compress MyISAM tables with myisampack to take up much less space. See myisampack.

The following is new in MyISAM:

MyISAM also supports the following things, which MySQL will be able to use in the near future:

Note that index files are usually much smaller with MyISAM than with ISAM. This means that MyISAM will normally use less system resources than ISAM, but will need more CPU time when inserting data into a compressed index.

The following options to mysqld can be used to change the behavior of MyISAM tables. See SHOW VARIABLES.

OptionDescription
--myisam-recover=#Automatic recovery of crashed tables.
-O myisam_sort_buffer_size=#Buffer used when recovering tables.
--delay-key-write=ALLDon't flush key buffers between writes for any MyISAM table
-O myisam_max_extra_sort_file_size=#Used to help MySQL to decide when to use the slow but safe key cache index create method. Note that this parameter is given in megabytes before 4.0.3 and in bytes beginning with this version.
-O myisam_max_sort_file_size=#Don't use the fast sort index method to created index if the temporary file would get bigger than this. Note that this parameter is given in megabytes before 4.0.3 and in bytes beginning with this version.
-O bulk_insert_buffer_size=#Size of tree cache used in bulk insert optimization. Note that this is a limit per thread!

The automatic recovery is activated if you start mysqld with --myisam-recover=#. See Server options. On open, the table is checked if it's marked as crashed or if the open count variable for the table is not 0 and you are running with --skip-external-locking. If either of the above is true the following happens.

If the recover wouldn't be able to recover all rows from a previous completed statement and you didn't specify FORCE as an option to myisam-recover, then the automatic repair will abort with an error message in the error file:

Error: Couldn't repair table: test.g00pages

If you in this case had used the FORCE option you would instead have got a warning in the error file:

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

Note that if you run automatic recovery with the BACKUP option, you should have a cron script that automatically moves file with names like tablename-datetime.BAK from the database directories to a backup media.

See Server options.

Space Needed for Keys

MySQL can support different index types, but the normal type is ISAM or MyISAM. These use a B-tree index, and 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 we don't have any compressed keys.)

String indexes are space compressed. If the first index part is a string, it will also be prefix compressed. Space compression makes the index file smaller than the above figures if the string column has a lot of trailing space or is a VARCHAR 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 PACK_KEYS=1 when you create the table. This helps when you have many integer keys that have an identical prefix when the numbers are stored high-byte first.