Go to the first, previous, next, last section, table of contents.


8 MySQL table types

As of MySQL Version 3.23.6, you can choose between three basic table formats. When you create a new table, you can tell MySQL which table type it should use for the table. MySQL will always create a .frm file to hold the table and column definitions. Depending on the table type the index and data will be stored in other files.

The default table type in MySQL is MyISAM. If you are trying to use a table type that is not incompiled or activated, MySQL will instead create a table of type MyISAM.

You can convert tables between different types with the ALTER TABLE statement. See section 7.8 ALTER TABLE Syntax.

Note that MySQL supports two different kind of tables. Transactions safe tables (BDB) and not transaction safe tables (ISAM, MERGE, MyISAM and HEAP).

Advantages of transaction safe tables (TST)

Advantages of not transaction safe tables (NTST):

You can combine TST and NTST tables in the same statements to get the best of both worlds.

8.1 MyISAM tables

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 file with the .MYD (MYData) extension. You can check/repair MyISAM tables with the myisamchk utility. See section 15.4 Using myisamchk for crash recovery.

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 when inserting data into compressed index.

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

Option Meaning
--myisam-recover=# Automatic recover of crashed tables.
-O myisam_sort_buffer_size=# Buffer used when recovering tables.
--delay-key-write-for-all-tables Don't flush key buffers between writes for any MyISAM table

The automatic recovery is activated if you start mysqld with --myisam-recover=#. See section 4.16.4 Command-line 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-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 recover with the BACKUP option, you should have a cron script that automaticly moves file with names like `tablename-datetime.BAK' from the database directories to a backup media.

See section 4.16.4 Command-line Options.

8.1.1 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 which have an identical prefix when the numbers are stored high-byte first.

8.1.2 MyISAM table formats

MyISAM supports 3 different table types. 2 of them are chosen automatically depending on the type of columns you are using. The third, compressed tables, can only be created with the myisampack tool.

8.1.2.1 Static (Fixed-length) table characteristics

This is the default format. It's used when the table contains no VARCHAR, BLOB or TEXT columns.

This format is the simplest and most secure format. It is also the fastest of the on-disk formats. The speed comes from the easy way data can be found on disk. When looking up something with an index and static format it is very simple, just multiply the row number by the row length.

Also when scanning a table it is very easy to read a constant number of records with each disk read.

The security comes from if your computer crashes when writing to a fixed size MyISAM file, myisamchk can easily figure out where each row starts and ends. So it can usually reclaim all records except the partially written one. Note that in MySQL all indexes can always be reconstructed.

8.1.2.2 Dynamic table characteristics

This format is used if the table contains any VARCHAR, BLOB or TEXT columns or if the table was created with ROW_FORMAT=dynamic.

This format is a litte more complex because each row has to have a header that says how long it is. One record can also end up at more than one location when it is made longer at an update.

You can use OPTIMIZE table or myisamchk to defragment a table. If you have static data that you access/change a lot in the same table as some VARCHAR or BLOB columns, it might be a good idea to move the dynamic columns to other tables just to avoid fragmentation.

8.1.2.3 Compressed table characteristics

This is a read-only type that is generated with the optional myisampack tool (pack_isam for ISAM tables).

8.2 MERGE tables

MERGE tables are new in MySQL Version 3.23.25; The code is still in alpha, but should stabilize soon! The one thing that is currently missing is a way from the SQL prompt to say which tables are part of the MERGE table.

A MERGE table is a collection of identical MyISAM tables that can be used as one. You can only SELECT, DELETE and UPDATE from the collection of tables. If you DROP the MERGE table, you are only dropping the MERGE specification.

With identical tables we mean that all tables are created with identical column information. Some of the tables can be compressed with myisampack. See section 14.8 The MySQL compressed read-only table generator..

When you create a MERGE table, you will get a .frm table definition file and a .MRG table list file. The .MRG just contains a list of the index files (.MYI files) that should be used as one.

For the moment you need to have SELECT, UPDATE and DELETE privileges on the tables you map to a MERGE table.

MERGE tables can help you solve the following problems:

The disadvantages with MERGE tables are:

The following example shows you how to use MERGE tables:

CREATE TABLE t1 (a INT AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
CREATE TABLE t2 (a INT AUTO_INCREMENT PRIMARY KEY, message CHAR(20));
INSERT INTO t1 (message) VALUES ("Testing"),("table"),("t1");
INSERT INTO t2 (message) VALUES ("Testing"),("table"),("t2");
CREATE TABLE total (a INT NOT NULL, message CHAR(20), KEY(a)) TYPE=MERGE UNION=(t1,t2);

Note that we didn't create an UNIQUE or PRIMARY KEY in the total table as the key isn't going to be unique in the total table.

Note that you can also manipulate the .MRG file directly from the outside of the MySQL server:

shell> cd /mysql-data-directory/current-database
shell> ls -1 t1.MYI t2.MYI > total.MRG

Now you can do things like:

mysql> select * from total;
+---+---------+
| a | message |
+---+---------+
| 1 | Testing |
| 2 | table   |
| 3 | t1      |
| 1 | Testing |
| 2 | table   |
| 3 | t2      |
+---+---------+

To remap a MERGE table you must either DROP it and recreate it, use ALTER TABLE with a new UNION specification or change the .MRG file and issue a FLUSH TABLE on the MERGE table and all underlying tables to force the handler to read the new definition file.

8.3 ISAM tables

You can also use the deprecated ISAM table type. This will disappear rather soon because MyISAM is a better implementation of the same thing. ISAM uses a B-tree index. The index is stored in a file with the .ISM extension and the data is stored in file with the .ISD extension. You can check/repair ISAM tables with the isamchk utility. See section 15.4 Using myisamchk for crash recovery.

ISAM has the following features/properties:

Most of the things for MyISAM tables are also true for ISAM tables. See section 8.1 MyISAM tables. The major differences compared to MyISAM tables are:

8.4 HEAP tables

HEAP tables use a hashed index and are stored in memory. This makes them very fast, but if MySQL crashes you will lose all data stored in them. HEAP is very useful for temporary tables!

The MySQL internal HEAP tables uses 100% dynamic hashing without overflow areas. There is no extra space needed for free lists. HEAP tables also don't have problems with delete + inserts, which normally is common with hashed tables..

mysql> CREATE TABLE test TYPE=HEAP SELECT ip,SUM(downloads) as down
        FROM log_table GROUP BY ip;
mysql> SELECT COUNT(ip),AVG(down) FROM test;
mysql> DROP TABLE test;

Here are some things you should consider when you use HEAP tables:

Memory needed for one row in a HEAP table is:

SUM_OVER_ALL_KEYS(max_length_of_key + sizeof(char*) * 2)
+ ALIGN(length_of_row+1, sizeof(char*))

sizeof(char*) is 4 on 32 bit machines and 8 on 64 bit machines.

8.5 BDB or Berkeley_db tables

Berkeley DB (http://www.sleepycat.com) has provided MySQL with a transaction safe table handler. This will survive crashes and also provides COMMIT and ROLLBACK on transactions. In order to build MySQL Version 3.23.x (BDB support first appeared in Version 3.23.15) with support for BDB tables, you will need Berkeley DB 3.1.14 or newer which can be downloaded from http://www.mysql.com/downloads/mysql-3.23.html; or also from Sleepycat's download page at http://www.sleepycat.com/download.html.

To install Berkeley DB first uncompress the BDB distribution and follow the instructions in the README provided in the distiribution directory. Basically what you need to do is:

cd build_[your_os]
../dist/configure
make
make install

Please refer to the manual provided by BDB distribution for more/updated information.

After this you need to configure your MySQL with --with-berkeley-db=DIR. The directory is the one where you installed BDB binaries with make install. (Usually it is /usr/local/BerkeleyDB.3.1/) You can give additional options to MySQL configure, --with-berkeley-db-includes=DIR and --with-berkeley-db-libs=DIR, if the BDB includes and/or libs directory is not under the first directory, by default they are. Then complete the MySQL installation as normal.

Even if Berkeley DB is in itself very tested and reliably, the MySQL interface is still very alpha, but we are actively improving and optimizing it to get it this stable real soon.

If you are running with AUTOCOMMIT=0 then your changes in BDB tables will not be updated until you execute COMMIT. Instead of commit you can execute ROLLBACK to forget your changes. See section 7.31 BEGIN/COMMIT/ROLLBACK syntax.

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

Option Meaning
--bdb-home=directory Base directory for BDB tables. This should be the same directory you use for --datadir.
--bdb-lock-detect=# Berkeley lock detect. One of (DEFAULT, OLDEST, RANDOM or YOUNGEST)
--bdb-logdir=directory Berkeley DB log file directory
--bdb-nosync Don't synchronously flush logs
--bdb-recover Start Berkeley DB in recover mode
--bdb-tmpdir=directory Berkeley DB tempfile name
--skip-bdb Don't use berkeley db.

If you use --skip-bdb, MySQL will not initialize the Berkeley DB library and this will save a lot of memory. You can of course not use BDB tables if you are using this option.

Normally you should start mysqld with --bdb-recover if you intend to use BDB tables. This may however give you problems when you try to start mysqld if the BDB log files are corrupted. See section 4.16.2 Problems Starting the MySQL Server.

Some characteristic of BDB tables:

Some things that we have to fix in the near future:


Go to the first, previous, next, last section, table of contents.