SlideShare a Scribd company logo
Instant ADD COLUMN for
InnoDB in MariaDB 10.3+
On the way to instant ALTER TABLE for failure-free record
format changes
Valerii Kravchuk, Principal Support Engineer, MariaDB
vkravchuk@gmail.com
1
www.percona.com
Who am I?
Valerii (aka Valeriy) Kravchuk:
● MySQL Support Engineer in MySQL AB, Sun and Oracle, 2005 - 2012
● Principal Support Engineer in Percona, 2012 - 2016
● Principal Support Engineer in MariaDB Corporation since March 2016
● http://mysqlentomologist.blogspot.com - my blog about MySQL (a lot about
MySQL bugs, but some HowTos as well)
● https://www.facebook.com/valerii.kravchuk - my Facebook page, a lot about
MySQL (mostly bugs, rants and links to blog posts…)
● http://bugs.mysql.com - it used to be my personal playground
● @mysqlbugs - #bugoftheday on Twitter
● I like FOSDEM, see slides from my previous talks:
○ http://www.slideshare.net/valeriikravchuk1/fosdem2015-gdb-tips-and-tricks-for-my-sql-db-as
○ http://www.slideshare.net/ValeriyKravchuk/more-on-gdb-for-my-sql-db-as-fosdem-2016
○ https://www.slideshare.net/ValeriyKravchuk/applying-profilers-to-my-sql-fosdem-2017
2
www.percona.com
What is this session about?
● History of ALTER TABLE improvements in MySQL
● Problems with current “online” table rebuilds
● How instant ADD COLUMN for InnoDB appeared in
MariaDB
● Basic usage of instant ADD COLUMN, limitations
● Page changes for instant ADD COLUMN
● Some “benchmarks” (not sure if I have enough time…)
● Next steps on the way to instant ALTER TABLE for
failure-free InnoDB record format changes that may
happen in MariaDB 10.4+ (or not :)
3
www.percona.com
History of ALTER TABLE in MySQL/MariaDB
● The old way (also known as ALGORITHM=COPY starting with MySQL 5.6)
○ CREATE TABLE ...; INSERT … SELECT; RENAME TABLE ...; DROP TABLE ...;
○ Lots of unnecessary undo and redo logging in InnoDB ("bulk insert" would help)
● "Fast index creation" in InnoDB Plugin for MySQL 5.1 (built-in 5.5 InnoDB)
○ Supports ADD INDEX, ADD UNIQUE INDEX, ADD PRIMARY KEY (?)
● ALGORITHM=INPLACE starting with MySQL 5.6
○ Misleading name; some operations may rebuild the table
■ ADD/DROP COLUMN, ADD PRIMARY KEY, CHANGE ...[NOT] NULL
○ Some operations are instant: rename column, change DEFAULT value, …
■ Should have ALGORITHM=(INSTANT|NOCOPY) to avoid surprises (MDEV-13134)
○ Somewhat wrongly called "online" DDL:
■ Changes to data in table are still prevented for some time in the process. Bug #84004!
■ It still causes slaves lag (Bug #73196)
■ We all know it should be “more online”, see Bug #77097, Bug #68498, Bug #72109,
Bug #83557 etc
○ Online (LOCK=NONE) is sometimes refused:
■ ALTER TABLE … ADD (FULLTEXT|SPATIAL) INDEX, ALGORITHM=INPLACE;
■ Any table rebuild operation when FULLTEXT or SPATIAL indexes are present (see
Bug #81819)
4
www.percona.com
Problems with Online Table Rebuild
● MySQL 5.6+ includes “online” ALTER TABLE
([ADD|DROP] COLUMN etc.), with LOCK=NONE. Why
are tools like gh-ost or pt-osc still used?
○ Replication ignores LOCK=NONE: Slaves will only continue after
commit → huge lag
○ The online log needs to be buffered (in memory or temporary files)
■ The size depends on the concurrent DML workload; hard to predict!
○ The whole table (including all indexes) will have to be copied
■ MySQL 5.7 included some performance improvements to this, but huge I/O remains
○ Theoretically, do we really have to rebuild?
■ Only when introducing stricter constraints (shorter columns, add NOT NULL)
■ Even that could be done by validating the table and editing metadata
■ Only ADD [UNIQUE|PRIMARY|SPATIAL|FULLTEXT] KEY really require writes
5
www.percona.com
History of Instant ADD COLUMN for InnoDB
● Both Alibaba and Tencent have instant ADD in their MySQL 5.6 forks
○ Does not work with old data files; requires a new ROW_FORMAT
● MariaDB wants it to work on old (possibly large) files
○ Vin Chen from Tencent Game DBA Team wrote a prototype that adds
an optional record header to identify "afterwards added columns"
○ The ADD … DEFAULT values are stored in one place
○ Data dictionary only reflects the latest table definition, including the
latest DEFAULT
● Marko Mäkelä rewrote the prototype for MariaDB 10.3.2 (see MDEV-11369)
○ Store a 'default row' at the start of the table (we want to remove SYS_*
tables one day)
○ Support all but ROW_FORMAT=COMPRESSED
○ Crash-safe DDL (a new undo record type); simpler DML rollback;
"compression"
○ ensured that online table rebuild (e.g. DROP COLUMN) still works
6
www.percona.com
Basic Usage of Instant ADD COLUMN
● By default, ALTER TABLE … ADD COLUMN is instantaneous
○ Limitation: No hidden FTS_DOC_ID column (for FULLTEXT INDEX)
must exist
● Use the FORCE keyword for the old-fashioned ADD COLUMN, with the
old-fashioned (additional) limitations:
○ ALGORITHM=INPLACE will not work if multiple FULLTEXT indexes
exist
○ LOCK=NONE will not work if FULLTEXT or SPATIAL index exist
● To monitor the number of avoided table rebuilds:
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
7
● No limitations on data types for added columns or
DEFAULT expression complexity:
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;
SELECT * FROM t;
● No backward compatibility until rebuilt with FORCE
(ToDo: check this). See MDEV-13562 also.
www.percona.com
Example of Instant ADD COLUMN
8
www.percona.com
Page Changes for Instant ADD COLUMN
● Clustered index root page changes:
○ FIL_PAGE_TYPE_INSTANT indicates that instant operation was used
○ PAGE_INSTANT stores the original (smaller) number of clustered index
fields
● Change the leftmost clustered index leaf page:
○ After the infimum, store a "default" record with REC_INFO_MIN_REC_FLAG:
■ Must have the optional "added fields" header
■ The number of fields must match the current table definition
■ Values of "added fields" are the values of "missing fields"
● Clustered index contents from the previous example:
○ (default,id,u,d=2017-11-10 12:14:00,t='The quick brown fox',p=POINT(0 0)),
○ (1,1), (2,2), (3,3,2017-11-10 12:14:00, NULL)
○ We omit trailing fields that are equal to the fields in the "default" record
9
www.percona.com
MariaDB 10.4+: ADD… [FIRST|AFTER], DROP...
● Keep the user record format unchanged
○ Physically, keep doing ADD COLUMN last in the
clustered index records
○ DROP COLUMN will leave garbage in the records
○ Changing column order physically becomes a no-op
○ ADD COLUMN will be possible even if hidden
FTS_DOC_ID exists
● In the "default" record, store a mapping of table columns to
index fields, pass it somehow when needed
10
www.percona.com
MariaDB 10.4+: Instant CHANGE COLUMN?
● MySQL 5.7/MariaDB 10.2: Extend VARCHAR maximum size
○ Only if the physical format allows; not VARCHAR(255) to VARCHAR(256)
● We need something in the user data records to indicate physical format
○ "Format version number" that points to something in the "default" record?
● Format changes can only be instantaneous if they relax constraints:
○ Example: CHAR(1) to CHAR(2), INT to BIGINT or NOT NULL to NULL
○ Less likely: Changing POINT to GEOMETRY, changing latin1 to utf8
● Failure is an option, if we perform table scan to validate the data:
○ Example: Changing BIGINT NULL to INT UNSIGNED NOT NULL
● Affected secondary indexes must be rebuilt if the physical format changes
○ Still much faster than rebuilding the entire table; can be done online
● Follow MDEV-11424, “Instant ALTER TABLE of failure-free record format
changes”, if interested...
11
www.percona.com
Set of Statements to Do Lame “Benchmark”
See test_instant_alter.sql for more details:
create table t(id int auto_increment primary key, c1 int);
insert into t(c1) values (0);
-- repeat 19 times
insert into t(c1) select rand()*1000 from t; -- 0
select count(*) from t; -- 1
...
alter table t add column c2 char(200) default 'a'; -- 2
...
update t set c2 = 'b'; -- 3
update t set c2 = 'c'; -- 4
update t set c2 = 'd'; -- !!!
...
alter table t force; -- 5
update t set c2 = 'e'; -- 6
update t set c2 = 'f'; -- 7
12
It may be interesting to compare
times to execute 2 and 5, as well
as 3 and 4 to 6 and 7, also for
different engines (like MyRocks)
and versions (PS 5.7, 10.2, 10.3,
and, surely MySQL 8.0.4)
www.percona.com
Example of Instant ADD COLUMN for Benchmark
create table t(id int auto_increment primary key, c1 int);
insert into t(c1) values (0);
insert into t(c1) select rand()*1000 from t;
… continue that way until it’s big enough
MariaDB [test]> insert into t(c1) select rand()*1000 from t;
Query OK, 262144 rows affected ( 4.141 sec)
Records: 262144 Duplicates: 0 Warnings: 0
MariaDB [test]> alter table t add column c2 char(200) default
repeat('a',200);
Query OK, 0 rows affected ( 0.039 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test]> update t set c2 = 'b';
Query OK, 524288 rows affected ( 55.682 sec)
Rows matched: 524288 Changed: 524288 Warnings: 0
MariaDB [test]> update t set c2 = 'c';
Query OK, 524288 rows affected ( 31.649 sec)
Rows matched: 524288 Changed: 524288 Warnings: 0
13
www.percona.com
Time to Run ALTER … ADD COLUMN (2)
● On my QuadCore Fedora 27 box, --no-defaults:
14
www.percona.com
Time to Run First UPDATE After ALTER (3)
● On my QuadCore Fedora 27 box, --no-defaults:
15
www.percona.com
Time to Run Entire “Benchmark” Script
● On my QuadCore Fedora 27 box, --no-defaults:
16
www.percona.com
Thanks and Links
● Thanks to Marko Mäkelä for his work, hints and slides on
this topic
● Thanks to LeFred and Belcona team for this event!
● Check MDEV-11369 for the details of original task in
MariaDB
● Check original blog post about the feature
● Check MDEV-11424 for further plans in MariaDB 10.4+ and
progress on them
● My blog post on current state of online DDL in MySQL 5.7
● Some test and raw benchmark results are shared here 17
www.percona.com
Thank you!
Questions and Answers?
Please, report bugs at:
https://bugs.mysql.com
https://jira.mariadb.org
https://jira.percona.com
18

More Related Content

PDF
Tracing and profiling my sql (percona live europe 2019) draft_1
PDF
FOSDEM 2015: gdb tips and tricks for MySQL DBAs
PDF
More on gdb for my sql db as (fosdem 2016)
PDF
Gdb basics for my sql db as (percona live europe 2019)
PDF
Applying profilers to my sql (fosdem 2017)
PDF
Gdb basics for my sql db as (openfest 2017) final
PDF
Performance schema in_my_sql_5.6_pluk2013
PDF
Mysql 56-experiences-bugs-solutions-50mins
Tracing and profiling my sql (percona live europe 2019) draft_1
FOSDEM 2015: gdb tips and tricks for MySQL DBAs
More on gdb for my sql db as (fosdem 2016)
Gdb basics for my sql db as (percona live europe 2019)
Applying profilers to my sql (fosdem 2017)
Gdb basics for my sql db as (openfest 2017) final
Performance schema in_my_sql_5.6_pluk2013
Mysql 56-experiences-bugs-solutions-50mins

What's hot (20)

PDF
MariaDB Server on macOS - FOSDEM 2022 MariaDB Devroom
PDF
MySQL Performance schema missing_manual_flossuk
PDF
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
PDF
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021
PDF
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
PDF
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...
PDF
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
PDF
The New MariaDB Offering - MariaDB 10, MaxScale and more
PDF
MySQL Indexierung CeBIT 2014
PDF
WiredTiger In-Memory vs WiredTiger B-Tree
PDF
Perl Stored Procedures for MySQL (2009)
PDF
Preparse Query Rewrite Plugins
PDF
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
PDF
HA with Galera
PDF
The Full MySQL and MariaDB Parallel Replication Tutorial
PDF
MySQL Parallel Replication: inventory, use-case and limitations
PDF
MySQL/MariaDB Parallel Replication: inventory, use-case and limitations
PDF
External Language Stored Procedures for MySQL
PDF
Riding the Binlog: an in Deep Dissection of the Replication Stream
PDF
Using Perl Stored Procedures for MariaDB
MariaDB Server on macOS - FOSDEM 2022 MariaDB Devroom
MySQL Performance schema missing_manual_flossuk
Flame Graphs for MySQL DBAs - FOSDEM 2022 MySQL Devroom
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021
More on bpftrace for MariaDB DBAs and Developers - FOSDEM 2022 MariaDB Devroom
Dynamic tracing of MariaDB on Linux - problems and solutions (MariaDB Server ...
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
The New MariaDB Offering - MariaDB 10, MaxScale and more
MySQL Indexierung CeBIT 2014
WiredTiger In-Memory vs WiredTiger B-Tree
Perl Stored Procedures for MySQL (2009)
Preparse Query Rewrite Plugins
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
HA with Galera
The Full MySQL and MariaDB Parallel Replication Tutorial
MySQL Parallel Replication: inventory, use-case and limitations
MySQL/MariaDB Parallel Replication: inventory, use-case and limitations
External Language Stored Procedures for MySQL
Riding the Binlog: an in Deep Dissection of the Replication Stream
Using Perl Stored Procedures for MariaDB
Ad

Similar to Instant add column for inno db in mariadb 10.3+ (fosdem 2018, second draft) (20)

PDF
ALTER TABLE Improvements in MariaDB Server
PDF
Developers' mDay 2017. - Bogdan Kecman Oracle
PDF
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
ODP
Inno db 5_7_features
PDF
MySQL optimisation Percona LeMug.fr
PDF
Introducing Spirit - Online Schema Change
PDF
My sql 5.7-upcoming-changes-v2
PDF
15 MySQL Basics #burningkeyboards
PDF
MySQL 优化
PDF
RivieraJUG - MySQL 8.0 - What's new for developers.pdf
PDF
MySQL 8 Server Optimization Swanseacon 2018
PDF
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
PDF
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
PDF
MariaDB for developers
PPTX
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
PDF
Getting ready for the new MySQL
PDF
Sesión técnica: Big Data Analytics con MariaDB ColumnStore
PPTX
M|18 Battle of the Online Schema Change Methods
PDF
56 Query Optimization
PDF
MariaDB 10.11 key features overview for DBAs
ALTER TABLE Improvements in MariaDB Server
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Inno db 5_7_features
MySQL optimisation Percona LeMug.fr
Introducing Spirit - Online Schema Change
My sql 5.7-upcoming-changes-v2
15 MySQL Basics #burningkeyboards
MySQL 优化
RivieraJUG - MySQL 8.0 - What's new for developers.pdf
MySQL 8 Server Optimization Swanseacon 2018
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MariaDB for developers
Работа с индексами - лучшие практики для MySQL 5.6, Петр Зайцев (Percona)
Getting ready for the new MySQL
Sesión técnica: Big Data Analytics con MariaDB ColumnStore
M|18 Battle of the Online Schema Change Methods
56 Query Optimization
MariaDB 10.11 key features overview for DBAs
Ad

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Transform Your Business with a Software ERP System
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
history of c programming in notes for students .pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administraation Chapter 3
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Nekopoi APK 2025 free lastest update
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
L1 - Introduction to python Backend.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg
Understanding Forklifts - TECH EHS Solution
Design an Analysis of Algorithms II-SECS-1021-03
Transform Your Business with a Software ERP System
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo Companies in India – Driving Business Transformation.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
history of c programming in notes for students .pptx
ai tools demonstartion for schools and inter college
How Creative Agencies Leverage Project Management Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administraation Chapter 3
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
CHAPTER 2 - PM Management and IT Context
Nekopoi APK 2025 free lastest update
2025 Textile ERP Trends: SAP, Odoo & Oracle
L1 - Introduction to python Backend.pptx
Upgrade and Innovation Strategies for SAP ERP Customers

Instant add column for inno db in mariadb 10.3+ (fosdem 2018, second draft)

  • 1. Instant ADD COLUMN for InnoDB in MariaDB 10.3+ On the way to instant ALTER TABLE for failure-free record format changes Valerii Kravchuk, Principal Support Engineer, MariaDB vkravchuk@gmail.com 1
  • 2. www.percona.com Who am I? Valerii (aka Valeriy) Kravchuk: ● MySQL Support Engineer in MySQL AB, Sun and Oracle, 2005 - 2012 ● Principal Support Engineer in Percona, 2012 - 2016 ● Principal Support Engineer in MariaDB Corporation since March 2016 ● http://mysqlentomologist.blogspot.com - my blog about MySQL (a lot about MySQL bugs, but some HowTos as well) ● https://www.facebook.com/valerii.kravchuk - my Facebook page, a lot about MySQL (mostly bugs, rants and links to blog posts…) ● http://bugs.mysql.com - it used to be my personal playground ● @mysqlbugs - #bugoftheday on Twitter ● I like FOSDEM, see slides from my previous talks: ○ http://www.slideshare.net/valeriikravchuk1/fosdem2015-gdb-tips-and-tricks-for-my-sql-db-as ○ http://www.slideshare.net/ValeriyKravchuk/more-on-gdb-for-my-sql-db-as-fosdem-2016 ○ https://www.slideshare.net/ValeriyKravchuk/applying-profilers-to-my-sql-fosdem-2017 2
  • 3. www.percona.com What is this session about? ● History of ALTER TABLE improvements in MySQL ● Problems with current “online” table rebuilds ● How instant ADD COLUMN for InnoDB appeared in MariaDB ● Basic usage of instant ADD COLUMN, limitations ● Page changes for instant ADD COLUMN ● Some “benchmarks” (not sure if I have enough time…) ● Next steps on the way to instant ALTER TABLE for failure-free InnoDB record format changes that may happen in MariaDB 10.4+ (or not :) 3
  • 4. www.percona.com History of ALTER TABLE in MySQL/MariaDB ● The old way (also known as ALGORITHM=COPY starting with MySQL 5.6) ○ CREATE TABLE ...; INSERT … SELECT; RENAME TABLE ...; DROP TABLE ...; ○ Lots of unnecessary undo and redo logging in InnoDB ("bulk insert" would help) ● "Fast index creation" in InnoDB Plugin for MySQL 5.1 (built-in 5.5 InnoDB) ○ Supports ADD INDEX, ADD UNIQUE INDEX, ADD PRIMARY KEY (?) ● ALGORITHM=INPLACE starting with MySQL 5.6 ○ Misleading name; some operations may rebuild the table ■ ADD/DROP COLUMN, ADD PRIMARY KEY, CHANGE ...[NOT] NULL ○ Some operations are instant: rename column, change DEFAULT value, … ■ Should have ALGORITHM=(INSTANT|NOCOPY) to avoid surprises (MDEV-13134) ○ Somewhat wrongly called "online" DDL: ■ Changes to data in table are still prevented for some time in the process. Bug #84004! ■ It still causes slaves lag (Bug #73196) ■ We all know it should be “more online”, see Bug #77097, Bug #68498, Bug #72109, Bug #83557 etc ○ Online (LOCK=NONE) is sometimes refused: ■ ALTER TABLE … ADD (FULLTEXT|SPATIAL) INDEX, ALGORITHM=INPLACE; ■ Any table rebuild operation when FULLTEXT or SPATIAL indexes are present (see Bug #81819) 4
  • 5. www.percona.com Problems with Online Table Rebuild ● MySQL 5.6+ includes “online” ALTER TABLE ([ADD|DROP] COLUMN etc.), with LOCK=NONE. Why are tools like gh-ost or pt-osc still used? ○ Replication ignores LOCK=NONE: Slaves will only continue after commit → huge lag ○ The online log needs to be buffered (in memory or temporary files) ■ The size depends on the concurrent DML workload; hard to predict! ○ The whole table (including all indexes) will have to be copied ■ MySQL 5.7 included some performance improvements to this, but huge I/O remains ○ Theoretically, do we really have to rebuild? ■ Only when introducing stricter constraints (shorter columns, add NOT NULL) ■ Even that could be done by validating the table and editing metadata ■ Only ADD [UNIQUE|PRIMARY|SPATIAL|FULLTEXT] KEY really require writes 5
  • 6. www.percona.com History of Instant ADD COLUMN for InnoDB ● Both Alibaba and Tencent have instant ADD in their MySQL 5.6 forks ○ Does not work with old data files; requires a new ROW_FORMAT ● MariaDB wants it to work on old (possibly large) files ○ Vin Chen from Tencent Game DBA Team wrote a prototype that adds an optional record header to identify "afterwards added columns" ○ The ADD … DEFAULT values are stored in one place ○ Data dictionary only reflects the latest table definition, including the latest DEFAULT ● Marko Mäkelä rewrote the prototype for MariaDB 10.3.2 (see MDEV-11369) ○ Store a 'default row' at the start of the table (we want to remove SYS_* tables one day) ○ Support all but ROW_FORMAT=COMPRESSED ○ Crash-safe DDL (a new undo record type); simpler DML rollback; "compression" ○ ensured that online table rebuild (e.g. DROP COLUMN) still works 6
  • 7. www.percona.com Basic Usage of Instant ADD COLUMN ● By default, ALTER TABLE … ADD COLUMN is instantaneous ○ Limitation: No hidden FTS_DOC_ID column (for FULLTEXT INDEX) must exist ● Use the FORCE keyword for the old-fashioned ADD COLUMN, with the old-fashioned (additional) limitations: ○ ALGORITHM=INPLACE will not work if multiple FULLTEXT indexes exist ○ LOCK=NONE will not work if FULLTEXT or SPATIAL index exist ● To monitor the number of avoided table rebuilds: 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 7
  • 8. ● No limitations on data types for added columns or DEFAULT expression complexity: 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; SELECT * FROM t; ● No backward compatibility until rebuilt with FORCE (ToDo: check this). See MDEV-13562 also. www.percona.com Example of Instant ADD COLUMN 8
  • 9. www.percona.com Page Changes for Instant ADD COLUMN ● Clustered index root page changes: ○ FIL_PAGE_TYPE_INSTANT indicates that instant operation was used ○ PAGE_INSTANT stores the original (smaller) number of clustered index fields ● Change the leftmost clustered index leaf page: ○ After the infimum, store a "default" record with REC_INFO_MIN_REC_FLAG: ■ Must have the optional "added fields" header ■ The number of fields must match the current table definition ■ Values of "added fields" are the values of "missing fields" ● Clustered index contents from the previous example: ○ (default,id,u,d=2017-11-10 12:14:00,t='The quick brown fox',p=POINT(0 0)), ○ (1,1), (2,2), (3,3,2017-11-10 12:14:00, NULL) ○ We omit trailing fields that are equal to the fields in the "default" record 9
  • 10. www.percona.com MariaDB 10.4+: ADD… [FIRST|AFTER], DROP... ● Keep the user record format unchanged ○ Physically, keep doing ADD COLUMN last in the clustered index records ○ DROP COLUMN will leave garbage in the records ○ Changing column order physically becomes a no-op ○ ADD COLUMN will be possible even if hidden FTS_DOC_ID exists ● In the "default" record, store a mapping of table columns to index fields, pass it somehow when needed 10
  • 11. www.percona.com MariaDB 10.4+: Instant CHANGE COLUMN? ● MySQL 5.7/MariaDB 10.2: Extend VARCHAR maximum size ○ Only if the physical format allows; not VARCHAR(255) to VARCHAR(256) ● We need something in the user data records to indicate physical format ○ "Format version number" that points to something in the "default" record? ● Format changes can only be instantaneous if they relax constraints: ○ Example: CHAR(1) to CHAR(2), INT to BIGINT or NOT NULL to NULL ○ Less likely: Changing POINT to GEOMETRY, changing latin1 to utf8 ● Failure is an option, if we perform table scan to validate the data: ○ Example: Changing BIGINT NULL to INT UNSIGNED NOT NULL ● Affected secondary indexes must be rebuilt if the physical format changes ○ Still much faster than rebuilding the entire table; can be done online ● Follow MDEV-11424, “Instant ALTER TABLE of failure-free record format changes”, if interested... 11
  • 12. www.percona.com Set of Statements to Do Lame “Benchmark” See test_instant_alter.sql for more details: create table t(id int auto_increment primary key, c1 int); insert into t(c1) values (0); -- repeat 19 times insert into t(c1) select rand()*1000 from t; -- 0 select count(*) from t; -- 1 ... alter table t add column c2 char(200) default 'a'; -- 2 ... update t set c2 = 'b'; -- 3 update t set c2 = 'c'; -- 4 update t set c2 = 'd'; -- !!! ... alter table t force; -- 5 update t set c2 = 'e'; -- 6 update t set c2 = 'f'; -- 7 12 It may be interesting to compare times to execute 2 and 5, as well as 3 and 4 to 6 and 7, also for different engines (like MyRocks) and versions (PS 5.7, 10.2, 10.3, and, surely MySQL 8.0.4)
  • 13. www.percona.com Example of Instant ADD COLUMN for Benchmark create table t(id int auto_increment primary key, c1 int); insert into t(c1) values (0); insert into t(c1) select rand()*1000 from t; … continue that way until it’s big enough MariaDB [test]> insert into t(c1) select rand()*1000 from t; Query OK, 262144 rows affected ( 4.141 sec) Records: 262144 Duplicates: 0 Warnings: 0 MariaDB [test]> alter table t add column c2 char(200) default repeat('a',200); Query OK, 0 rows affected ( 0.039 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> update t set c2 = 'b'; Query OK, 524288 rows affected ( 55.682 sec) Rows matched: 524288 Changed: 524288 Warnings: 0 MariaDB [test]> update t set c2 = 'c'; Query OK, 524288 rows affected ( 31.649 sec) Rows matched: 524288 Changed: 524288 Warnings: 0 13
  • 14. www.percona.com Time to Run ALTER … ADD COLUMN (2) ● On my QuadCore Fedora 27 box, --no-defaults: 14
  • 15. www.percona.com Time to Run First UPDATE After ALTER (3) ● On my QuadCore Fedora 27 box, --no-defaults: 15
  • 16. www.percona.com Time to Run Entire “Benchmark” Script ● On my QuadCore Fedora 27 box, --no-defaults: 16
  • 17. www.percona.com Thanks and Links ● Thanks to Marko Mäkelä for his work, hints and slides on this topic ● Thanks to LeFred and Belcona team for this event! ● Check MDEV-11369 for the details of original task in MariaDB ● Check original blog post about the feature ● Check MDEV-11424 for further plans in MariaDB 10.4+ and progress on them ● My blog post on current state of online DDL in MySQL 5.7 ● Some test and raw benchmark results are shared here 17
  • 18. www.percona.com Thank you! Questions and Answers? Please, report bugs at: https://bugs.mysql.com https://jira.mariadb.org https://jira.percona.com 18