Choosing the Right MySQL Storage Engine

When working with MySQL, selecting the appropriate storage engine is crucial to meet the specific requirements of your application. In this blog post, we will delve into two commonly used storage engines: MyISAM and InnoDB.

We will examine the differences in their behaviours during transactions by creating two identical tables. Let's dive in!

MyISAM

MyISAM (MySQL Indexed Sequential Access Method) is an older storage engine that used to be the default choice for MySQL (before version 5.5).

  1. B-tree Index: MyISAM utilizes a B-tree index structure, allowing for efficient retrieval by directly pointing to the row data.

  2. Non-Transactional: It does not provide transaction support, meaning it does not guarantee the ACID properties (Atomicity, Consistency, Isolation, Durability) for database operations.

  3. Table-Level Locking: MyISAM performs well with fast insert operations, but updates and deletes can be problematic due to its table-level locking mechanism. This can lead to contention and reduced concurrency in multi-user environments.

  4. No Foreign Key Constraints: MyISAM is less suitable for applications that heavily rely on maintaining relationships between tables.

InnoDB

InnoDB is the default and widely used storage engine in MySQL and MariaDB. It offers robust features to ensure data integrity and provides transactional support.

  1. B+ Tree Structure: InnoDB utilizes a B+ tree index structure. The index points to the primary key, and the primary key points to the actual row of data, enabling efficient querying and retrieval.

  2. ACID Compliance: InnoDB provides full transactional support, guaranteeing Atomicity, Consistency, Isolation, and Durability. This ensures data integrity and reliable processing of complex operations.

  3. Row-Level Locking: InnoDB employs row-level locking, reducing contention and improving concurrency in multi-user environments. Multiple transactions can read and write to the database simultaneously without blocking each other.

  4. Foreign Key Support: InnoDB supports foreign key constraints, allowing you to establish and maintain relationships between tables, thus enforcing referential integrity.


Creating Identical Tables with MyISAM and InnoDB

To understand the differences in behaviour, let's create two identical student tables using MyISAM and InnoDB in MySQL

-- Create student table using the MyISAM storage engine
CREATE TABLE student_myisam_table (
    id INT PRIMARY KEY,
    name VARCHAR(50)
) ENGINE=MyISAM;

DESCRIBE student_myisam_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | NO   | PRI | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
-- Create student table using the InnoDB storage engine
CREATE TABLE student_innodb_table (
    id INT PRIMARY KEY,
    name VARCHAR(50)
) ENGINE=InnoDB;

DESCRIBE student_innodb_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | NO   | PRI | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

Transaction Behavior Comparison

InnoDB - Transactional Database

To demonstrate the transaction support in InnoDB, let's consider two clients simultaneously accessing the student records:

Client 1 starts a transaction to create a student record

-- client 1

-- begins a transaction
mysql> begin;
Query OK, 0 rows affected (0.01 sec)

-- checks for existing records
mysql> select * from student_innodb_table;
Empty set (0.11 sec)

-- inserts a record
mysql> insert into student_innodb_table(id, name) values(1, 'krishna');
Query OK, 1 row affected (0.01 sec)

-- verifies the record created
mysql> select * from student_innodb_table;
+----+---------+
| id | name    |
+----+---------+
|  1 | krishna |
+----+---------+
1 row in set (0.01 sec)

-- not commited yet

Meanwhile, Client 2 attempts to access the student records

-- Client 2

mysql> select * from student_innodb_table;
Empty set (0.00 sec)

-- Empty set (due to ongoing transaction in Client 1)

This confirms the transaction support in InnoDB.

MyISAM - Non-Transactional Database

In contrast, MyISAM does not support transactions. Let's again consider two clients accessing the student records

Client 1 starts a transaction to create a student record

-- client 1

-- begins a transaction
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

-- checks for existing records
mysql> select * from student_myisam_table;
Empty set (0.01 sec)

-- inserts a record
mysql>  insert into student_myisam_table(id, name) values(1, 'krishna');
Query OK, 1 row affected (0.02 sec)

-- verifies the record created
mysql> select * from student_myisam_table;
+----+---------+
| id | name    |
+----+---------+
|  1 | krishna |
+----+---------+
1 row in set (0.00 sec)

-- not commited yet

Meanwhile, Client 2 attempts to access the student records

-- Client 2

mysql> select * from student_myisam_table;
+----+---------+
| id | name    |
+----+---------+
|  1 | krishna |
+----+---------+
1 row in set (0.01 sec)

-- Record is visible even before the transaction is committed

Client 2 can see the student record before the transaction created by Client 1 is committed. Furthermore, MyISAM lacks rollback options.


To conclude, It is essential to consider your specific requirements and understand the trade-offs associated with storage engines before making a decision. While InnoDB provides transaction support, row-level locking, and strict data consistency, MyISAM may still be suitable for certain use cases, such as read-heavy applications or scenarios that require full-text search functionality. By carefully evaluating your application's needs, you can choose the right storage engine to optimize performance and maintain data integrity.

Remember, there is no one-size-fits-all solution when it comes to storage engines. And, as always, It's important to understand our requirements and the trade-offs associated with storage engines before deciding on the one to use.

0
Subscribe to my newsletter

Read articles from Krishnaraj Venkatesan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Krishnaraj Venkatesan
Krishnaraj Venkatesan

Principal Software Engineer at Chargebee. Expert in Java, Database, Integrations, and cloud. Passionate about technology to solve business problems.