Storage Engines With Simple Examples
Storage Engines With Simple Examples
The default storage engine for MySQL before version 5.5 was MyISAM. For MySQL 5.5 and
later, the default storage engine is InnoDB. Choosing the right storage engine is an important
strategic decision, which will impact future development.
MyISAM
MEMORY
ARCHIVE
CSV
MERGE
FEDERATED
BLACKHOLE
MyISAM InnoDB
Few DML operations OLTP with a lot of DML
Very fast for selects Slower for selects
Some data can get corrupted Good crash-recovery facility
Cannot be a MASTER server in replication, but can be a SLAVE Perfect for MASTER server in the replication
Supports FOREIGN KEY
Binary logs
Point-in-time recovery
Feature MyISAM innoDB
Storage limits 256TB 64TB
Transactions No Yes
Locking granularity Table Row
MVCC No Yes
Geospatial indexing support Yes No
Full-text search indexes Yes No
Clustered indexes No Yes
Data caches No Yes
Cluster database support No No
Foreign key support No Yes
Point-in-time recovery No Yes
MyISAM
MyISAM is a MySQL default storage engine (Before Mysql 5.5 version). It is a non-
transactional storage engine. It doesn't support transactions. It process the insert and select
statements as fast as possible. It supports fulltest index supporting. And also table level locking.
No foreign key support. It supports 64 indexes per row. Count(*) works fast than Innodb. We can
compress the data here.
mysql>create database exdb;
mysql>use exdb;
mysql>create table emp(id int(10),name varchar(30),sal double(14,2)) engine=”myisam”;
mysql>show tables;
mysql>desc emp;
mysql>show create table emp;
mysql>show columns from emp;
mysql> create table emp2(id int(10),name varchar(30),sal double(14,2),FULLTEXT(name))
engine="MYISAM";
#cd /var/lib/mysql/dbname/
here we can see emp.frm,(It contains structure) emp.MYI (it contains indexex) ,emp.MYD(it
contains data)..So take disk space more.
Innodb
Innodb is a MySQL default storage engine (From 5.5 version). It is a transactional storage
engine. It supports transactions and row-level locking. It provides ACID properties. It
processes update queries as fast as possible. Count(*) little slow than MyISAM. It doesn't
support a full-text index. Here we can't compress the data because everything store in a single
file(ibdata).
Atomicity : a transaction is a unit of operation - either all the transaction's actions are completed
or none are.
Consistency: Any work in progress must not be visible to other transactions until the transaction
has been committed.
Isolation: A transaction should appear to be running by itself, the effects of other ongoing
transactions must be invisible to this transaction, and the effects of this transaction must be
invisible to other ongoing transaction.
Durability: When the transaction is committed, it must be persisted so it is not lost in the event of
a power failure. Only committed transactions are recovered during power-up and crash recovery;
uncommitted work is rollbacked.
#cd /var/lib/mysql/dbname
here we can see the only emp.frm. The data and indexes will be store in the ibdata file.
innodb_file_per_table supports from MySQL 5.5 to stores index and data in .ibd file separately
for every table.
alter an engine
MYSQL> ALTER TABLE EMP2 ENGINE=INNODB;
For this engine, the data will be stored in the memory. When we restart the server we will lose all
the data. When we create a table only the .frm file will be store in the data directory. It doesn't
support BLOB, TEXT data types.
CSV
The CSV storage engine stores data in text files using comma-separated values format.
The CSV storage engine is always compiled into the MySQL server.
CREATE TABLE test_csv (id INT NOT NULL, name CHAR(10) NOT NULL) ENGINE =
CSV;
Creating a CSV table also creates a corresponding Metafile that stores the state of the table and
the number of rows that exist in the table. The name of this file is the same as the name of the ta-
ble with the extension CSM. Parition tables and indexing not supported in CSV Engine.
ARCHIVE
This engine stores data in zip format. It does't support primary key. It is use to stores historical
data. The select statements will be very slow because the data will be stored in compressed
format.
create table test_arch(name varchar(30)) engine=ARCHIVE;
insert into test_arch values ('tom');
insert into test_arch values ('sushma');
insert into test_arch values ('ramya');
insert into test_arch values ('sravya');
insert into test_arch values ('anusha');
MERGE
The MERGE storage engine, also known as the MRG_MyISAM engine, is a collection of
identical MyISAM tables that can be used as one. “Identical” means that all tables have identical
column and index information.
mysql> CREATE TABLE EMP1 (EMPID INT NOT NULL AUTO_INCREMENT PRIMARY
KEY, NAME CHAR(20)) ENGINE=MyISAM;
mysql> CREATE TABLE EMP2 (EMPID INT NOT NULL AUTO_INCREMENT PRIMARY
KEY,NAME CHAR(20)) ENGINE=MyISAM;
FEDERATED
The FEDERATED storage engine is used to access the remote MySQL database without using
replication or cluster technology.
#ifconfig
172.16.88.134 [ My Server ipaddress ]
CREATE TABLE TEST9(ID INT(10),NAME VARCHAR(20),SAL DOUBLE(14,2);
localhost
CREATE TABLE `Table1`(`A` VARCHAR(100),UNIQUE KEY(`A`(30))) ENGINE=FEDERATED
CONNECTION='MYSQL://127.0.0.1:3306/TEST/Table1';
BLACKHOLE
In this location /dev/null storage engine (anything you write to it disappears), This engine you
can use in master-slave replication. In some cases, this will give benefits to replication topology.
mysql> create table black1(name char(20)) engine=BLACKHOLE;
Query OK, 0 rows affected (0.01 sec)