Table of Contents
What is a storage engine?
Storage engines in MySQL are responsible for handling a database table's SQL operations, and managing how data is stored and retrieved. Each engine offers different features and advantages, making it essential to select the one that aligns best with your application's needs. Now Let's see the Example of a Storage Engine.
A storage engine is a software module that a database management system uses to create, read, and update data from a database.
There are two types of storage engines in MySQL : transactional and non-transactional.
Example of Storage Engines
InnoDB Storage Engine
It is an engine that performs well and offers many of the required attributes that any database would need. Oracle recommends using InnoDB for tables except for specialized use cases.
Features of InnoDB Engine
1. Full ACID compliance.
2. Commit, rollback, and crash recovery.
3. Row-level locking.
4. FOREIGN KEY referential-integrity constraints.
5. Increase multi-user concurrency (via non-locking reads).
Example of InnoDB Engine
Step 1) Log in to MySQL and check the database status.
Output
mysql> show databases;
+——————–+
| Database
+——————–+
| information_schema
| MySQL
| performance_schema
| sys
| world
+———————+
Step 2) Create a Table In the InnoDB Storage Engine.
Command: Use World;
Output
mysql> use world;
Database changed
Command: CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=InnoDB;
Output
mysql> CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=InnoDB;
Query OK, 0 rows affected (8.04 sec)
mysql> SELECT * FROM continents;
Empty set (0.00 sec)
Step 3) Insert Data into the Table.
Output
mysql> INSERT INTO continents(id,name)
-> VALUES
-> (1, 'Asia'),
-> (2, 'Africa'),
-> (3, 'Europe'),
-> (4,' North America'),
-> (5,' South America'),
-> (6,' Australia'),
-> (7, 'Antarctica');
Query OK, 7 rows affected (0.82 sec)
Records: 7 Duplicates: 0 Warnings: 0
Step 4) Now get the table information from the Information_schema database.
Output
mysql> SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='InnoDB' and TABLE_NAME='continents';
+————+————+——–+————+———————+
| TABLE_NAME | TABLE_TYPE | ENGINE | TABLE_ROWS | CREATE_TIME |
+————+————+——–+————+———————+
| continents | BASE TABLE | InnoDB | 7 | 2024-09-22 21:05:30 |
+————+————+——–+————+———————+
1 row in set (0.00 sec)
Step 5) Now you can check the data inside the Continents table.
Output
mysql> SELECT * FROM continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 5 | South America
| 6 | Australia
| 7 | Antarctica
+—-+—————+
7 rows in set (0.00 sec)
Step 6) Now check whether this engine follows the ACID, Index Creation, and Row Level Locking mechanism.
ACID Complaint
mysql> START TRANSACTION; ——————–> Need to start transaction First
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE continents SET name='sou' WHERE id=5; ———–> Table has been updated
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 5 | sou ————————————————> Table name has been changed
| 6 | Australia
| 7 | Antarctica
+—-+—————+
7 rows in set (0.00 sec)
mysql> ROLLBACK; ————————————-> Rollback the transaction
Query OK, 0 rows affected (0.09 sec)
mysql> SELECT * FROM continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 5 | South America —————————-> Table name has been changed to old value
| 6 | Australia
| 7 | Antarctica
+—-+—————+
7 rows in set (0.00 sec)
Index Creation
mysql> CREATE INDEX idx_id ON continents (id); ——————–> Index has been created
Query OK, 0 rows affected (0.42 sec)
Records: 0 Duplicates: 0 Warnings: 0
Row Level Locking
mysql> START TRANSACTION; ——————–> Need to start transaction First
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE continents SET name='sou' WHERE id=5; —-> Table updated but not committed
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> use world;——————-> Open a new terminal and connect with the database
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> DELETE FROM continents WHERE id=5;–>Parallel run the Delete command which will hang
mysql> commit; ——–> Come back to the old window and execute the commit statement
Query OK, 0 rows affected (0.11 sec)
mysql> DELETE FROM continents WHERE id=5; —–> Once commit happens Delete statement will get executed
Query OK, 1 row affected (5.93 sec)
mysql> SELECT * FROM continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 6 | Australia
| 7 | Antarctica
+—-+—————+
6 rows in set (0.01 sec)
Note: – 5th Row has been deleted successfully.
So this concludes our example for the InnoDB Engine.
MEMORY Storage Engine
1. It Creates tables in memory.
2. It is the fastest engine.
3. It provides table-level locking.
4. It does not support transactions.
5. A memory storage engine is ideal for creating temporary tables or quick lookups.
6. The data is lost when the database is restarted.
Example of MEMORY Engine
Step 1) Log in to MySQL and check the database status.
Output
mysql> show databases;
+——————–+
| Database
+——————–+
| information_schema
| MySQL
| performance_schema
| sys
| world
+———————+
Step 2) Drop the continents table and recreate it with the Memory Storage Engine.
Command: Use World;
Output
mysql> use world;
Database changed
mysql> drop table continents;
Query OK, 0 rows affected (0.05 sec)
Command: CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=MEMORY;
Output
mysql> CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=MEMORY;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM continents;
Empty set (0.00 sec)
Step 3) Insert Data into the Table.
Output
mysql> INSERT INTO continents(id,name)
-> VALUES
-> (1, 'Asia'),
-> (2, 'Africa'),
-> (3, 'Europe'),
-> (4,' North America'),
-> (5,' South America'),
-> (6,' Australia'),
-> (7, 'Antarctica');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
Step 4) Now get the table information from the Information_schema database.
Output
mysql> SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='MEMORY';
+————+————+——–+————+———————+
| TABLE_NAME | TABLE_TYPE | ENGINE | TABLE_ROWS | CREATE_TIME |
+————+————+——–+————+———————+
| continents | BASE TABLE | MEMORY | 7 | 2024-09-22 22:44:14 |
+————+————+——–+————+———————+
1 row in set (0.01 sec)
Step 5) Now you can check the data inside the Continents table.
Output
mysql> SELECT * FROM continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 5 | South America
| 6 | Australia
| 7 | Antarctica
+—-+—————+
Step 6) Restart MySQL Service and check if the data is there.
Output
Status
[root@mysql2 ~]# systemctl status mysqld@MYS00407
● mysqld@MYS00407.service – Multi-Instance MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld@.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2024-09-21 11:10:58 IST; 1 day 11h ago
Main PID: 1416 (mysqld)
CGroup: /system.slice/system-mysqld.slice/mysqld@MYS00407.service
└─1416 /local/mysqlserver/mysql-8.0.39/admin/mysqlhome/bin/mysqld –defaults-file=/local/mysqlserver/mysql-8.0.39/data/my.cnf
Stop
[root@mysql2 ~]# systemctl stop mysqld@MYS00407
Start
[root@mysql2 ~]# systemctl start mysqld@MYS00407
Status
[root@mysql2 ~]# systemctl status mysqld@MYS00407
● mysqld@MYS00407.service – Multi-Instance MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld@.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2024-09-22 22:58:44 IST; 3s ago
Main PID: 28811 (mysqld)
CGroup: /system.slice/system-mysqld.slice/mysqld@MYS00407.service
└─28811 /local/mysqlserver/mysql-8.0.39/admin/mysqlhome/bin/mysqld –defaults-file=/local/mysqlserver/mysql-8.0.39/data/my.cnf
Step 7) Now again check the data inside the Continents table.
Note: – The table structure will remain intact, but the data will be absent.
Output
mysql> show tables;
+—————–+
| Tables_in_world
+—————–+
| city
| continents————————> We can see table structure is present
| country
| countrylanguage
+—————–+
4 rows in set (0.00 sec)
mysql> select * from continents;
Empty set (0.00 sec)——————–> Data has gone
We can see data was present in the memory but not in the persistent state that's why after restart it has gone. So this concludes our example for the Memory Engine.
CSV Storage Engine
1. A useful storage engine when data needs to be shared with other applications that use CSV formatted data.
2. The tables are stored as comma-separated value text files.
3. Though this makes sharing the data with scripts and applications easier, one drawback is that the CSV files are not indexed.
4. So, the data should be stored in an InnoDB table until the Import/Export stage of the process.
Example of CSV Engine
Step 1) Log in to MySQL and check the database status.
Output
mysql> show databases;
+——————–+
| Database
+——————–+
| information_schema
| MySQL
| performance_schema
| sys
| world
+———————+
Step 2) Drop the continents table and recreate it with the CSV Storage Engine.
Command: Use World;
Output
mysql> use world;
Database changed
mysql> drop table continents;
Query OK, 0 rows affected (0.05 sec)
Command: CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=CSV;
Output
mysql> CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=CSV;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM continents;
Empty set (0.00 sec)
Step 3) Insert Data into the Table.
Output
mysql> INSERT INTO continents(id,name)
-> VALUES
-> (1, 'Asia'),
-> (2, 'Africa'),
-> (3, 'Europe'),
-> (4,' North America'),
-> (5,' South America'),
-> (6,' Australia'),
-> (7, 'Antarctica');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
Step 4) Now get the table information from the Information_schema database.
Output
mysql> SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='CSV' and TABLE_NAME='continents';
+————+————+——–+————+———————+
| TABLE_NAME | TABLE_TYPE | ENGINE | TABLE_ROWS | CREATE_TIME |
+————+————+——–+————+———————+
| continents | BASE TABLE | CSV | 7 | 2024-09-22 23:19:51 |
+————+————+——–+————+———————+
Step 5) Now you can check the data inside the Continents table.
Output
mysql> SELECT * FROM continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 5 | South America
| 6 | Australia
| 7 | Antarctica
+—-+—————+
Step 6) Now you can go to the $DATA_DIR/database_name directory and check if the CSV file is present.
Command to find the DATA_DIR location
mysql> show variables like '%datadir%';
+—————+—————————————+
| Variable_name | Value |
+—————+—————————————+
| datadir | /local/mysqlserver/mysql-8.0.39/data/
+—————+—————————————+
1 row in set (0.01 sec)
We got the data directory path and now we need to go inside the database directory in my case it is WORLD.
[mysql@mysql2 world]$ cd /local/mysqlserver/mysql-8.0.39/data/world—> Go to the directory path
[mysql@mysql2 world]$ ls -lrt *.CSV ————————–> Search for the CSV file
-rw-r—– 1 mysql mysql 96 Sep 22 23:20 continents.CSV
[mysql@mysql2 world]$ file /local/mysqlserver/mysql-8.0.39/data/world/continents.CSV
/local/mysqlserver/mysql-8.0.39/data/world/continents.CSV: ASCII text —-> Clearly shown it is a text file which means we can see the content of this file and even export the data as well in Excel sheet format.
[mysql@mysql2 world]$ cat /local/mysqlserver/mysql-8.0.39/data/world/continents.CSV
1, "Asia"
2, "Africa"
3, "Europe"
4, "North America"
5, "South America"
6, "Australia"
7, "Antarctica"
So, this concludes our example for the CSV Engine.
BLACKHOLE Storage Engine
1. The Blackhole storage engine accepts but does not store data.
2. Retrievals always return an empty set.
3. The functionality can be used in distributed database design where data is automatically replicated, but not stored locally.
4. This storage engine can be used to perform performance tests or other testing.
Example of BLACKHOLE Engine
Step 1) Log in to MySQL and check the database status.
Output
mysql> show databases;
+——————–+
| Database
+——————–+
| information_schema
| MySQL
| performance_schema
| sys
| world
+———————+
Step 2) Drop the continents table and recreate it with the BLACKHOLE Storage Engine.
Command: Use World;
Output
mysql> use world;
Database changed
mysql> drop table continents;
Query OK, 0 rows affected (0.05 sec)
Command: CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=BLACKHOLE;
Output
mysql> CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=BLACKHOLE;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM continents;
Empty set (0.00 sec)
Step 3) Insert Data into the Table.
Output
mysql> INSERT INTO continents(id,name)
-> VALUES
-> (1, 'Asia'),
-> (2, 'Africa'),
-> (3, 'Europe'),
-> (4,' North America'),
-> (5,' South America'),
-> (6,' Australia'),
-> (7, 'Antarctica');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
Step 4) Now get the table information from the Information_schema database.
Output
mysql> SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='BLACKHOLE';
+————+————+———–+————+———————+
| TABLE_NAME | TABLE_TYPE | ENGINE | TABLE_ROWS | CREATE_TIME |
+————+————+———–+————+———————+
| continents | BASE TABLE | BLACKHOLE | 0 | 2024-09-22 23:41:16 |
+————+————+———–+————+———————+
1 row in set (0.01 sec)
Step 5) Now you can check the data inside the Continents table and guess what it will show as empty.
Output
mysql> SELECT * FROM continents;
Empty set (0.00 sec)
So, this concludes our example for the BLACKHOLE Engine.
FEDERATED Storage Engine
1. This storage engine is for creating a single, local, logical database by linking several different physical MySQL servers.
2. No data is stored on the local server.
3. Queries are automatically executed on the respective remote server.
4. It is perfect for distributed data mart environments and can vastly improve performance when using MySQL for analytical reporting.
Note: – This engine is disabled by default So, I have not taken any example for this one, but in case if you want to practice and see the example of this engine as well then please do let me know in the comment section. I would be happy to create and share it with you all.
MyISAM Storage Engine
1. This storage engine supports full-text search indexes.
2. It also supports table-level locking.
3. In this engine there is a lack of support for transactions. (ACID properties)
4. Though it is a fast storage engine, it is best suited for use in read-heavy and mostly read applications such as data warehousing and web applications that don't need transaction support or ACID compliance.
Example of MyISAM Engine
Step 1) Log in to MySQL and check the database status.
Output
mysql> show databases;
+——————–+
| Database
+——————–+
| information_schema
| MySQL
| performance_schema
| sys
| world
+———————+
Step 2) Drop the continents table and recreate it with the MyISAM Storage Engine.
Command: Use World;
Output
mysql> use world;
Database changed
mysql> drop table continents;
Query OK, 0 rows affected (0.05 sec)
Command: CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=MyISAM;
Output
mysql> CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=MyISAM;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM continents;
Empty set (0.00 sec)
Step 3) Insert Data into the Table.
Output
mysql> INSERT INTO continents(id,name)
-> VALUES
-> (1, 'Asia'),
-> (2, 'Africa'),
-> (3, 'Europe'),
-> (4,' North America'),
-> (5,' South America'),
-> (6,' Australia'),
-> (7, 'Antarctica');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
Step 4) Now get the table information from the Information_schema database.
Output
mysql> SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='MyISAM';
+————+————+——–+————+———————+
| TABLE_NAME | TABLE_TYPE | ENGINE | TABLE_ROWS | CREATE_TIME |
+————+————+——–+————+———————+
| continents | BASE TABLE | MyISAM | 7 | 2024-09-23 00:01:49 |
+————+————+——–+————+———————+
Step 5) Now check whether this engine follows the ACID mechanism.
ACID Complaint
mysql> START TRANSACTION; ——————–> Need to start transaction First
Query OK, 0 rows affected (0.00 sec)
mysql> UPDATE continents SET name='ant' WHERE id=7; ———–> Table has been updated
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> ROLLBACK; ——————————> Rollback the transaction
Query OK, 0 rows affected, 1 warning (0.00 sec)————> We will get the warning
mysql>
mysql> show warnings; ——————————–> Check the Warning
+———+——+—————————————————————+
| Level | Code | Message |
+———+——+—————————————————————+
| Notice | 1196 | Some non-transactional changed tables couldn't be rolled back |
+———+——+—————————————————————+
1 row in set (0.00 sec)
mysql> select * from continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 5 | South America
| 6 | Australia
| 7 | ant———————-> It shows column value has been changed and didn't rollback
+—-+—————+
7 rows in set (0.00 sec)
Note: – It clearly shows Some non-transactional changed tables couldn't be rolled back which means this engine does not support the ACID mechanism.
So, this concludes our example for the MyISAM Engine.
ARCHIVE Storage Engine
1. The ARCHIVE storage engine creates specialized tables designed to hold vast amounts of unindexed data, all while maintaining a minimal storage footprint.
2. It Generates .ARZ files that share the same name as the corresponding table.
3. ARZ files are binary data files known as MySQL Archive Storage Engine data files.
4. It uses gzip to compress the rows.
Example of ARCHIVE Engine
Step 1) Log in to MySQL and check the database status.
Output
mysql> show databases;
+——————–+
| Database
+——————–+
| information_schema
| MySQL
| performance_schema
| sys
| world
+———————+
Step 2) Drop the continents table and recreate it with the ARCHIVE Storage Engine.
Command: Use World;
Output
mysql> use world;
Database changed
mysql> drop table continents;
Query OK, 0 rows affected (0.05 sec)
Command: CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=ARCHIVE;
Output
mysql> CREATE TABLE continents ( id int NOT NULL, name VARCHAR(25) NOT NULL) ENGINE=ARCHIVE;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM continents;
Empty set (0.00 sec)
Step 3) Insert Data into the Table.
Output
mysql> INSERT INTO continents(id,name)
-> VALUES
-> (1, 'Asia'),
-> (2, 'Africa'),
-> (3, 'Europe'),
-> (4,' North America'),
-> (5,' South America'),
-> (6,' Australia'),
-> (7, 'Antarctica');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
Step 4) Now get the table information from the Information_schema database.
Output
mysql> SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_ROWS,CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='ARCHIVE';
+————+————+———+————+———————+
| TABLE_NAME | TABLE_TYPE | ENGINE | TABLE_ROWS | CREATE_TIME |
+————+————+———+————+———————+
| continents | BASE TABLE | ARCHIVE | 0 | 2024-09-23 00:22:07 |
+————+————+———+————+———————+
Step 5) Now you can check the data inside the Continents table.
Output
mysql> SELECT * FROM continents;
+—-+—————+
| id | name
+—-+—————+
| 1 | Asia
| 2 | Africa
| 3 | Europe
| 4 | North America
| 5 | South America
| 6 | Australia
| 7 | Antarctica
+—-+—————+
Step 6) Now you can go to the $DATA_DIR/database_name directory and check if the ARZ file is present.
Command to find the DATA_DIR location
mysql> show variables like '%datadir%';
+—————+—————————————+
| Variable_name | Value |
+—————+—————————————+
| datadir | /local/mysqlserver/mysql-8.0.39/data/
+—————+—————————————+
1 row in set (0.01 sec)
We got the data directory path and now we need to go inside the database directory in my case it is WORLD.
[mysql@mysql2 world]$ cd /local/mysqlserver/mysql-8.0.39/data/world—> Go to the directory path
[mysql@mysql2 world]$ ls -lrt *.ARZ ———> Search for the ARZ file
-rw-r—– 1 mysql mysql 88 Sep 23 00:22 continents.ARZ
We can see from our output ARZ file has been created we can use this method when we need to store a large amount of data So, this concludes our example for the ARCHIVE Engine.
FAQ's
Is InnoDB a storage engine?
Yes, InnoDB is a Storage Engine and it is a default storage engine in MySQL, chosen for its robustness, reliability, and performance.
It has various features like ACID Compliance, Transactions, Row-Level Locking, Efficient Memory Usage, Crash Recovery, and many more.
Which MySQL engine is faster?
There is no doubt that from MySQL version 5.7, the InnoDB storage engine is the fastest one.