MyISAM

MyISAM: It is the default storage engine for MySQL . It is based on ISAM code and has many useful extensions.

MySQL supports several storage engines that deal with different types of tables. MySQL’s storage engines include some that deal with transactional tables and others that don’t: MyISAM handles non-transactional tables, provides fast data retrieval and storage, and fulltext search capabilities.

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

Summary

[ hide ]

  • 1 Features
  • 2 Boot Options
  • 3 Key space
    • 1 Static tables
      • 1.1 Characteristics of the static format
    • 2 Pivot tables
      • 2.1 Dynamic format features
    • 3 Compressed tables
      • 3.1 Characteristics of the compressed format
    • 4 Problems in MyISAM tables
      • 1 Corrupt MyISAM Tables
    • 5 Source

characteristics

MyISAM Storage Engine Features:

  • All data is stored with the smallest byte first. This makes them independent of the machine and the operating system. The only requirement for binary portability is that the machine uses two’s complement signed integers and IEEE floating point format. The only area of ​​machines that may not support binary compatibility are embedded systems, which sometimes have quirky processors. There is no speed penalty when storing the minor byte first; Bytes in a table record are typically not aligned, and reading an unaligned byte in normal or reverse order is not a problem. Also, the code on the server that chooses the column values ​​is not critical to other code in terms of speed.
  • Large files (up to 63 bit length) are supported on file systems and operating systems that support large files.
  • Dynamic-sized records become much less fragmented when mixed erased with updates and inserts. This is done by automatically combining adjacent deleted blocks and extending blocks if the next block is deleted.
  • The maximum number of indexes per MyISAM table in MySQL0 is 64. This can be changed by recompiling. The maximum number of columns per index is 16.
  • The maximum key length is 1000 bytes. This can be changed by recompiling. In case of a key greater than 250 bytes, a larger block size of 1024 bytes is used.
  • BLOB and TEXT columns can be indexed.
  • NULL values ​​are allowed on indexed columns.
  • All numeric key values ​​are stored with the largest byte first for better index compression.

Boot options

The following mysqld options can be used to change the behavior of MyISAM tables:

–myisam-recover = mode: Change the mode for automatic recovery for MyISAM tables.

–delay-key-write = ALL: Do not dump key buffers between writes for any MyISAM table.

The following system variables affect the behavior of MyISAM tables:

bulk_insert_buffer_size : Size of the cache tree used in insert optimization. This is the limit per flow.

myisam_max_extra_sort_file_size  : Used to help MySQL decide when to use the slow but secure cache key index creation method.

myisam_max_sort_file_size : Do not use the fast index sort method to create an index if the temporary file will be larger than this size.

myisam_sort_buffer_size  : Change the size of the buffer used when retrieving tables. Automatic recovery is activated if you start mysqld with the –myisam-recover option. In that case, when the server opens a MyISAM table, it checks if the table is marked as badly closed or if the count of times the table has been opened is not 0 and it is executing the server with –skip-external-locking. If any of these conditions is true, the following occurs:

  • The table is checked for errors.
  • If the server encounters an error, it tries to do a quick table repair (ordering and not recreating the data file).
  • If the repair fails due to an error in the data file (for example, duplicate key error), the server tries again, this time recreating the data file.
  • If it continues to fail, the server tries once again with the old repair method (written record by record without ordering). This method must be able to repair any kind of error and has low space requirements.

Key space

MyISAM tables use B-tree indexes . The size of the index file can be calculated using (key_length + 4) /0.67, added over all the keys. This is the worst case where all keys are inserted in ordered order and the table has no compressed key.

Character string indexes are compressed in space. If the first part of the index is a string, it also has the compressed prefix. Space compression makes the index file less than the worst case if the character string column has many trailing spaces or is a VARCHAR column that is not always used with full length. Prefix compression is used on keys that start with a character string. Prefix compression helps if there are many character strings with an identical prefix.

In MyISAM tables you can compress prefix numbers by specifying PACK_KEYS = 1 when creating the table. This helps when you have many integer keys with an identical prefix when the numbers are stored with the largest byte first.

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

Static tables

The default format for MyISAM is static. It is used when the table does not contain variable length columns (VARCHAR, BLOB, or TEXT). Each record is stored using a fixed number of bytes.

Of the three MyISAM storage formats, the static format is the simplest and most secure (least subject to corruption). It is also the fastest of the disk formats. The speed comes from the ease with which the records are found in the data file on disk: When searching for a record based on a record number in the index, multiply the record number by the record length. Also when scanning a table, it is very easy to read a constant record number with every disk read operation.

Security is evident if your machine fails while the MySQL server is writing to a fixed-format MyISAM file. In this case myisamchk can easily determine where each record starts and where it ends, so it can usually retrieve all records except partially written ones. Note that MyISAM table indexes can always be rebuilt based on data records.

Static Format Features

  • CHAR columns add spaces up to the column width. This is also true for columns of type NUMERIC and DECIMAL created before MySQL0.3.
  • Very fast.
  • Easy to cache.
  • Easy to rebuild after failure, as the logs are located in fixed positions.
  • Reorganization is not necessary unless you delete a large number of records and want to return free space to the operating system. For this you can use OPTIMIZE TABLE or myisamchk.
  • It usually requires more disk space than for dynamic format tables.

Dynamic tables

The heap format is used if a MyISAM table contains any variable length columns (VARCHAR, BLOB, or TEXT), or if the table is created with the ROW_FORMAT = DYNAMIC option.

This format is a little more complex since each column has a header that indicates the length. A record may end in more than one location when it is lengthened as a result of an update.

OPTIMIZE TABLE or myisamchk can be used to defragment a table. If you have fixed-length columns that you frequently access or change in a table that also contains a variable-length column, the variable-length columns can be moved to other tables to avoid fragmentation.

Dynamic format features

  • All columns of character strings are dynamic except those with length less than four.
  • Each record is preceded by a bitmapthat indicates which columns contain the empty string (for string columns) or zero (for numeric columns). It should be noted that this does not include columns that contain NULL values. If a character string column has a length of zero after removing trailing blanks, or a numeric column has a value of zero, it is marked in the bitmap and is not saved to disk. Non-empty strings are saved as one byte longer than the contents of the string.
  • For fixed-length tables, much less disk space is typically required.
  • Each record uses only as much space as it needs. However, if a record grows, it is divided into as many chunks as necessary, resulting in record fragmentation. For example, if you update a record with information that lengthens the length of the record, the record becomes fragmented. In this case, OPTIMIZE TABLE or myisamchkcan be run from time to time to improve performance.
  • More difficult to rebuild after failure than static format tables, since records can fragment into multiple chunks and some link (fragment) may be missing.

Compressed tables

The compressed storage format is read-only generated with the myisampack tool . Compressed table scans are unzipped by myisamchk .

Characteristics of the compressed format

  • Compressed tables take up very little space. This minimizes disk usage, which is useful when using slow disks (such as CD-ROMs).
  • Each records is compressed separately, so there is little access overhead. The header of a record takes 1 to 3 bytes depending on the largest record in the table. Each column is compressed differently. There is usually a Huffman tree for each column. Some of the compression types are:

– Spatial compression of suffix.

– Prefix spatial compression.

– Numbers with a value of zero are stored using a bit.

– If the values ​​of a starting column have a small range, the column is stored using the smallest possible type. For example, a BIGINT column (eight bytes) can be stored as a TINYINT column (one byte) if all the values ​​are in the range -128 to 127.

– If a column has only a small set of possible values, the column type is converted to ENUM.

– A column can use any combination of the preceding compression types.

  • They can handle records of fixed or variable length.

Problems in MyISAM tables

The file format that MySQL uses to store data has been extensively tested, but there are always circumstances that can cause tables to become corrupted.

Corrupt MyISAM tables

Even the MyISAM table format is very reliable (all changes made to a table by an SQL command are written before the command returns), you can get corrupt tables if any of the following events occurs:

  • The mysqld process dies during a write.
  • The machine turns off unexpectedly.
  • Hardwarefailures .
  • Use an external program like myisamchkon a table that is being modified by the server at the same time.
  • A bug in the MySQLor MyISAM code .

The typical symptoms of a corrupt table are:

  • You get the following error when selecting data from a table:

Incorrect key file for table: ‘…’. Try to repair it

  • Queries do not get records in the table or return incomplete data.

You can check the health of a MyISAM table using the CHECK TABLE command, and repair a corrupt MyISAM table with REPAIR TABLE. When mysqld is not running, you can check or repair a table with the myisamchk command .

 

Leave a Comment

Scroll to Top