oak-online-alter-table
NAME
oak-online-alter-table: Perform a non-blocking ALTER TABLE operationSYNOPSIS
Add an index on the Population column, City table, world database:oak-online-alter-table --database=world --table=City --alter="ADD KEY(Population)"Same as above, specify database with table name:
oak-online-alter-table --table=world.City --alter="ADD KEY(Population)"Perform multiple alters:
oak-online-alter-table --table=world.City --alter="ADD KEY(Population), MODIFY Name VARCHAR(64) CHARSET utf8 NOT NULL"Add column, drop column, modify AUTO_INCREMENT:
oak-online-alter-table --table=world.City --alter="ADD COLUMN Mayor VARCHAR(64) CHARSET utf8 DEFAULT '', DROP COLUMN district, MODIFY id SMALLINT AUTO_INCREMENT"Do not ALTER, just rebuild City:
oak-online-alter-table --database=world --table=CityCreate a ghost table called City_mirror, which is updated online from City table:
oak-online-alter-table --database=world --table=City --alter="ADD KEY(Population)" --ghost=City_mirrorPerform ALTER in chunks of 5000 rows:
oak-online-alter-table --table=world.City --alter="ADD KEY(Population)" --chunk-size=5000Perform ALTER in chunks of 5000 rows, sleep for 20 milliseconds between chunks:
oak-online-alter-table --table=world.City --alter="ADD KEY(Population)" --chunk-size=5000 --sleep=20Perform a cleanup for an aborted run:
oak-online-alter-table --database=world --table=City --cleanupProvide connection parameters. Prompt for password:
oak-online-alter-table --database=world --table=City --alter="ADD KEY(Population)" --user=root --ask-pass --socket=/tmp/mysql.sockUse a defaults file for connection parameters:
oak-online-alter-table --database=world --table=City --alter="ADD KEY(Population)" --defaults-file=/home/myuser/.my-oak.cnf
DESCRIPTION
oak-online-alter-table allows for non blocking ALTER TABLE operations, table rebuilds and creating a table's ghost. To run this utility, the following requirements must be met:- The table has at least one single-column UNIQUE KEY
- Altered table shares a single-column UNIQUE KEY with the original table
- No ‘AFTER’ triggers are defined on the table (the utility creates its own triggers for the duration of the operation)
- The table has no FOREIGN KEYs
- Table name is no longer than 57 characters
- A non blocking ALTER TABLE operation: adding columns or keys, dropping columns or keys, modifying columns, converting table character set etc., are all supported, under the following restrictions:
- ADD COLUMN (new column must have a default value)
- DROP COLUMN (as long as there’s still a shared single-column UNIQUE KEY between the old table and the altered one)
- MODIFY COLUMN (change type, including UNIQUE KEY columns)
- ADD KEY (normal, unique, fulltext etc.)
- DROP KEY (as long as there’s still a shared single-column UNIQUE KEY between the old table and the altered one)
- Change ENGINE: works, but great care should be taken when dealing with non-transactional engines
- Adding FOREIGN KEY constraints is possible
- More… Not all ALTERS have been tested, e.g. CONVERT, partition handling…
- An empty ALTER, which rebuilds a table: potentially releasing disk space and organizing rows in a more optimized way on disk.
- (May not be supported in future versions): Creating a live 'ghost' table, which is synchronized with the original table, as long as the following do not occur:
- An ALTER TABLE on the original table
- A TRUNCATE on the original table
- LOAD DATA INFILE into the original table
- OPTIMIZE on the original table
OPTIONS
-a ALTER_STATEMENT, --alter=ALTER_STATEMENTComma delimited ALTER statement details, excluding the 'ALTER TABLE t' itself. When this options is not used, an 'empty' ALTER is performed, essentially rebuilding the table.
--ask-passPrompt for password.
-c CHUNK_SIZE, --chunk-size=CHUNK_SIZENumber of rows to act on in chunks. Default: 1000. The lower the number, the shorter any locks are held, but the more operations required and the more total running time. Do not use very low values when the PRIMARY KEY, or otherwise the only UNIQUE KEY are on textual columns, as values from such keys are reused when working the chunks. If you're not sure - stick with the defaults.
--cleanupRemove custom triggers, ghost table from possible previous runs. In case a previous run was abruptly terminated, this option removes all custom data this utility may have created. It is not necessary to run with this option after a normal completion.
-d DATABASE, --database=DATABASEDatabase name (required unless table is fully qualified)
--defaults-file=DEFAULTS_FILERead from MySQL configuration file. Overrides --user, --password, --socket, --port.
Configuration needs to be in the following format:
[client]
user=my_user
password=my_pass
socket=/tmp/mysql.sock
port=3306
Table name to serve as ghost. When this option is used, a table by this name is created and synchronized with the original table. The original table is thereafter unaltered, and the three AFTER INSERT, AFTER UPDATE and AFTER DELETE triggers are maintained. To perform an ALTER TABLE, do not use this option. [May be removed in future versions]
-H HOST, --host=HOSTMySQL host (default: localhost)
-l, --lock-chunksUse LOCK TABLES for each chunk. This option enforces a higher locking mechanism, and is at current available as preparation to be able to work with unsupported engines. It is not required nor advisable to use this option with MyISAM or InnoDB engines. [May be removed in future versions]. -p PASSWORD, --password=PASSWORD
MySQL password
-P PORT, --port=PORTTCP/IP port (default: 3306)
-q, --quietQuiet mode, do not verbose. Verbose is on by default, use this option to turn it off.
--sleep=SLEEP_MILLISNumber of milliseconds to sleep between chunks. Default: 0. Use a higher value if your system suffers from high load average. The higher the value, the more time the system is allowed to 'rest', but also the longer the runtime, and the more overhead (over time) from the triggers effect. You may also wish to set a higher number if slaves are finding it hard otherwise to catch up.
-S SOCKET, --socket=SOCKETMySQL socket file. Only applies when host is localhost
-t TABLE, --table=TABLETable with AUTO_INCREMENT column to alter (optionally fully qualified as database_name.table_name, in which case --database is not required)
-u USER, --user=USERMySQL user
-v, --verbosePrint user friendly messages. Enabled by default.
ENVIRONMENT
Requires MySQL 5.0 or newer, python 2.3 or newer. python-mysqldb must be installed in order to use this tool. You canapt-get install python-mysqldbor
yum install mysql-python
SEE ALSO
LICENSE
This tool is released under the BSD license.Copyright (c) 2008 - 2010, Shlomi Noach All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.