DbVisualizer
MySQL
SQL

Understanding MVCC in MySQL

intro

Multiversion concurrency control or MVCC is a mechanism that helps us provide concurrent access to the database for multiple transactions. Discover everything you need to know about MVCC and MVCC connect in this blog!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
mariadb MARIADB
THE MARIADB DATABASE
MySQL logo MySQL
THE MYSQL DATABASE

As you might already know, MySQL is a powerful beast. A part of the reason is that it consists of multiple storage engines that make up the DBMS. We’ve written about the ins and outs of one of the most important files in the primary database management system — InnoDB — before, but we haven’t yet told you about one of the mechanisms that makes it tick. That mechanism is called Multiversion Concurrency Control, or MVCC.

What is MVCC?

MVCC is a short acronym for a term referred to as Multiversion Concurrency Control. This is a method DBAs and developers use to optimize their database, and it is commonly used by multiple database management systems to accomplish a goal of providing concurrent access to the database and provide data consistency.

Consider this: if you’re writing to the database at the same time someone is reading from it, you don’t want the reader to see an inconsistent (not fully completed) set of data, right? The question then becomes, how do you approach your database so that all operations would be completed when other people see it? The answer is simple — you use MVCC connect!

Purpose of MVCC

By using MVCC, you ensure that if the situation described above occurs to your database, everyone involved will see a completed — or consistent — set of data.

No matter what kind of relational database management system you use, the core purpose of MVCC is to accomplish a couple of goals. These goals are related to isolation, consistency, and durability and these properties will ensure the following:

  • Isolation — transactions will complete in an isolated way (i.e., one transaction will not affect other transactions.)
  • Consistency — transaction results will be consistent when committed (we’ll walk you through what committing is in a moment.)
  • Durability — transaction results will be durable and won’t disappear even if electricity would disappear in the middle of your database executing an SQL query.

MVCC connect-compliant database engines like InnoDB ensure these properties are being followed to the tea: if they’re not, the engine is not MVCC compliant.

Your Database and MVCC

Many relational databases will follow MVCC properties by default and there’s no “way to disable MVCC connect” in any DBMS, but there are ways to force certain properties upon your database.

“Forcing” certain properties is not necessarily a bad thing — in many cases, that just means that you’re forcing your database to take things slower to ensure that all MVCC properties are followed.

A very popular way to make a database follow MVCC connect properties later on is by disabling automatic commits by using queries like autocommit=0 or equivalents to this query clause. The autocommit=0 query does exactly what you think it does: it disables automatic commit operations until you tell your database to resume them, or until the transaction is finished (i.e., the entire data dump is re-imported back into your database.)

That is important because by default, many database management systems including MySQL would save the results to your database by committing operations after every INSERT. This can become problematic for those running bigger data sets because such operations would necessitate your database pulling a lot of weight — your database would need to insert data, commit, insert data, commit, insert data, commit, then repeat the same cycle n thousand times.

Not great, right? Setting autocommit to 0 and using COMMIT after all of the data is already inserted into your database ensures that your database works without any overhead added to its operations — do that and you will essentially tell your database “insert all data, commit once you’re done and that’s it.” Your database does that by delaying some MVCC connect properties (consistency) to the very last moment and saves you a lot of headaches in the process.

Modifying MVCC Properties

“If delaying MVCC can be useful, wouldn’t it be more useful to delay, or turn off, MVCC as a whole?”, — we hear you asking. And no, that’s not the case — that’s not the case because.

While delaying MVCC may be useful for those working with bigger sets of data (delaying MVCC commit properties would make bulk INSERT queries faster because of the reasons described above), MVCC is vitally important for your database because without it — you’ve guessed — there would be no data consistency.

So no, turning off MVCC connect properties isn’t possible and doing that wouldn’t be a smart idea even if it would be viable, but there are ways to delay them.

Delaying MVCC connect is a great idea when your data sets get bigger because by doing that, you force your database to skip certain steps when loading data into your database, and thus, speed up INSERT operations. To delay MVCC, do one (or more) of the following:

  • Disable automatic commit operations until your data is fully inserted into a database — set autocommit to 0 before inserting data (autocommit=0), then issue a COMMIT; once that operation is finished.
  • Temporarily use storage engines without MVCC properties — another way to delay key MVCC operations would be to use storage engines that provide no support for MVCC in the first place: temporarily using MyISAM in MySQL is a good way to do know the exact count of the rows in a table as well as to delay MVCC, but the key word here is temporarily, which means that you must switch to InnoDB (or any other modern storage engine that provides consistency properties, e.g., XtraDB, once you’re done) — you wouldn’t want a crash to wipe out all of your precious data, would you?

With that said, do keep in mind that delaying MVCC only helps with data sets that are middle-sized, meaning that these methods would be viable for those working with data sets of under 100 million rows, but at the same time, they may not work for bigger data.

MVCC and Big Data

“Can’t I delay MVCC for big data sets?”, — you ask. Technically you can: practically, your storage engine will do that for you. In other words, once your data set gets bigger, forget about fiddling with MVCC because your database will do that for you — you have better things to care about, don’t you?

Once your data gets bigger, it will be no longer viable to delay MVCC because operations related to it would need to be skipped due to overhead to begin with — in other words, we’d need to use LOAD DATA INFILE instead of INSERT in the place, and thus, disabling autocommit operations applicable to INSERTs would no longer make sense.

SQL Clients

When your data gets bigger, your eyesight will likely turn to database clients. After all, you’re reading a blog of an SQL client right now, aren’t you? You’re in the right place — once your data gets bigger and you’ll have more operations to take care of, you will inevitably need some assistance.

Then, you would need to turn to tools that provide that assistance and DbVisualizer is one of them — from importing table data to its powerful SQL editor with autocomplete capabilities, the features of DbVisualizer are nothing short of impressive.

DbVisuazlier can format your SQL queries and split their results into separate tabs (example below — tabs can also be re-arranged or dragged into separate windows if necessary), help you edit your data as if it was a spreadsheet by opening a set of data in Excel for your convenience, help set user privileges for specific queries, or even create monitors to query your database in a set interval.

DbVisualizers SQL client splitting responses into tabs
DbVisualizer’s SQL client splitting responses into tabs
Turning on or off `autocommit` in DbVisualizer
Turning on or off `autocommit` in DbVisualizer

Did we mention that DbVisualizer supports more than 50 data sources? Not finding your database in the list would be close to impossible, so try DbVisualizer today, tell us how it goes by tweeting us at DbVisualizer, and until next time.

Summary

Multiversion Concurrency Control — or MVCC connect for short — has to do with providing concurrent access to consistent data sets. MVCC ensures that a person reading from the database will see a data set that has been fully acted on and no “surprises” will await him afterward.

MVCC ensures that data remains isolated, consistent, and durable — and that you, as a DBA, can go on with your pleasant day.

Aside from MVCC, your database needs to take care of a multitude of other things too — as most of them can be automated by SQL clients, DbVisualizer is a great choice both for starters and senior DBAs alike.

FAQ

What is MVCC connect?

MVCC connect is a technique exclusive to database management systems that ensures concurrent access to a consistent data set. MVCC ensures that data remains isolated, consistent, and durable.

Can MVCC connect be turned off?

No — MVCC connect can, however, be delayed. Delaying MVCC may be useful when importing middle-sized data sets.

Where can I learn more about MVCC and other database topics?

You can learn more about MVCC and other database topics by following database-related blogs like the one you’re reading right now (TheTable), attending conferences, workshops and gatherings, or by watching YouTube videos on channels like Database Dive.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Derived Table: Everything You Need to Know

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-02-06
title

MySQL UPDATE JOIN Queries: A Complete Guide

author Antonello Zanini tags MySQL 8 min 2025-02-04
title

Postgres Create Array of Numbers: Complete Guide

author Lukas Vileikis tags POSTGRESQL SQL 7 min 2025-02-03
title

How to Protect MySQL With Fail2Ban

author Antonello Zanini tags MySQL 12 min 2025-01-30
title

SQL Injection Cheat Sheet: SQLi 101

author Lukas Vileikis tags MySQL SECURITY SQL 10 min 2025-01-29
title

SQL PARTITION BY in PostgreSQL: A Guide to Window Functions and Data Segmentation

author Leslie S. Gyamfi tags SQL 7 min 2025-01-28
title

A Complete Guide to the ALTER TABLE DROP COLUMN Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-01-27
title

How to Use a Recursive CTE in SQL

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 5 min 2025-01-22
title

Guidelines for a Proper SQL Script: ACID, CRUD, and Other Things You Need to Know

author Lukas Vileikis tags DbVisualizer MySQL OPTIMIZATION SQL 6 min 2025-01-21
title

How to Use MERGE in SQL Query Statements: Complete Guide

author Antonello Zanini tags ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-01-20

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.