Menu

1 / 2

MySQL Generated Columns: VIRTUAL vs STORED, and Which One to Index

DEV Community: database·Son Tran·2 days ago
#GQcvKugN
#devtodatabase#devto#dev#column#table
Long article (2257 words) — no AI summary yet.
Reading 0:00
15s threshold

Disclosure: I build Schemity , a desktop ERD tool - this post is from our blog and uses it for the examples.

TL;DR: Make a MySQL generated column VIRTUAL unless it has to be a primary key, a foreign key, or carry a FULLTEXT or SPATIAL index, which only STORED allows. Adding a VIRTUAL column to 1,000,000 rows was instant and an index on it worked as well as one on a STORED column, while adding the STORED one copied the table for 4.8 seconds with writes blocked. The trap is changing a VIRTUAL column's expression: MODIFY copies the table too, and dropping and re-adding an unindexed column does not.

A MySQL generated column should be VIRTUAL unless you need it as a primary key, a foreign key, or under a FULLTEXT or SPATIAL index, because a VIRTUAL column costs nothing to add, takes no space in the row, and can still be indexed.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.