Table of Contents
Introduction
In this article, we will get a detailed overview of Storage Engines in MySQL. As we all know MySQL is an open-source relational database that is very easy to use It is also owned by Oracle.
You can check the ranking of the databases as well which shows after Oracle, MySQL is the most widely used open-source database in the world. You can also Install the MySQL server on your machine and start practicing it.
What is a storage engine in MySQL?
Storage engines in MySQL are a part that manages a database table's SQL operations. It helps to retrieve and store the data inside the database. The features and benefits of storage engines vary, therefore it is critical to choose the one that best suits the requirements of your application.
MySQL Engine Types
MySQL supports multiple storage engines, and each one of them is designed in a way to meet different needs. Here comes the main part to explore each storage engine in MySQL.
MySQL Engine Types
Command : – show engines;
Engine | Support | Comment | Transactions | XA | Savepoints |
FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
MEMORY | YES | Hash-based, stored in memory, useful for temporary tables | NO | NO | NO |
InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
MyISAM | YES | MyISAM storage engine | NO | NO | NO |
BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
CSV | YES | CSV storage engine | NO | NO | NO |
ARCHIVE | YES | Archive storage engine | NO | NO | NO |
Default Storage Engine in MySQL
InnoDB is the default storage engine in MySQL, chosen for its robustness, reliability, and performance. Here’s a detailed explanation of why it’s the default and its key features:
1. ACID Compliance
InnoDB is the storage engine that supports all types of transactions.
A) Atomicity: It involves transactions like COMMIT & ROLLBACK.
B) Consistency : It is a mechanism used for Crash Recovery.
C) Isolation : Different isolation levels apply at each transaction level.
D) Durability : The storage engine interacts with underlying hardware to provide the best performance
2. Transactions
InnoDB is a transactional storage engine supporting Commit, Rollback, and Savepoint operations. Transactions help to ensure data integrity, especially in the case of complex operations.
3. Row-Level Locking
It uses Row-level locking instead of table-level locking, It also supports Indexing.
4. Efficient Memory Usage
It maintains its own buffer pool (memory area where the InnoDB cache table and indexed data are stored)
5. Crash Recovery
It has also supported the crash recovery mechanism. At the time of system failure, it uses its transactional logs and checkpoint system to detect the failure and resolve it.
In-Depth Exploration of MySQL Storage Engines
1. FEDERATED Storage Engine
a) FEDERATED Storage Engine is Disabled by default.
b) The Tables that are created with FEDERATED Storage Engine, normally point to a table in another MySQL instance installed on a separate server.
c) In Microsoft SQL Server this mechanism is called Linked Server whereas in Oracle it is called Database link.
d) Both tables should have the same name and definition i.e. Requester and Target tables.
e) The table in the requester acts like a view.
f) The target table can have a different storage engine but the requester table should be created with FEDERATED.
Limitation
a) This storage engine is disabled by default so we cannot touch it also it does not support any transactions support.
Syntax
Create table employees (Employee_id int, Employee_salary int) ENGINE=FEDERATED CONNECTION = 'mysql://db_user@target-server:3306/employees/employees';
2. MEMORY Storage Engine
a) In MEMORY storage engine Table data will be written in Memory only instead of the physical files.
b) In previous versions it was called HEAP, but now it has been renamed to Memory Storage engine.
c) It is very useful for the temporary tables.
d) It is temporary which means data will be lost on server crash.
e) Extremely fast data access, but memory is volatile, so it's best suited for read-only cache or temporary tables.
Limitations
a) No Transaction support.
b) No Referential Integrity support – No Foreign Key.
c) No TEXT data type support – No BLOB column.
Syntax
CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=MEMORY;
3. BLACKHOLE Storage Engine
a) BLACKHOLE Storage engine acts like a Black hole, which means whatever the data goes in this engine will never come back. It will disappear.
b) You can store as much data as you want, when you retrieve it, it returns an empty result set.
Limitation
a) It does not support transaction mechanisms.
Syntax
CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=BLACKHOLE;
4. CSV Storage Engine
a) CSV storage engine stores tables in text files using comma-separated values format.
b) MySQL creates a .csv file in the $DATA_DIR – plain text file.
c) CSV format can be read, and written by spreadsheet applications like Excel.
Note: – Below is the command to find out the $DATA_DIR path.
Limitations
a) It does not support transaction mechanisms.
b) CSV files are not indexed.
Syntax
CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=CSV;
5. MyISAM Storage Engine
a) MyISAM = My + ISAM = Indexed Sequential Access Method.
b) Indexing algorithm developed by IBM that allows retrieving information from large sets of data in a fast way.
c) MyISAM was the default storage engine up until MySQL 5.5 – around 2009-2010.
d) Good speed advantages especially useful in Data warehouse scenarios.
e) it is replaced by InnoDB.
Limitation
a) It does not support transactions – ACID Model
Note: – MyISAM ENGINE is used in Data Warehouse databases where there are a lot of reads of the records.
Syntax
CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=MYISAM;
6. ARCHIVE Storage Engine
a) ARCHIVE Storage Engine Produces special-purpose tables that store large amounts of un-indexed data in a very small footprint.
b) Creates .ARZ files with the same name as the table name.
c) ARZ files are binary data files and are called MySQL Archive Storage Engine Data Files.
d) Uses gzip to compress rows.
Limitations
a) No DELETE or UPDATE operation.
b) No Partitioning.
Syntax
CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=ARCHIVE;
7. InnoDB Storage Engine
a) It is a general-purpose storage engine that balances high reliability and high
performance.
b) In MySQL 8, It is a default storage engine.
c) Its DML operations follow the ACID model.
d) Transactions support commit, rollback, and crash-recovery to protect data.
e) It supports the Row-Level Locking function.
f) InnoDB tables arrange data on disk to optimize queries based on PK.
g) Each InnoDB table has a primary key index, clustered index, and arranged data.
h) Data Integrity supports FK constraints.
Syntax
CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=InnoDB;
CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL);
Note: – We can use the above commands to create a table in the InnoDB engine, if we cannot give the engine value then also it will be created under the InnonDb engine as it is set by default.
Please click here to see the example of storage engine.
Conclusion
A storage engine dictates the way MySQL handles the storage, retrieval, and processing of data within tables. InnoDB serves as the default storage engine in MySQL.
FAQ's
How many types of engines are in MySQL?
There are a total of 11 engines are there and every engine has its role. However, we need to choose the engine accordingly as per our use. InnoDB is the default engine from MySQL version 8, before that it was MyISAM.
You can use the below command to check the details of Storage Engines.
Command : show engines;
How to change storage engine in MySQL?
You can use the below steps If you want to change the storage engine of a table in MySQL.
a) You can check the present storage engine of a table by using the below command.
select table_name, engine from information_schema.tables where table_name='&TABLE_NAME';
b) Now you can use the below command to change the Storage engine of a Table.
Command : ALTER TABLE (&TABLE_NAME) ENGINE = 'ENGINE_NAME';
eg. ALTER TABLE continents ENGINE = 'InnoDB';
What is the difference between InnoDB and MyISAM?
Below are the differences between InnoDB and MyIASM.
a) Transactions : It fully supports ACID-compliant transactions in InnoDB whereas it is not supported in MyIASM.
b) Foreign Key Constraints : It Supports foreign key relationships for referential integrity In InnoDB whereas it is not supported in MyIASM.
c) Locking Mechanism : InnoDB uses row-level locking for better concurrency whereas MyISAM Uses table-level locking.
d) Crash Recovery : Robust crash recovery with automatic data recovery in the InnoDB engine whereas Limited crash recovery, may lose data in MyISAM.
e) Replication : InnoDB Supports row-based and statement-based replication whereas MyIASM Supports only statement-based replication.
f) Use Case : InnoDB is Best for transactional systems like e-commerce, and banking apps whereas MyIASM is Ideal for read-heavy applications like data warehouses.