• Expertise:
• One of the world’s largest concentrations of dedicated, full-time DBA expertise.
22 © 2010/2011 Pythian
Agenda
33 © 2010/2011 Pythian
Schema changes are slow
• Drop column
• Rename column
44 © 2010/2011 Pythian
Using standby for schema changes
master-master
Primary Standby
On Standby:
•SET SQL_LOG_BIN = 0;
•Apply schema changes on standby
•Failover application to standby
55 © 2010/2011 Pythian
Using standby for schema changes
66 © 2010/2011 Pythian
“Shadow” table approach
77 © 2010/2011 Pythian
Use case
88 © 2010/2011 Pythian
The process
CREATE TABLE `t_original` (
`id` int(11) NOT NULL,
`A` varchar(50) DEFAULT NULL,
`B` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
99 © 2010/2011 Pythian
Triggers to keep data in sync
10
10 © 2010/2011 Pythian
Triggers to keep data in sync
CREATE TRIGGER t_original_ad AFTER DELETE ON
t_original
FOR EACH ROW
DELETE FROM t_new WHERE id = OLD.id;
UNLOCK TABLES;
11
11 © 2010/2011 Pythian
Copy data
t_original t_new
MIN(id)
MAX(id)
12
12 © 2010/2011 Pythian
Copy data. Sample code
$lastId=$minid;
$sql=<<SQL;
INSERT IGNORE INTO t_new(id, A, B, AB)
(SELECT id, A, B, CONCAT(A,',',B) )
FROM t_original
WHERE id>=? LIMIT 5000)
SQL
my $sth1 = $dbh->prepare($sql);
while ($rv > 1)
{
$dbh->do(‘START TRANSACTION’);
$sth1->execute($lastId);
13
13 © 2010/2011 Pythian
Copy data. Sample code
$sth = $dbh->prepare("SELECT id FROM t_original
WHERE id >='$lastId' LIMIT 5000");
$sth->execute();
$rv = $sth->rows;
14
14 © 2010/2011 Pythian
Basic checks
SELECT COUNT(*)
FROM t_new
UNION
SELECT COUNT(*)
FROM t_original;
15
15 © 2010/2011 Pythian
During the release
16
16 © 2010/2011 Pythian
Limitations
17
17 © 2010/2011 Pythian
Foreign key issue
If there is an existing FK on t_original:
FOREIGN KEY (`fkId`) REFERENCES `t_original` (`id`)
Solution is a hack:
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE t_old;
RENAME TABLE t_original TO t_old;
RENAME TABLE t_old TO t_original;
18
18 © 2010/2011 Pythian
Existing solutions
•http://code.openark.org/blog/mysql/
online-alter-table-now-available-in-
openark-kit
•http://www.facebook.com/notes/mysql-
at-facebook/online-schema-change-for-
mysql/430801045932
19
19 © 2010/2011 Pythian
Q&A
Thank you!
• zburivsky@pythian.com
• http://www.pythian.com/news/author/zburivsky/
• Twitter: @zburivsky
20
20 © 2010/2011 Pythian