SlideShare a Scribd company logo
ALTER TABLE
Improvements in
MARIADB Server
Marko Mäkelä
Lead Developer InnoDB
MariaDB Corporation
• Retroactively named ALGORITHM=COPY in MySQL 5.6 and MariaDB 10.0
• Until MariaDB 10.2.13 (MDEV-11415), lots of unnecessary undo logging (and the
infamous “commit every 10,000 rows” hack to speed up crash recovery).
• Inserting into each index one record at a time (very inefficient).
• No sort buffer is being used inside InnoDB (other than the change buffer)
• Writes a large amount of redo log for the second copy of the table.
Generic ALTER TABLE in MariaDB
CREATE TABLE …; INSERT…SELECT; RENAME …; DROP TABLE …;
• “Fast index creation”: ADD [UNIQUE] INDEX, ADD PRIMARY KEY
• ALGORITHM=INPLACE starting with MySQL 5.6 and MariaDB 10.0
○ Misleading name “inplace”; some operations may rebuild the table!
■ (ADD|DROP) COLUMN, ADD PRIMARY KEY, CHANGE…[NOT] NULL
○ Some operations are instantaneous: rename column, change DEFAULT, …
○ Sometimes sloppily called “online” even when no concurrent DML is allowed
History of Native ALTER TABLE in InnoDB
Starting with InnoDB Plugin for MySQL 5.1
• InnoDB supports two classes of operations in online ALTER TABLE:
○ ADD [UNIQUE] INDEX: create indexes without copying the table
○ online table rebuild: ADD PRIMARY KEY or ADD, DROP, MODIFY columns
• InnoDB refuses ALTER ONLINE TABLE or ALTER TABLE…LOCK=NONE if:
○ A FULLTEXT or SPATIAL index is being created
○ The table needs to be rebuilt while FULLTEXT or SPATIAL index are present
ALTER ONLINE TABLE
Instant ALTER TABLE
in InnoDB
Instant ALTER TABLE Operations up to 10.3
● 10.0: Renaming columns, changing DEFAULT value
● 10.2: Extend VARCHAR in some cases: not VARCHAR(255) to VARCHAR(256)
● 10.3: ADD COLUMN (as the last column only), DROP CONSTRAINT
● 10.3.8 (MDEV-16330): Add or remove SYSTEM VERSIONING of a column
● 10.3.10 (MDEV-16328): change page_compression_level
● 10.3.x (MDEV-13301): Rename indexes (by DROP INDEX, ADD INDEX)
10.4: Instant Change of Collation or Charset
● Change the collation only, e.g., latin1_swedish_ci to latin1_german_ci
● Change ascii to almost anything, utf8mb3 to utf8mb4, ucs2 to utf16, …
○ Unless the collation is compatible, we must drop/add any indexes on the columns.
● The table may have to be copied in order to:
○ Change a CHAR column to variable-length encoding (e.g., ascii→utf8)
○ Change the maximum length from 128‥255 bytes to more than 255 bytes;
Example: Change CHAR(85) or VARCHAR(85) from utf8mb3 to utf8mb4
Change character set or collation without copying table
Sponsored by ServiceNow
Instant Column Extension for InnoDB Tables
● 10.2: Any extension of VARCHAR except from ≤255 bytes to >255 bytes
● 10.4: Any extension of VARCHAR from ≤127 bytes or ROW_RORMAT=REDUNDANT
● 10.x: Any extension of CHAR containing UTF-8 (or other variable-length charset), or
internally stored as variable-length
● These operations are compatible with old versions of MariaDB or MySQL.
No change to file formats or data; for any ROW_FORMAT
Sponsored by ServiceNow
● Instantly remove NOT NULL attribute, or extend any VARCHAR.
● Cancelled (MDEV-18627): Extend fixed-size columns (treat as variable-size)
○ TINYINT→SMALLINT→MEDIUMINT→INT→BIGINT; CHAR; VARCHAR→CHAR
● Uses 6+c or 6+2c bytes of record header, storing all c columns as variable-length.
○ Later formats (MySQL 5.0.3+): 5+⌈log2
(n+1)⌉+v to 5+⌈log2
(n+1)⌉+2v bytes (v≤c, n≤c);
using extra space for variable-length or NULLable columns only. Minimum is 5 bytes.
Instant ALTER TABLE Operations in 10.4
Specific to the original ROW_FORMAT=REDUNDANT
Sponsored by ServiceNow
Short History of InnoDB ROW_FORMAT
● Originally, InnoDB had a record header of 6+c or 6+2c bytes.
○ Basically, each column was encoded as variable-length and allowing NULL.
● MySQL 5.0.3 retroactively named the original format ROW_FORMAT=REDUNDANT
and introduced a new default ROW_FORMAT=COMPACT:
○ 5-byte fixed header, “is null” bitmap (except for NOT NULL columns), encode the lengths
of variable-length fields only (using 1 or 2 bytes per field)
○ CHAR(n) on UTF-8 is encoded like VARCHAR (n to 3n or 4n bytes)
○ Must copy table to remove NOT NULL or to extend fixed-length columns.
● InnoDB Plugin for MySQL 5.1 introduced DYNAMIC and (dead end) COMPRESSED:
○ Based on COMPACT, but not storing 768-byte prefix of off-page columns.
● innodb_default_row_format=DYNAMIC since MariaDB 10.2
File Format Changes
to Avoid Rebuild for
Instant ALTER TABLE
• MDEV-13134 introduced syntax to avoid “surprise rebuilds”:
ALGORITHM=(INSTANT|NOCOPY) and SET alter_algorithm=(instant|nocopy)
• MDEV-11369 introduced instant ADD COLUMN, limited to appending last
○ Both Alibaba and Tencent had developed something similar based on MySQL 5.6.
○ MariaDB supports also DEFAULT value expressions, with values stored in one place,
in a hidden metadata record at the start of the clustered index.
○ Does not support ROW_FORMAT=COMPRESSED.
ALTER TABLE Improvements in MariaDB 10.3
Example of Instant ADD COLUMN
CREATE TABLE t(id INT PRIMARY KEY, u INT UNIQUE) ENGINE=InnoDB;
INSERT INTO t(id,u) VALUES(1,1),(2,2),(3,3);
ALTER TABLE t ADD COLUMN
(d DATETIME DEFAULT current_timestamp(),
t TEXT CHARSET utf8 DEFAULT 'The quick brown fox',
p POINT NOT NULL DEFAULT ST_GeomFromText('POINT(0 0)'));
UPDATE t SET t=NULL WHERE id=3;
id u
1 1
2 2
3 3
Example of Instant ADD COLUMN
CREATE TABLE t(id INT PRIMARY KEY, u INT UNIQUE) ENGINE=InnoDB;
INSERT INTO t(id,u) VALUES(1,1),(2,2),(3,3);
ALTER TABLE t ADD COLUMN
(d DATETIME DEFAULT current_timestamp(),
t TEXT CHARSET utf8 DEFAULT 'The quick brown fox',
p POINT NOT NULL DEFAULT ST_GeomFromText('POINT(0 0)'));
UPDATE t SET t=NULL WHERE id=3;
id u d t p
2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
1 1 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
2 2 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
3 3 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
Example of Instant ADD COLUMN
CREATE TABLE t(id INT PRIMARY KEY, u INT UNIQUE) ENGINE=InnoDB;
INSERT INTO t(id,u) VALUES(1,1),(2,2),(3,3);
ALTER TABLE t ADD COLUMN
(d DATETIME DEFAULT current_timestamp(),
t TEXT CHARSET utf8 DEFAULT 'The quick brown fox',
p POINT NOT NULL DEFAULT ST_GeomFromText('POINT(0 0)'));
UPDATE t SET t=NULL WHERE id=3;
id u d t p
2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
1 1 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
2 2 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
3 3 2017-11-10 12:14:00 NULL POINT(0 0)
10.4: DROP, (ADD|MODIFY)…(FIRST|AFTER…)
● Keeps the user record format unchanged; adds metadata for column mapping.
○ Physically, do ADD COLUMN last in the clustered index records.
○ DROP COLUMN will leave garbage in old records; new records will write NULL, empty
strings, or dummy fixed-length values.
● The format of secondary indexes remains completely unchanged.
● Replacing PRIMARY KEY(a,b) with PRIMARY KEY(b,a) must copy the table.
Extends the 10.3 Instant ADD COLUMN metadata record with a BLOB
Sponsored by ServiceNow
Basic Usage of Instant ALTER TABLE
● By default, ALTER TABLE is instantaneous when possible
● Use the FORCE keyword for the old-fashioned table rebuild, with the old-fashioned
(additional) limitations with regard to FULLTEXT INDEX and SPATIAL INDEX
● FULLTEXT INDEX limits the ability to ADD, DROP columns or change their order
● To monitor the number of avoided table rebuilds via using the metadata record:
SELECT variable_value
FROM information_schema.global_status
WHERE variable_name = 'innodb_instant_alter_column';
● See also https://mariadb.com/resources/blog/instant-add-column-innodb
Record Changes for Instant ADD COLUMN
● An InnoDB table is a collection of indexes:
○ Clustered index (ordered by PRIMARY KEY or similar); index-organized table
○ Optional secondary indexes, pointing to clustered index keys
● In the clustered index leaf page records, we ADD COLUMN last:
○ (PRIMARY KEY, DB_TRX_ID, DB_ROLL_PTR, non-virtual columns, added columns)
● How to tell if added columns are present?
○ ROW_FORMAT=REDUNDANT explicitly stores the number of index fields.
○ ROW_FORMAT=COMPACT, ROW_FORMAT=DYNAMIC will require bigger changes:
■ Record header flag and optional field for “number of added columns”.
■ Must store the original number of fields or columns somewhere.
Page Changes for Instant ALTER TABLE
● Root page: FIL_PAGE_TYPE_INSTANT; PAGE_INSTANT stores the original
(smaller, or with DROP COLUMN, bigger) number of clustered index fields
● At the start of the clustered index, store a metadata record with
REC_INFO_MIN_REC_FLAG and the optional “added columns” header:
○ The number of fields must match the current table definition
○ Values of “added columns” are the values of “missing columns” in user records
○ For DROP COLUMN, some original metadata is stored in a metadata BLOB
Better ALTER TABLE
for Replication and
all Storage Engines
• Replication slave will only start after commit→huge lag (to be fixed in MDEV-11675)
• The online_log needs to be buffered (in memory or temporary files)
○ The size depends on the concurrent DML workload; hard to predict!
○ Written before commit; DML duplicate key errors make also ALTER TABLE fail
Watch out for MDEV-16329 Cross-Engine ALTER ONLINE TABLE
○ Keep engine-native for ADD [UNIQUE] INDEX or ALGORITHM=INSTANT
Problems with Online Table Rebuild
Why are tools like GH-OST still used instead of ALTER ONLINE TABLE?
MariaDB Server 10.5?
MDEV-515: InnoDB bulk insert into empty table or partition
○ Speeds up mysqldump and many INSERT, REPLACE, LOAD DATA
○ Works also for generic ALTER TABLE…ALGORITHM=COPY
○ For recovery, just write 1 undo log record “truncate on rollback”
○ Avoid or reduce redo logging; build indexes pre-sorted, page by page
■ Similar to CREATE INDEX in MariaDB 10.2+
Speeding up Bulk Operations in InnoDB
Needed for MDEV-16329 Cross-Engine ALTER ONLINE TABLE
MariaDB Server 10.5?
Theoretical Limits of
Avoiding Copying in
ALTER TABLE
Format Tagging for Lazy Conversions
● Format changes can be instantaneous if they relax constraints:
○ Change virtually anything to utf8 or utf16; e.g.: _latin1 0xe4 ≙ _utf8 0xc3a4
○ Change INT UNSIGNED to BIGINT (unsigned to wider signed integer)
● These could be implemented with a per-record or per-page “format version tag” and
by converting records to the newest version whenever the data is being read.
● Affected secondary indexes must be rebuilt.
Avoid rebuilding or copying the table when changing data encodings
Speculation
File Format Changes for Format Tagging
● “Format version number” that points to something in the hidden metadata record?
● A prototype with “dual-format” clustered index leaf pages was implemented and
rejected due to the ROW_FORMAT=REDUNDANT storage overhead
● For any ROW_FORMAT, we need additional metadata to indicate how to convert data
when reading or searching: e.g., latin1 to utf8, INT to BIGINT
● Do we want this? Could add significant memory and time overhead to DML!
User data records (or pages) must indicate their physical format
Speculation
ALGORITHM=NOCOPY with Validation (1/2)
● Perform a locking table scan to validate the data.
○ Example: i BIGINT NULL to INT UNSIGNED NOT NULL is OK if i>=0
○ ALTER ONLINE TABLE actually conflicts with ALGORITHM=NOCOPY in this case!
○ ALTER IGNORE TABLE would involve UPDATE of offending data.
● Affected secondary indexes must be rebuilt if the physical format changes
○ ADD CONSTRAINT … (CHECK|FOREIGN KEY) does not change format!
Avoid copying the table even if the data could be incompatible
Speculation
ALGORITHM=NOCOPY with Validation (2/2)
1. Check constraints for each row, e.g., MODIFY i INT UNSIGNED:
○ ALTER IGNORE would UPDATE offending data, e.g.: SET i=NULL WHERE i<0
2. DROP INDEX and ADD INDEX of affected indexes, or user-specified ones
3. Any additional operations that are part of the ALTER (say, instant DROP COLUMN)
4. Update the data dictionary
The Lifetime of an ALTER TABLE Transaction
Speculation
Summary
● MariaDB 10.3 and 10.4 changed the InnoDB data format to allow instantaneous
(ADD|MODIFY) COLUMN…(FIRST|AFTER…), DROP. You can still FORCE a rebuild.
● MariaDB 10.4 supports instant ALTER TABLE whenever it is technically possible
without changing the storage format further.
● Future MariaDB versions might support instant ALTER TABLE or avoid copying
whenever technically possible. The current metadata format is extensible.
● Use ALGORITHM=INSTANT or ALGORITHM=NOCOPY (or SET alter_algorithm)
to get errors instead of unexpected DoS via excessive I/O.

More Related Content

What's hot (19)

PDF
Using histograms to get better performance
Sergey Petrunya
 
PDF
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
PgDay.Seoul
 
PDF
MariaDB Temporal Tables
Federico Razzoli
 
PDF
MariaDB ColumnStore
MariaDB plc
 
PDF
Юра Гуляев. Oracle tables
Aleksandr Motsjonov
 
PDF
ANALYZE for Statements - MariaDB's hidden gem
Sergey Petrunya
 
PDF
MariaDB Temporal Tables
Federico Razzoli
 
PDF
Mysqlconf2013 mariadb-cassandra-interoperability
Sergey Petrunya
 
PPTX
Faster transactions & analytics with the new SQL2016 In-memory technologies
Henk van der Valk
 
PDF
Performance tuning ColumnStore
MariaDB plc
 
PDF
When and Why to Use MariaDB: New Features in 10.0 to 10.5
Ian Gilfillan
 
PPTX
R Get Started II
Sankhya_Analytics
 
PDF
Practical Recipes for Daily DBA Activities using DB2 9 and 10 for z/OS
Cuneyt Goksu
 
PPTX
R Get Started I
Sankhya_Analytics
 
PDF
Query Optimizer in MariaDB 10.4
Sergey Petrunya
 
PDF
Optimizer Trace Walkthrough
Sergey Petrunya
 
PDF
Oracle Database InMemory
Jorge Barba
 
PDF
New features-in-mariadb-and-mysql-optimizers
Sergey Petrunya
 
PDF
PGConf.ASIA 2017 Logical Replication Internals (English)
Noriyoshi Shinoda
 
Using histograms to get better performance
Sergey Petrunya
 
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
PgDay.Seoul
 
MariaDB Temporal Tables
Federico Razzoli
 
MariaDB ColumnStore
MariaDB plc
 
Юра Гуляев. Oracle tables
Aleksandr Motsjonov
 
ANALYZE for Statements - MariaDB's hidden gem
Sergey Petrunya
 
MariaDB Temporal Tables
Federico Razzoli
 
Mysqlconf2013 mariadb-cassandra-interoperability
Sergey Petrunya
 
Faster transactions & analytics with the new SQL2016 In-memory technologies
Henk van der Valk
 
Performance tuning ColumnStore
MariaDB plc
 
When and Why to Use MariaDB: New Features in 10.0 to 10.5
Ian Gilfillan
 
R Get Started II
Sankhya_Analytics
 
Practical Recipes for Daily DBA Activities using DB2 9 and 10 for z/OS
Cuneyt Goksu
 
R Get Started I
Sankhya_Analytics
 
Query Optimizer in MariaDB 10.4
Sergey Petrunya
 
Optimizer Trace Walkthrough
Sergey Petrunya
 
Oracle Database InMemory
Jorge Barba
 
New features-in-mariadb-and-mysql-optimizers
Sergey Petrunya
 
PGConf.ASIA 2017 Logical Replication Internals (English)
Noriyoshi Shinoda
 

Similar to ALTER TABLE Improvements in MariaDB Server (20)

PDF
Instant add column for inno db in mariadb 10.3+ (fosdem 2018, second draft)
Valerii Kravchuk
 
PDF
Mysql alter-command
beben benzy
 
PDF
15 MySQL Basics #burningkeyboards
Denis Ristic
 
PDF
Introducing Spirit - Online Schema Change
Morgan Tocker
 
PDF
Developers' mDay 2017. - Bogdan Kecman Oracle
mCloud
 
PDF
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
mCloud
 
ODP
Inno db 5_7_features
Tinku Ajit
 
PDF
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
Geir Høydalsvik
 
PDF
MySQL 优化
YUCHENG HU
 
PPTX
Boosting MySQL (for starters)
Jose Luis Martínez
 
PPTX
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...
CAPSiDE
 
PPT
My SQL
Karan Kashyap
 
PPT
Star schema my sql
deathsubte
 
PDF
MySQL optimisation Percona LeMug.fr
cyruss666
 
PDF
MariaDB for developers
Colin Charles
 
PDF
Introducción rápida a SQL
Carlos Hernando
 
PDF
MariaDB 10.11 key features overview for DBAs
Federico Razzoli
 
PPTX
What's new in MariaDB TX 3.0
MariaDB plc
 
PDF
What's New MySQL 8.0?
OracleMySQL
 
PDF
My sql 5.7-upcoming-changes-v2
Morgan Tocker
 
Instant add column for inno db in mariadb 10.3+ (fosdem 2018, second draft)
Valerii Kravchuk
 
Mysql alter-command
beben benzy
 
15 MySQL Basics #burningkeyboards
Denis Ristic
 
Introducing Spirit - Online Schema Change
Morgan Tocker
 
Developers' mDay 2017. - Bogdan Kecman Oracle
mCloud
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
mCloud
 
Inno db 5_7_features
Tinku Ajit
 
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
Geir Høydalsvik
 
MySQL 优化
YUCHENG HU
 
Boosting MySQL (for starters)
Jose Luis Martínez
 
"MySQL Boosting - DB Best Practices & Optimization" by José Luis Martínez - C...
CAPSiDE
 
Star schema my sql
deathsubte
 
MySQL optimisation Percona LeMug.fr
cyruss666
 
MariaDB for developers
Colin Charles
 
Introducción rápida a SQL
Carlos Hernando
 
MariaDB 10.11 key features overview for DBAs
Federico Razzoli
 
What's new in MariaDB TX 3.0
MariaDB plc
 
What's New MySQL 8.0?
OracleMySQL
 
My sql 5.7-upcoming-changes-v2
Morgan Tocker
 
Ad

More from MariaDB plc (20)

PDF
MariaDB Berlin Roadshow Slides - 8 April 2025
MariaDB plc
 
PDF
MariaDB München Roadshow - 24 September, 2024
MariaDB plc
 
PDF
MariaDB Paris Roadshow - 19 September 2024
MariaDB plc
 
PDF
MariaDB Amsterdam Roadshow: 19 September, 2024
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - Newpharma
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - Cloud
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - Performance Optimization
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - MaxScale
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - novadys presentation
MariaDB plc
 
PDF
MariaDB Paris Workshop 2023 - DARVA presentation
MariaDB plc
 
PDF
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB plc
 
PDF
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB plc
 
PDF
Einführung : MariaDB Tech und Business Update Hamburg 2023
MariaDB plc
 
PDF
Hochverfügbarkeitslösungen mit MariaDB
MariaDB plc
 
PDF
Die Neuheiten in MariaDB Enterprise Server
MariaDB plc
 
PDF
Global Data Replication with Galera for Ansell Guardian®
MariaDB plc
 
PDF
Introducing workload analysis
MariaDB plc
 
PDF
Under the hood: SkySQL monitoring
MariaDB plc
 
MariaDB Berlin Roadshow Slides - 8 April 2025
MariaDB plc
 
MariaDB München Roadshow - 24 September, 2024
MariaDB plc
 
MariaDB Paris Roadshow - 19 September 2024
MariaDB plc
 
MariaDB Amsterdam Roadshow: 19 September, 2024
MariaDB plc
 
MariaDB Paris Workshop 2023 - MaxScale 23.02.x
MariaDB plc
 
MariaDB Paris Workshop 2023 - Newpharma
MariaDB plc
 
MariaDB Paris Workshop 2023 - Cloud
MariaDB plc
 
MariaDB Paris Workshop 2023 - MariaDB Enterprise
MariaDB plc
 
MariaDB Paris Workshop 2023 - Performance Optimization
MariaDB plc
 
MariaDB Paris Workshop 2023 - MaxScale
MariaDB plc
 
MariaDB Paris Workshop 2023 - novadys presentation
MariaDB plc
 
MariaDB Paris Workshop 2023 - DARVA presentation
MariaDB plc
 
MariaDB Tech und Business Update Hamburg 2023 - MariaDB Enterprise Server
MariaDB plc
 
MariaDB SkySQL Autonome Skalierung, Observability, Cloud-Backup
MariaDB plc
 
Einführung : MariaDB Tech und Business Update Hamburg 2023
MariaDB plc
 
Hochverfügbarkeitslösungen mit MariaDB
MariaDB plc
 
Die Neuheiten in MariaDB Enterprise Server
MariaDB plc
 
Global Data Replication with Galera for Ansell Guardian®
MariaDB plc
 
Introducing workload analysis
MariaDB plc
 
Under the hood: SkySQL monitoring
MariaDB plc
 
Ad

Recently uploaded (20)

PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Import Data Form Excel to Tally Services
Tally xperts
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Tally software_Introduction_Presentation
AditiBansal54083
 

ALTER TABLE Improvements in MariaDB Server

  • 1. ALTER TABLE Improvements in MARIADB Server Marko Mäkelä Lead Developer InnoDB MariaDB Corporation
  • 2. • Retroactively named ALGORITHM=COPY in MySQL 5.6 and MariaDB 10.0 • Until MariaDB 10.2.13 (MDEV-11415), lots of unnecessary undo logging (and the infamous “commit every 10,000 rows” hack to speed up crash recovery). • Inserting into each index one record at a time (very inefficient). • No sort buffer is being used inside InnoDB (other than the change buffer) • Writes a large amount of redo log for the second copy of the table. Generic ALTER TABLE in MariaDB CREATE TABLE …; INSERT…SELECT; RENAME …; DROP TABLE …;
  • 3. • “Fast index creation”: ADD [UNIQUE] INDEX, ADD PRIMARY KEY • ALGORITHM=INPLACE starting with MySQL 5.6 and MariaDB 10.0 ○ Misleading name “inplace”; some operations may rebuild the table! ■ (ADD|DROP) COLUMN, ADD PRIMARY KEY, CHANGE…[NOT] NULL ○ Some operations are instantaneous: rename column, change DEFAULT, … ○ Sometimes sloppily called “online” even when no concurrent DML is allowed History of Native ALTER TABLE in InnoDB Starting with InnoDB Plugin for MySQL 5.1
  • 4. • InnoDB supports two classes of operations in online ALTER TABLE: ○ ADD [UNIQUE] INDEX: create indexes without copying the table ○ online table rebuild: ADD PRIMARY KEY or ADD, DROP, MODIFY columns • InnoDB refuses ALTER ONLINE TABLE or ALTER TABLE…LOCK=NONE if: ○ A FULLTEXT or SPATIAL index is being created ○ The table needs to be rebuilt while FULLTEXT or SPATIAL index are present ALTER ONLINE TABLE
  • 6. Instant ALTER TABLE Operations up to 10.3 ● 10.0: Renaming columns, changing DEFAULT value ● 10.2: Extend VARCHAR in some cases: not VARCHAR(255) to VARCHAR(256) ● 10.3: ADD COLUMN (as the last column only), DROP CONSTRAINT ● 10.3.8 (MDEV-16330): Add or remove SYSTEM VERSIONING of a column ● 10.3.10 (MDEV-16328): change page_compression_level ● 10.3.x (MDEV-13301): Rename indexes (by DROP INDEX, ADD INDEX)
  • 7. 10.4: Instant Change of Collation or Charset ● Change the collation only, e.g., latin1_swedish_ci to latin1_german_ci ● Change ascii to almost anything, utf8mb3 to utf8mb4, ucs2 to utf16, … ○ Unless the collation is compatible, we must drop/add any indexes on the columns. ● The table may have to be copied in order to: ○ Change a CHAR column to variable-length encoding (e.g., ascii→utf8) ○ Change the maximum length from 128‥255 bytes to more than 255 bytes; Example: Change CHAR(85) or VARCHAR(85) from utf8mb3 to utf8mb4 Change character set or collation without copying table Sponsored by ServiceNow
  • 8. Instant Column Extension for InnoDB Tables ● 10.2: Any extension of VARCHAR except from ≤255 bytes to >255 bytes ● 10.4: Any extension of VARCHAR from ≤127 bytes or ROW_RORMAT=REDUNDANT ● 10.x: Any extension of CHAR containing UTF-8 (or other variable-length charset), or internally stored as variable-length ● These operations are compatible with old versions of MariaDB or MySQL. No change to file formats or data; for any ROW_FORMAT Sponsored by ServiceNow
  • 9. ● Instantly remove NOT NULL attribute, or extend any VARCHAR. ● Cancelled (MDEV-18627): Extend fixed-size columns (treat as variable-size) ○ TINYINT→SMALLINT→MEDIUMINT→INT→BIGINT; CHAR; VARCHAR→CHAR ● Uses 6+c or 6+2c bytes of record header, storing all c columns as variable-length. ○ Later formats (MySQL 5.0.3+): 5+⌈log2 (n+1)⌉+v to 5+⌈log2 (n+1)⌉+2v bytes (v≤c, n≤c); using extra space for variable-length or NULLable columns only. Minimum is 5 bytes. Instant ALTER TABLE Operations in 10.4 Specific to the original ROW_FORMAT=REDUNDANT Sponsored by ServiceNow
  • 10. Short History of InnoDB ROW_FORMAT ● Originally, InnoDB had a record header of 6+c or 6+2c bytes. ○ Basically, each column was encoded as variable-length and allowing NULL. ● MySQL 5.0.3 retroactively named the original format ROW_FORMAT=REDUNDANT and introduced a new default ROW_FORMAT=COMPACT: ○ 5-byte fixed header, “is null” bitmap (except for NOT NULL columns), encode the lengths of variable-length fields only (using 1 or 2 bytes per field) ○ CHAR(n) on UTF-8 is encoded like VARCHAR (n to 3n or 4n bytes) ○ Must copy table to remove NOT NULL or to extend fixed-length columns. ● InnoDB Plugin for MySQL 5.1 introduced DYNAMIC and (dead end) COMPRESSED: ○ Based on COMPACT, but not storing 768-byte prefix of off-page columns. ● innodb_default_row_format=DYNAMIC since MariaDB 10.2
  • 11. File Format Changes to Avoid Rebuild for Instant ALTER TABLE
  • 12. • MDEV-13134 introduced syntax to avoid “surprise rebuilds”: ALGORITHM=(INSTANT|NOCOPY) and SET alter_algorithm=(instant|nocopy) • MDEV-11369 introduced instant ADD COLUMN, limited to appending last ○ Both Alibaba and Tencent had developed something similar based on MySQL 5.6. ○ MariaDB supports also DEFAULT value expressions, with values stored in one place, in a hidden metadata record at the start of the clustered index. ○ Does not support ROW_FORMAT=COMPRESSED. ALTER TABLE Improvements in MariaDB 10.3
  • 13. Example of Instant ADD COLUMN CREATE TABLE t(id INT PRIMARY KEY, u INT UNIQUE) ENGINE=InnoDB; INSERT INTO t(id,u) VALUES(1,1),(2,2),(3,3); ALTER TABLE t ADD COLUMN (d DATETIME DEFAULT current_timestamp(), t TEXT CHARSET utf8 DEFAULT 'The quick brown fox', p POINT NOT NULL DEFAULT ST_GeomFromText('POINT(0 0)')); UPDATE t SET t=NULL WHERE id=3; id u 1 1 2 2 3 3
  • 14. Example of Instant ADD COLUMN CREATE TABLE t(id INT PRIMARY KEY, u INT UNIQUE) ENGINE=InnoDB; INSERT INTO t(id,u) VALUES(1,1),(2,2),(3,3); ALTER TABLE t ADD COLUMN (d DATETIME DEFAULT current_timestamp(), t TEXT CHARSET utf8 DEFAULT 'The quick brown fox', p POINT NOT NULL DEFAULT ST_GeomFromText('POINT(0 0)')); UPDATE t SET t=NULL WHERE id=3; id u d t p 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0) 1 1 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0) 2 2 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0) 3 3 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0)
  • 15. Example of Instant ADD COLUMN CREATE TABLE t(id INT PRIMARY KEY, u INT UNIQUE) ENGINE=InnoDB; INSERT INTO t(id,u) VALUES(1,1),(2,2),(3,3); ALTER TABLE t ADD COLUMN (d DATETIME DEFAULT current_timestamp(), t TEXT CHARSET utf8 DEFAULT 'The quick brown fox', p POINT NOT NULL DEFAULT ST_GeomFromText('POINT(0 0)')); UPDATE t SET t=NULL WHERE id=3; id u d t p 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0) 1 1 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0) 2 2 2017-11-10 12:14:00 'The quick brown fox' POINT(0 0) 3 3 2017-11-10 12:14:00 NULL POINT(0 0)
  • 16. 10.4: DROP, (ADD|MODIFY)…(FIRST|AFTER…) ● Keeps the user record format unchanged; adds metadata for column mapping. ○ Physically, do ADD COLUMN last in the clustered index records. ○ DROP COLUMN will leave garbage in old records; new records will write NULL, empty strings, or dummy fixed-length values. ● The format of secondary indexes remains completely unchanged. ● Replacing PRIMARY KEY(a,b) with PRIMARY KEY(b,a) must copy the table. Extends the 10.3 Instant ADD COLUMN metadata record with a BLOB Sponsored by ServiceNow
  • 17. Basic Usage of Instant ALTER TABLE ● By default, ALTER TABLE is instantaneous when possible ● Use the FORCE keyword for the old-fashioned table rebuild, with the old-fashioned (additional) limitations with regard to FULLTEXT INDEX and SPATIAL INDEX ● FULLTEXT INDEX limits the ability to ADD, DROP columns or change their order ● To monitor the number of avoided table rebuilds via using the metadata record: SELECT variable_value FROM information_schema.global_status WHERE variable_name = 'innodb_instant_alter_column'; ● See also https://mariadb.com/resources/blog/instant-add-column-innodb
  • 18. Record Changes for Instant ADD COLUMN ● An InnoDB table is a collection of indexes: ○ Clustered index (ordered by PRIMARY KEY or similar); index-organized table ○ Optional secondary indexes, pointing to clustered index keys ● In the clustered index leaf page records, we ADD COLUMN last: ○ (PRIMARY KEY, DB_TRX_ID, DB_ROLL_PTR, non-virtual columns, added columns) ● How to tell if added columns are present? ○ ROW_FORMAT=REDUNDANT explicitly stores the number of index fields. ○ ROW_FORMAT=COMPACT, ROW_FORMAT=DYNAMIC will require bigger changes: ■ Record header flag and optional field for “number of added columns”. ■ Must store the original number of fields or columns somewhere.
  • 19. Page Changes for Instant ALTER TABLE ● Root page: FIL_PAGE_TYPE_INSTANT; PAGE_INSTANT stores the original (smaller, or with DROP COLUMN, bigger) number of clustered index fields ● At the start of the clustered index, store a metadata record with REC_INFO_MIN_REC_FLAG and the optional “added columns” header: ○ The number of fields must match the current table definition ○ Values of “added columns” are the values of “missing columns” in user records ○ For DROP COLUMN, some original metadata is stored in a metadata BLOB
  • 20. Better ALTER TABLE for Replication and all Storage Engines
  • 21. • Replication slave will only start after commit→huge lag (to be fixed in MDEV-11675) • The online_log needs to be buffered (in memory or temporary files) ○ The size depends on the concurrent DML workload; hard to predict! ○ Written before commit; DML duplicate key errors make also ALTER TABLE fail Watch out for MDEV-16329 Cross-Engine ALTER ONLINE TABLE ○ Keep engine-native for ADD [UNIQUE] INDEX or ALGORITHM=INSTANT Problems with Online Table Rebuild Why are tools like GH-OST still used instead of ALTER ONLINE TABLE? MariaDB Server 10.5?
  • 22. MDEV-515: InnoDB bulk insert into empty table or partition ○ Speeds up mysqldump and many INSERT, REPLACE, LOAD DATA ○ Works also for generic ALTER TABLE…ALGORITHM=COPY ○ For recovery, just write 1 undo log record “truncate on rollback” ○ Avoid or reduce redo logging; build indexes pre-sorted, page by page ■ Similar to CREATE INDEX in MariaDB 10.2+ Speeding up Bulk Operations in InnoDB Needed for MDEV-16329 Cross-Engine ALTER ONLINE TABLE MariaDB Server 10.5?
  • 23. Theoretical Limits of Avoiding Copying in ALTER TABLE
  • 24. Format Tagging for Lazy Conversions ● Format changes can be instantaneous if they relax constraints: ○ Change virtually anything to utf8 or utf16; e.g.: _latin1 0xe4 ≙ _utf8 0xc3a4 ○ Change INT UNSIGNED to BIGINT (unsigned to wider signed integer) ● These could be implemented with a per-record or per-page “format version tag” and by converting records to the newest version whenever the data is being read. ● Affected secondary indexes must be rebuilt. Avoid rebuilding or copying the table when changing data encodings Speculation
  • 25. File Format Changes for Format Tagging ● “Format version number” that points to something in the hidden metadata record? ● A prototype with “dual-format” clustered index leaf pages was implemented and rejected due to the ROW_FORMAT=REDUNDANT storage overhead ● For any ROW_FORMAT, we need additional metadata to indicate how to convert data when reading or searching: e.g., latin1 to utf8, INT to BIGINT ● Do we want this? Could add significant memory and time overhead to DML! User data records (or pages) must indicate their physical format Speculation
  • 26. ALGORITHM=NOCOPY with Validation (1/2) ● Perform a locking table scan to validate the data. ○ Example: i BIGINT NULL to INT UNSIGNED NOT NULL is OK if i>=0 ○ ALTER ONLINE TABLE actually conflicts with ALGORITHM=NOCOPY in this case! ○ ALTER IGNORE TABLE would involve UPDATE of offending data. ● Affected secondary indexes must be rebuilt if the physical format changes ○ ADD CONSTRAINT … (CHECK|FOREIGN KEY) does not change format! Avoid copying the table even if the data could be incompatible Speculation
  • 27. ALGORITHM=NOCOPY with Validation (2/2) 1. Check constraints for each row, e.g., MODIFY i INT UNSIGNED: ○ ALTER IGNORE would UPDATE offending data, e.g.: SET i=NULL WHERE i<0 2. DROP INDEX and ADD INDEX of affected indexes, or user-specified ones 3. Any additional operations that are part of the ALTER (say, instant DROP COLUMN) 4. Update the data dictionary The Lifetime of an ALTER TABLE Transaction Speculation
  • 28. Summary ● MariaDB 10.3 and 10.4 changed the InnoDB data format to allow instantaneous (ADD|MODIFY) COLUMN…(FIRST|AFTER…), DROP. You can still FORCE a rebuild. ● MariaDB 10.4 supports instant ALTER TABLE whenever it is technically possible without changing the storage format further. ● Future MariaDB versions might support instant ALTER TABLE or avoid copying whenever technically possible. The current metadata format is extensible. ● Use ALGORITHM=INSTANT or ALGORITHM=NOCOPY (or SET alter_algorithm) to get errors instead of unexpected DoS via excessive I/O.