Enterprise Edition Home | Express Edition Home | Previous Page | Next Page   Table Performance Considerations > Changing Tables >

Altering a Table Definition

The database server uses one of several algorithms to process an ALTER TABLE statement in SQL:

Slow Alter

When the database server uses the slow alter algorithm to process an ALTER TABLE statement, the table can be unavailable to other users for a long period of time because the database server:

The database server uses the slow alter algorithm when the ALTER TABLE statement makes column changes that it cannot perform in place:

In-Place Alter

The in-place alter algorithm provides the following performance advantages over the slow alter algorithm:

When the Database Server Uses the In-Place Alter Algorithm

The database server uses the in-place alter algorithm for certain types of operations that you specify in the ADD, DROP, and MODIFY clauses of the ALTER TABLE statement:

Important:
When a table contains a user-defined data type or smart large objects, the database server does not use the in-place alter algorithm even when the column being altered contains a built-in data type.

Table 7 shows the conditions under which the ALTER TABLE MODIFY statement uses the in-place alter algorithm to process the ALTER TABLE MODIFY Statement.

Table 7. MODIFY Operations and Conditions That Use the In-Place Alter Algorithm
Operation on Column Condition
Convert a SMALLINT column to a INTEGER column All
Convert a SMALLINT column to an INTEGER8 column All
Convert a SMALLINT column to a DEC(p2,s2) column p2-s2 >= 5
Convert a SMALLINT column to a DEC(p2) column p2-s2 >= 5 OR nf
Convert a SMALLINT column to a SMALLFLOAT column All
Convert a SMALLINT column to a FLOAT column All
Convert a SMALLINT column to a CHAR(n) column n >= 6 AND nf
Convert an INT column to an INTEGER8 column All
Convert an INT column to a DEC(p2,s2) column p2-s2 >= 10
Convert an INT column to a DEC(p2) column p2 >= 10 OR nf
Convert an INT column to a SMALLFLOAT column nf
Convert an INT column to a FLOAT column All
Convert an INT column to a CHAR(n) column n >= 11 AND nf
Convert a SERIAL column to an INTEGER8 column All
Convert an SERIAL column to a DEC(p2,s2) column p2-s2 >= 10
Convert a SERIAL column to a DEC(p2) column p2 >= 10 OR nf
Convert a SERIAL column to a SMALLFLOAT column nf
Convert a SERIAL column to a FLOAT column All
Convert a SERIAL column to a CHAR(n) column n >= 11 AND nf
Convert a SERIAL column to a SERIAL column All
Convert a SERIAL column to a SERIAL8 column All
Convert a DEC(p1,s1) column to a SMALLINT column p1-s1 < 5 AND
(s1 == 0 OR nf)
Convert a DEC(p1,s1) column to an INTEGER column p1-s1 < 10 AND
(s1 == 0 OR nf)
Convert a DEC(p1,s1) column to an INTEGER8 column p1-s1 < 20 AND
(s1 == 0 OR nf)
Convert a DEC(p1,s1) column to a SERIAL column p1-s1 < 10 AND
(s1 == 0 OR nf)
Convert a DEC(p1,s1) column to a SERIAL8 column p1-s1 < 20 AND
(s1 == 0 OR nf)
Convert a DEC(p1,s1) column to a DEC(p2,s2) column p2-s2 >= p1-s1 AND
(s2 >= s1 OR nf)
Convert a DEC(p1,s1) column to a DEC(p2) column p2 >= p1 OR nf
Convert a DEC(p1,s1) column to a SMALLFLOAT column nf
Convert a DEC(p1,s1) column to a FLOAT column nf
Convert a DEC(p1,s1) column to a CHAR(n) column n >= 8 AND nf
Convert a DEC(p1) column to a DEC(p2) column p2 >= p1 OR nf
Convert a DEC(p1) column to a SMALLFLOAT column nf
Convert a DEC(p1) column to a FLOAT column nf
Convert a DEC(p1) column to a CHAR(n) column n >= 8 AND nf
Convert a SMALLFLOAT column to a DEC(p2) column nf
Convert a SMALLFLOAT column to a FLOAT column nf
Convert a SMALLFLOAT column to a CHAR(n) column n >= 8 AND nf
Convert a FLOAT column to a DEC(p2) column nf
Convert a FLOAT column to a SMALLFLOAT column nf
Convert a FLOAT column to a CHAR(n) column n >= 8 AND nf
Convert a CHAR(m) column to a CHAR(n) column n >= m OR (nf AND not ANSI mode)
Increase the length of a CHARACTER column Not ANSI mode
Increase the length of a DECIMAL or MONEY column All

Notes:

Performance Considerations for DML Statements

Each time you execute an ALTER TABLE statement that uses the in-place alter algorithm, the database server creates a new version of the table structure. The database server keeps track of all versions of table definitions. The database server resets the version status and all of the version structures and alter structures until the entire table is converted to the final format or a slow alter is performed.

If the database server detects any down-level version page during the execution of data manipulation language (DML) statements (INSERT, UPDATE, DELETE, SELECT), it performs the following actions:

Performance Considerations for DDL Statements

The oncheck -pT tablename option displays data-page versions for outstanding in-place alter operations. An in-place alter is outstanding when data pages still exist with the old definition.

Figure 33 displays a portion of the output that the following oncheck command produces after four in-place alter operations are executed on the customer demonstration table:

Figure 33. Sample oncheck -pT Output for customer Table
oncheck -pT stores_demo:customer

...
Home Data Page Version Summary

            Version           Count

            0 (oldest)           2
            1           0
            2           0
            3           0
            4 (current)           0
...

The Count field in Figure 33 displays the number of pages that currently use that version of the table definition. This oncheck output shows that four versions are outstanding:

Important:
As you perform more in-place alters on a table, each subsequent ALTER TABLE statement takes more time to execute than the previous statement. Therefore, it is recommended that you have no more than approximately 50 to 60 outstanding alters on a table. A large number of outstanding alters affects only the subsequent ALTER TABLE statements, but does not degrade the performance of SELECT statements.

You can convert data pages to the latest definition with a dummy UPDATE statement. For example, the following statement, which sets a column value to the existing value, causes the database server to convert data pages to the latest definition:

UPDATE tab1 SET col1 = col1;

This statement does not change any data values, but it converts the format of the data pages to the latest definition.

After an update is executed on all pages of the table, the oncheck -pT command displays the total number of data pages in the Count field for the current version of the table.

Alter Operations That Do Not Use the In-Place Alter Algorithm

The database server does not use the in-place alter algorithm in the following situations:

Altering a Column That Is Part of an Index

If the altered column is part of an index, the table is still altered in place, but in this case the database server rebuilds the index or indexes implicitly. If you do not need to rebuild the index, you should drop or disable it before you perform the alter operation. Taking these steps improves performance.

However, if the column that you modify is a primary key or foreign key and you want to keep this constraint, you must specify those keywords again in the ALTER TABLE statement, and the database server rebuilds the index.

For example, suppose you create tables and alter the parent table with the following SQL statements:

CREATE TABLE parent 
   (si smallint primary key constraint pkey);
CREATE TABLE child
   (si smallint references parent on delete cascade
   constraint ckey);
INSERT INTO parent (si) VALUES (1); 
INSERT INTO parent (si) VALUES (2); 
INSERT INTO child (si) VALUES (1); 
INSERT INTO child (si) VALUES (2); 
ALTER TABLE parent
     MODIFY (si int PRIMARY KEY constraint PKEY);

This ALTER TABLE example converts a SMALLINT column to an INT column. The database server retains the primary key because the ALTER TABLE statement specifies the PRIMARY KEY keywords and the PKEY constraint. However, the database server drops any referential constraints to that primary key. Therefore, you must also specify the following ALTER TABLE statement for the child table:

ALTER TABLE child 
   MODIFY       (si int references parent on delete cascade
         constraint ckey);

Even though the ALTER TABLE operation on a primary key or foreign key column rebuilds the index, the database server still takes advantage of the in-place alter algorithm. The in-place alter algorithm provides the following performance benefits:

Warning:
If you alter a table that is part of a view, you must re-create the view to obtain the latest definition of the table.

Fast Alter

The database server uses the fast alter algorithm when the ALTER TABLE statement changes attributes of the table but does not affect the data. The database server uses the fast alter algorithm when you use the ALTER TABLE statement to:

With the fast alter algorithm, the database server holds the lock on the table for just a short time. In some cases, the database server locks the system catalog tables only to change the attribute. In either case, the table is unavailable for queries for only a short time.

Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]