Why Does MariaDB 10.6.11 Copy Data When Adding a New Column (NULL Default) to the End of a Table?

2024-07-27

In MariaDB versions prior to 10.6.11, adding a new column to an existing table with the InnoDB storage engine typically involved a process known as a "silent table rebuild." This rebuild involved:

  1. Creating a temporary table: MariaDB would create a temporary table in the background with the same structure as the original table, but including the new column.
  2. Copying data: Each row from the original table was copied into the temporary table, with the new column filled with the default value (NULL in your case).
  3. Dropping the original table: Once the copy was complete, MariaDB would drop the original table.
  4. Renaming the temporary table: Finally, the temporary table, now containing the data with the added column, would be renamed to the original table's name.

This approach had some limitations:

  • Downtime: While the rebuild was happening, the table would be temporarily unavailable for writes (inserts and updates).
  • Resource consumption: Copying a large table could be time-consuming and resource-intensive, especially on systems with limited disk space or memory.

Changes in MariaDB 10.6.11

MariaDB 10.6.11 introduced an optimization for certain scenarios when adding a column to the end of an InnoDB table:

  • "Instant ADD COLUMN" for NULL defaults: If the new column has a default value of NULL (like in your case), and certain other conditions are met (such as not having a FULLTEXT index), MariaDB can perform an "instant ADD COLUMN" operation.
  • No temporary table: In this case, MariaDB doesn't need to create a temporary table or copy the data. Instead, it updates the table structure in-place, adding the new column definition and setting its default value for existing rows.
  • Faster and less disruptive: This "instant ADD COLUMN" approach is significantly faster and less disruptive compared to the traditional rebuild method.

Conditions for "Instant ADD COLUMN"

For the "instant ADD COLUMN" optimization to work, the following conditions must generally be true:

  • The table uses the InnoDB storage engine.
  • The new column is added at the end of the table (not in the middle).
  • The new column has a default value of NULL.
  • The table doesn't have a FULLTEXT index.

If any of these conditions aren't met, MariaDB might still have to resort to the traditional rebuild method, which would involve copying to a temporary table.




-- Assuming you have a table named `customers` with existing columns

ALTER TABLE customers
ADD COLUMN new_column VARCHAR(255) DEFAULT NULL;

In this case, if the customers table uses InnoDB, the new new_column will be added at the end with a default value of NULL, potentially using the "instant ADD COLUMN" method (assuming other conditions are met).

Scenario 2: Traditional Rebuild (MariaDB versions before 10.6.11 or when "Instant ADD COLUMN" is not applicable)

-- Assuming you have a table named `products` with existing columns

ALTER TABLE products
ADD COLUMN discount DECIMAL(5,2) DEFAULT 0.00;  -- Not NULL default for illustration

-- This might involve a silent table rebuild in older MariaDB versions.

Here, the products table might undergo a temporary rebuild because the default value isn't NULL (for demonstration purposes). This could involve copying data to a temporary table.




This method involves manually handling the data migration and involves more steps:

  • Create a New Table: Define a new table with the desired structure, including the new column.
  • Copy Data (Potentially with Transformation): Use a SELECT statement to extract data from the original table, optionally performing any necessary transformations for the new column. Insert the data with the new column into the new table.
  • Drop the Original Table (Optional): Once the data has been migrated, you can drop the original table if it's no longer needed.
  • Rename the New Table (Optional): If you want to keep the original table name, you can rename the new table to match it.

Drawbacks:

  • More complex and error-prone compared to ALTER TABLE.
  • Can be significantly slower and resource-intensive for large tables.

Partitioning (Advanced):

If you're using MariaDB with table partitioning enabled, you can potentially add columns to individual partitions instead of the entire table at once. This can be helpful for reducing downtime when dealing with very large tables. However, partitioning is an advanced feature and requires careful planning and understanding.

Recommendation:

In most cases, the ALTER TABLE statement with the "instant ADD COLUMN" optimization (if applicable) is the most efficient and straightforward approach. However, if you have specific requirements or limitations, the manual data manipulation method or partitioning (for advanced users) might be considered as alternatives.


mariadb



Understanding Example Codes for Granting All Privileges in MySQL/MariaDB

In simple terms, "granting all privileges on a database" in MySQL or MariaDB means giving a user full control over that specific database...


MAMP with MariaDB: Configuration Options

Stands for Macintosh Apache MySQL PHP.It's a local development environment that bundles Apache web server, MySQL database server...


MySQL 5 vs 6 vs MariaDB: Choosing the Right Database Server

The original open-source relational database management system (RDBMS).Widely used and considered the industry standard...


Beyond Backups: Alternative Approaches to MySQL to MariaDB Migration

There are two main approaches depending on your comfort level:Complete Uninstall/Install:Stop the MySQL server. Uninstall MySQL...


MySQL vs MariaDB vs Percona Server vs Drizzle: Choosing the Right Database

Here's an analogy: Imagine MySQL is a popular recipe for a cake.MariaDB would be someone taking that recipe and making a very similar cake...



mariadb

Understanding and Resolving MySQL Error 1153: Example Codes

Common Causes:Large Data Sets: When dealing with large datasets, such as importing a massive CSV file or executing complex queries involving many rows or columns


Speed Up Your Inserts: Multi-Row INSERT vs. Multiple Single INSERTs in MySQL/MariaDB

Reduced Overhead: Sending a single INSERT statement with multiple rows requires less network traffic compared to sending many individual INSERT statements


Understanding MySQL's SELECT * INTO OUTFILE LOCAL Statement

Functionality:This statement exports the results of a MySQL query to a plain text file on the server that's running the MySQL database


MariaDB for Commercial Use: Understanding Licensing and Support Options

Commercial License: Typically refers to a license where you pay a fee to use software for commercial purposes (selling a product that uses the software)


Fixing 'MariaDB Engine Won't Start' Error on Windows

MariaDB: An open-source relational database management system similar to MySQL.Windows: The operating system where MariaDB is installed