Where the open source database community meets: Use code PERCONA75 and secure your spot for Percona Live.  Register

Virtual columns in MySQL and MariaDB

March 4, 2016
Author
Stephane Combaudon
Share this Post:

MariaDB no longer meeting your needs?

Migrate to Percona software for MySQL – an open source, production-ready, and enterprise-grade MySQL alternative.

Learn More


Virtual columns in MySQL and MariaDB

In this blog post, we compare virtual columns in MySQL and MariaDB.

Virtual columns are one of the standout features in MySQL 5.7. They allow you to store a value derived from one or more columns in the same table. This is a powerful way to build functional indexes. The feature has been available in MariaDB for some time, so it’s worth comparing the implementations.

Documentation

The MariaDB documentation is easy to find.

MySQL 5.7 documentation is less obvious, but the best reference is here.

MariaDB documentation does not clearly explain when to use persistent vs virtual columns. Indexes are only supported on persistent columns, but trade-offs could be better documented.

MySQL documentation suggests using virtual columns by default unless computation cost is high. Stored columns are not required for indexing in most cases.

Syntax

Creating a virtual column is similar in both systems:

However, NOT NULL is not supported in MariaDB:

Materialized columns also differ:

  • MariaDB: PERSISTENT
  • MySQL: STORED

Adding a Virtual Column

MySQL 5.7:

This is a metadata-only change and executes instantly.

MariaDB:

This requires a full table rebuild and is not online. Writes may stall during the operation.


MariaDB insert impact

Indexing

MariaDB requires virtual columns to be PERSISTENT to be indexed.

MySQL 5.7 allows indexing virtual columns without storing them, except when used as a primary key.

Example:

Differences:

  • MySQL: Online operation
  • MariaDB: Blocking operation

Conclusion

While MySQL 5.7 and MariaDB 10 appear similar in supporting virtual columns, their implementations differ significantly in syntax, performance, and operational impact.

MySQL 5.7 provides a more production-friendly implementation, especially for large tables and high-traffic environments.

0 0 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Peter Laursen
10 years ago

There is one more important difference as described (by me) here:
http://blog.webyog.com/2015/11/09/beware-virtual-columns-may-render-backups-unusable/

Mary
10 years ago

It is a little bit hard for understanding, but thank you for the article. I am going to try it with MySQL. Hope, everything will be OK!

MAEDA Atsushi
10 years ago

Hi

Thank you for your nice article.
I translated this into Japanese for users in Japan.
Translated one is as follows

https://yakst.com/ja/posts/3836

If there is any problem, please get in touch with me.
thank you.

Balazs
Balazs
9 years ago

Thank you, but it defeats itself in the examples; you cannot just throw around things like
“ALTER TABLE sbtest1 ADD reverse_pad char(60) GENERATED ALWAYS AS (reverse(pad)) VIRTUAL;”
What is reverse, what is pad?

Balazs
Balazs
9 years ago
Reply to  Balazs

Actually once you get past the facts that “reverse” is a rarely used function in mysql, and “pad” is an example column name, this is quite good.

lirezh
7 years ago

Indexes of virtual columns in Mysql 8.0 and likely older is seriously bugged.
Not recommended.
Performance will degrade over time as it doesn’t refresh content with every update resulting in partial fulltable scans.
You can verify that using EXPLAIN syntax and compare the counters with count()

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved