最近写一个DB2的REORG的脚本,因为以前在mysql里面是没有接触过这个REORG的,所以系统了解一下,我的学习的话是先从命令入手,看看这个名的结构,然后还有选项,这些选项会揭示一些这个命令能干什么事,了解了命令,命令的选项之后再去看相关的理论,再结合理解一下应该就会比较清晰一些了。
所以先来看命令吧。
REORG INDEXES/TABLE 命令
重新组织一个索引或一张表。
你可以通过 将索引数据重建到 碎片的,物理连续的页上面。在一个数据分区的表上,你可以重组一个特定的分区表上的非分区索引,或者,你可以重组某一个数据分区上的所有分区索引。
如果你声明了index子句的CLEANUP选项,在不重建索引的情况下将会执行cleanup(cleanup is performed without rebuilding the indexes)。这个命令不能被用于声明临时表和创建临时表的索引(This command cannot be used against indexes on declared temporary tables or created temporary tables (SQLSTATE 42995))。
table选项 通过重建行来剔除碎片数据 和压缩信息来 重组一个表(The table option reorganizes a table by reconstructing the rows to eliminate fragmented data, and by compacting information),在一个分区表上,你可以重组一个单一分区。
Scope范围
这个命令影响在数据库分区组中的所有的数据库分区
Authorization权限
需要下列权限之一:
- SYSADM
- SYSCTRL
- SYSMAINT
- DBADM
- SQLADM
- CONTROL privilege on the table.
必要的连接信息Required connection
Database/数据库
命令语法
>>-REORG-------------------------------------------------------->
>--+-TABLE--table-name--| Table clause |--------------------------------+-->
+-+-INDEXES ALL FOR TABLE--table-name------------+--| Index clause |-+
| '-INDEX--index-name--+-----------------------+-' |
| '-FOR TABLE--table-name-' |
| .-ALLOW WRITE ACCESS-. |
'-TABLE--table-name--RECLAIM EXTENTS--+--------------------+---------'
+-ALLOW READ ACCESS--+
'-ALLOW NO ACCESS----'
>--+-------------------------------+---------------------------->
'-| Table partitioning clause |-'
>--+-------------------------------+---------------------------><
'-| Database partition clause |-'
Table clause
|--+-------------------+---------------------------------------->
'-INDEX--index-name-'
.-KEEPDICTIONARY--.
>--+-+-------------------+--+-------------------+--+-----------+--+---------------------+--+-----------------+-+--|
| +-ALLOW NO ACCESS---+ '-USE--tbspace-name-' '-INDEXSCAN-' '-| longlob-options |-' '-RESETDICTIONARY-' |
| '-ALLOW READ ACCESS-' |
| .-ALLOW WRITE ACCESS-. .-START--. |
'-INPLACE--+-+--------------------+--+------------------+--+--------+-+-------------------------------------'
| '-ALLOW READ ACCESS--' '-NOTRUNCATE TABLE-' '-RESUME-' |
'-+-STOP--+------------------------------------------------'
'-PAUSE-'
longlob-options
|--LONGLOBDATA--+-----------------------+-----------------------|
'-USE--longtbspace-name-'
Index clause
.-REBUILD---------------.
|--+--------------------+--+-----------------------+------------|
+-ALLOW NO ACCESS----+ '-space-reclaim-options-'
+-ALLOW WRITE ACCESS-+
'-ALLOW READ ACCESS--'
space-reclaim-options
|--+--------------------+--+-----------------+------------------|
| .-ALL---. | '-RECLAIM EXTENTS-'
'-CLEANUP--+-------+-'
'-PAGES-'
Table partitioning clause
|--ON DATA PARTITION--partition-name----------------------------|
Database partition clause
|--ON----------------------------------------------------------->
>--+-+-DBPARTITIONNUM--+--| Partition selection clause |-------------------------------------+--|
| '-DBPARTITIONNUMS-' |
'-ALL DBPARTITIONNUMS--+----------------------------------------------------------------+-'
'-EXCEPT--+-DBPARTITIONNUM--+--| Partition selection clause |--)-'
'-DBPARTITIONNUMS-'
Partition selection clause
.-,--------------------------------------------------.
V |
|--(----db-partition-number1--+--------------------------+-+--)--|
'-TO--db-partition-number2-'
命令参数Command parameters
INDEXES ALL FOR TABLE table-name
指定要重组的索引所属的表,这个表可以是位于本地数据库,也可以是位于远程数据库。
INDEX index-name
指定一个位于数据分区表上的 要重组的独立索引。 对于单一索引的重组仅支持在一个分区表上的非分区索引。这个参数不支持块索引(*block indexes*).
FOR TABLE table-name
指定 非分区索引索引名是创建在 的那个表名称。鉴于一个数据库内的索引名称是唯一的,这个参数是可选的。
重组索引的访问方式选项
ALLOW NO ACCESS
- 对于重组全部索引,这个选项指定 在索引重建过程中 其他用户不可以访问表。如果对一个分区表指定了ON DATA PARTITION子句,则只有指定非分区严格限制该访问模式。
- 对于重组单一索引,这个选项指定 在非分区索引重组时, 索引所在的表不能被其他用户访问。
ALLOW READ ACCESS
- 对于重组 全部索引,这个选项指定 在索引重建过程中 其他用户只能对表进行只读访问。ALLOW READ ACCESS模式不支持在一个分区表上 重组全部索引,除非指定了CLEANUP或RECLAIM EXTENTS选项,或者指定了ON DATA PARTITION 子句。如果对一个分区表指定了ON DATA PARTITION子句,则只有指定非分区严格限制该访问模式。
- 对于重组单一索引,这个选项指定 在非分区索引重组时, 索引所在的表仅允许用户进行只读访问。
ALLOW WRITE ACCESS
- 对于重组 全部索引,这个选项指定 在索引重组时,其他用户可以对表进行读写访问。ALLOW WRITE ACCESS模式不支持分区表,除非指定了CLEANUP或RECLAIM EXTENTS选项,或者指定了ON DATA PARTITION 子句。如果对一个分区表指定了ON DATA PARTITION子句,则只有指定非分区严格限制该访问模式。
- 对于重组单一索引,这个选项指定 在非分区索引重组过程中 可以读写表。
- ALLOW WRITE ACCESS 模式不支持多维集群( multidimensional clustering (MDC))或插入时间集群(ITC)表和扩展索引,除非指定了CLEANUP或RECLAIM EXTRNTS 选项。
下面的条目适用于数据分区表 当REORG INDEXES ALL命令指定了ON DATA PARTITION子句时 的 数据分区表:
-
只有指定数据分区严格限制访问模式级别。在一个指定的分区的分区表索引被重组的过程中,表的其他分区允许用户读写。
下面的 表 列出当指定了ON DATA PARTITION子句时,表的其他分区上支持的和允许的一致性访问。表格1:当指定REORG INDEXES ALL 命令的ON DATA PARTITION 子句时 ,支持的 访问模式和并发(*concurrent*)访问允许
|Access mode | 指定分区上的并发访问允许|其他分区上的并发访问允许|
|--|--|--|
| ALLOW NO ACCESS |不允许访问 | 读写访问|
| ALLOW READ ACCESS | 分区上可读直到索引被更新| 读写访问|
| ALLOW WRITE ACCESS | 分区上读写访问直到索引被更新| 读写|
- 只有 指定分区的 分区索引被重组。分区表上的不分区索引不会被重组。
如果在表上有被标记为“失效”或“用于重构(rebuild)”的非分区索引,这些索引会在重组之前rebuild。如果没有,如果索引对象被标记为“失效”或“用于重构(rebuild)” 只有指定分区上的分区索引被重组或重构 . - 当指定了 CLEANUP 或 RECLAIM EXTENTS选项时只有指定分区的分区索引被cleaned 。
下面的表格列示了分区表和非分区表的索引重组时支持的访问模式。
表格2. 分区表和分区表上的索引重组支持的访问模式
命令 | 表类型 | 表分区子句 | 索引子句指定的附加参数 | 支持的访问模式 |
---|---|---|---|---|
REORG INDEXES | 非分区表 | 不适用 Not applicable |
Any | ALLOW NO ACCESS, ALLOW READ ACCESS1, ALLOW WRITE ACCESS |
REORG INDEX | 分区表 | 不适用 Not applicable |
Any | ALLOW NO ACCESS, ALLOW READ ACCESS1, ALLOW WRITE ACCESS |
REORG INDEXES | 分区表 | None | REBUILD (不指定的话 这是默认的) |
ALLOW NO ACCESS 1 |
REORG INDEXES | 分区表 | ON DATA PARTITION | REBUILD (不指定的话 这是默认的) |
ALLOW NO ACCESS, ALLOW READ ACCESS1, ALLOW WRITE ACCESS |
REORG INDEXES | 分区表 | 有或没有ON DATA PARTITION 子句 |
指定CLEANUP 或 RECLAIM EXTENTS |
ALLOW NO ACCESS, ALLOW READ ACCESS1, ALLOW WRITE ACCESS |
注:标有上标1的是默认的访问模式.
CLEANUP
当需要CLEANUP时,执行cleanup(清空)而不是REBUILD。索引不被重建,并且释放的任何页 仅可以被 定义在这个表上的索引重用。
ALL
指定索引 应该 通过提交 pseudo 删除的键 和pseudo的 空页 而打扫干净。
CLEANUP ALL选项将会释放标记为(pseudo)空 的页,也会从非空的pages中提交标记(pseudo)删除的键。 如果合并可以 使得 合并后的叶子page上 至少有合并叶子页的 PCTFREE 的空闲空间的话, 这个选项也会尝试合并临近的叶子页, PCTFREE是 在索引创建时为索引定义的空闲空间百分比。默认的PCTFREE是10%。如果两个页可以合并,这两个页中的一个将被释放。在一个索引中pseudo删除的键(包含那些在pseudo空的页上的键 )的数目 可以通过运行RUNSTATS命令,然后select NUMRIDS DELETED from SYSCAT.INDEXES来获得。ALL选项可以清空NUMRIDS DELETED 和 NUMEMPTY LEAFS ,在它们已经被确定要提交的情况下。
PAGES
这个选项指定 已提交的 pseudo空的页 需要 从索引树中移除。这将不会清空那些没有被标记为空的页上pseudo删除的键。既然它只检查那些标记为空的叶子页,那么大多数情况下就会比使用ALL选项 相对要快。
CLEANUP PAGES选项会搜索并且释放pseudo空的页。一个已经提交的pseudo空的页 是这样一个页,这个 页上的所有的键都被标记为删除,所有的这些删除操作都是等待被提交的( are known to be committed). 一个索引中 pseudo 空的页 可以通过运行RUNSTATS命令和审视 SYSCAT.INDEXES表中的NUM EMPTY LEAFS列来判定/裁定。 PAGES选项将会清扫 NUM EMPTY LEAFS 如果它们被 裁定为要提交的。
使用ALLOW READ ACCESS 或 ALLOW WRITE ACCESS 选项来允许 在索引被重组的过程中 其他事务 对表的只读或可读写访问权限 。 在被重组的索引的拷贝 被可用之前 的这段时期内 重建索引 时 不允许访问表(No access to the table is allowed when rebuilding an index during the period in which the reorganized copies of the indexes are made available.)。
INDEX index-name REBUILD
REBUILD选项是默认的,表现 在之前的版本中当没有指定CLEANUP和CONVERT子句时由 索引重组 提供的 相同的功能。索引重组的REBUILD选项 将索引数据重构(rebuild)到一组物理连续的页( physically contiguous pages)上。默认的访问模式基于表类型而不同。
INDEX index-name RECLAIM EXTENTS
指定索引 去重组和回收没有被使用的扩展数据块。这个行为 在索引的页之内移动索引对象来创建空的extents,然后释放这些空的extents 从被索引对象使用的 专用空间(exclusive)中 释放这些空的extents,使这些空间可以被这个表空间中的其他数据库对象使用。extents从索引对象中回收到表空间中。默认的模式是ALLOW READ ACCESS,但是事实上可以支持所有的访问模式。
TABLE table-name RECLAIM EXTENTS
Specifies the table to reorganize and reclaim extents that are not being used. The table-name variable must specify a multidimensional clustering (MDC), insert time clustering (ITC) table or column-organized tables. The name or alias in the form: schema.table-name can be used. The schema is the user name under which the table was created. If you omit the schema name, the default schema is assumed.
For REORG TABLE RECLAIM EXTENTS when the ON DATA PARTITION clause is specified, the access clause only applies to the named partition. Users can read from and write to the rest of the table while the extents on the specified partition are being reclaimed. This situation also applies to the default access levels.
ALLOW NO ACCESS
For REORG TABLE RECLAIM EXTENTS, specifies that no other users can access the table while the extents are being reclaimed.
ALLOW READ ACCESS
For REORG TABLE RECLAIM EXTENTS, specifies that other users can have read-only access to the table while the extents are being reclaimed.
ALLOW WRITE ACCESS
For REORG TABLE RECLAIM EXTENTS, specifies that other users can read from and write to the table while the extents are being reclaimed. This is the default option.
TABLE table-name
指定要重组的表。这个表可以位于本地或远程数据库。名称或别名遵循以下形式:schema.table-name 的可以被使用。模式是表创建在哪个用户名下。如果你忽略了模式名,将使用默认的模式名。
对typed表,指定的表名称必须是继承的根表的名称。
你不能指定 为 多维集群(MDC)或插入时间集群(ITC)表指定一个索引。 因为 放置 表的重组 的地方不能被用于MDC或ITC(In place reorganization of tables cannot be used for MDC or ITC tables)。
当 为一个数据分组的表的表重组 指定ON DATA PARTITION子句时,只有指定的数据分区被指定:
- 如果在表上没有定义非分区索引(除了系统自动生成的XML路径索引),访问模式仅适用于指定的分区,用户被允许读写表的其他表。
- 如果(除了系统自动生成的XML路径索引)表上定义 了 非分区索引,默认使用ALLOW NO ACCESS访问模式,并且仅支持这个访问模式。在这种情况下,表被放在ALLOW NO ACCESS模式下。如果指定ALLOW READ ACCESS (允许读访问),将会返回SQL 1548N。
表格3:非分区表和分区表在表重组时支持的访问模式
命令 | 表类型 | 表分区子句 | 支持的访问模式 |
---|---|---|---|
REORG TABLE | 非分区表 | Not applicable | ALLOW NO ACCESS, ALLOW READ ACCESS1 |
REORG TABLE | 分区表 | Not specified | ALLOW NO ACCESS1 |
REORG TABLE (表上没有索引或者是只有分区索引) |
分区表 | ON DATA PARTITION | ALLOW NO ACCESS, ALLOW READ ACCESS1 |
REORG TABLE (表上有除了系统生成的XML路径索引之外的其他非分区索引) |
分区表 | ON DATA PARTITION | ALLOW NO ACCESS1 |
注:
上标1:当访问子句没有指定时使用默认模式。
对于一个分区表,表重组 在重新组织表之后 重新建立 表上的 非分区索引和分区索引。如果ON DATA PARTITION 子句 被用来重新组织 数据分区表的一个特定的数据分区, 表重组 只为特定的分区 重建非分区索引和分区索引
INDEX index-name
这个选项用来指定 当重新组织表的时候 使用的索引。如果你不以如下格式 schema.index-name指定全名,将使用默认的模式。默认模式是这个索引被创建在哪个用户下。数据库管理这个在表重组的时候使用这个索引来完全的对表中的数据进行重新排序。
对于一个in place表重组,如果集群索引 被定义在表上且指定了一个索引,那么必须指定的必须是这个集群索引。如果没有指定in place选项,可以指定任何索引。如果你不指明一个索引名,记录将不管顺序进行重组。如果这个表定义了一个集群索引,那么,即使是没有指明索引,也会使用该聚集索引来聚集表。如果你正在重组MDC表或IDC表,那么你不能指定索引。
如果一个表重组使用了INDEX,也使用了ON DATA PARTITION子句,那么只有指定的分区被使用该索引进行重组。
ALLOW NO ACCESS
指定在表重组的过程中其他用户不能访问该表。
ALLOW NO ACCESS是默认的模式,并且是在没有指定ON DATA PARTITION 子句 的情况下重组一个分区表时 唯一支持 的模式。
如果为一个数据分区表指定了 ON DATA PARTITION子句,那么只有这个被指定的数据分区被重组。
- 如果除了系统生成的XML路径索引之外表上没有定义非分区索引,只有指定的分区限制ALLOW NO ACCESS模式。运行用户读写该表的其他数据分区。
- access mode. In this case, the table is placed in ALLOW NO ACCESS mode.如果除了系统生成的XML路径索引之外,表上定义了非分区索引,那么ALLOW NO ACCESS是默认的模式并且是唯一支持的访问模式。
ALLOW READ ACCESS
在重组过程中仅允许对表的读访问。
.ALLOW READ ACCESS模式是一个非分区表的默认模式。
如果 为一个数据分区表指定了 ON DATA PARTITION 子句,那么仅有指定的数据分区被重组:
- 如果除了系统生成的XML路径索引之外表上没有定义非分区索引,那么ALLOW READ ACCESS是默认的模式,并且只有指定的分区被限制该访问级别,用户可以读写表的其他分区。
- 如果除了系统生成的XML路径索引之外表上定义了非分区索引,那么不支持ALLOW READ ACCESS模式。如果在这种情况下指定了ALLOW READ ACCESS,那么会返回SQL1548N的错误 (SQLSTATE 5U047)。
INPLACE(inplace: 在合适的位置)
在允许用户访问的情况下重新组织表。
合适位置表重组(In place table reorganization )仅适用于非分区、非MDC、非ITC的表,且要求表中 没有扩展索引(extended indexes) 没有定义在XML列上的索引的情况。合适位置表重组只能在至少有3个页的表上执行。
合适位置表重组异步的进行,并且可能不立即生效。
-
ALLOW READ ACCESS
在表重组过程中仅允许读访问。
-
ALLOW WRITE ACCESS
在表重组过程中允许写访问,这是默认情况。
-
NOTRUNCATE TABLE
在合适位置重组之后不truncate表。在truncation时期,这个表加S锁。
-
START
开始 in place REORG处理过程。因为这是默认的,所以这个关键字是可选的。
-
STOP
在当前时间点停止in place进程。
-
PAUSE
为当前时间正在进行时的in place REORG 挂起或终止(* Suspend or pause in place REORG for the time being.*)。
-
RESUME
继续或恢复一个之前中断的in place 表重组。当一个在线重组被恢复后,并且你希望使用与重组中断时使用的相同的选项你必须在恢复时再指定一遍选项。
USE tbspace-name
这个选项指定一个系统临时表空间的名称,在这个表空间中存放一个被重组的临时表。如果你不停一个表空间名称,数据库管理者在将被重组的表所在的表空间中存储一份表的工作备份。
对一个8KB,16 KB, 或32 KB的表对象,如果你所指定的系统临时表空间的页大小与表数据所在的表空间的页大小不匹配,DB2数据库产品会尝试找一个 LONG/LOB对象的正确大小的 临时表空间。 这样的一个表空间必须存在才能重组成功。
对于分区表,临时表空间被用作 表的数据分区的重新组织 的临时存储。整个分区表的重组 在一个时刻只会 重新组织一个单一数据分区。临时表空间必须能够保存表中的最大的数据分区的数据,而不是整个表的数据。当指定 ON DATA PARTITION 子句时,临时表空间必须能够保存指定的分区的数据。
如果你不为分区表提供一个表空间的名称,每个数据分区所在的表空间会被用于数据分区的临时存储。数据分区所在的表空间上必须有足够的可用空间来存储数据分区的备份。
INDEXSCAN索引扫描
对于一个聚集的REORG,索引扫描 将会被用来重新组织表的记录。通过through 索引 访问表 重新组织表的行。默认的方法是扫描表,排序结果来重新组织表,使用临时表空间是必要的。 即使 through 索引键作为排序顺序,扫描和排序 比起 先从一个索引中读记录标识符 然后遍历记录来 仍会比较快。
LONGLOBDATA
长fielf和LOB数据将会被重新组织。
This is not required even if the table contains long or LOB columns. The default is to avoid reorganizing these objects because it is time consuming and does not improve clustering. However, running a reorganization with the LONGLOBDATA option on tables with XML columns will reclaim unused space and thereby reduce the size of the XML storage object.
This parameter is required when converting existing LOB data into inlined LOB data.
USE longtbspace-name
This is an optional parameter, which can be used to specify the name of a temporary table space to be used for rebuilding long data. If no temporary table space is specified for either the table object or for the long objects, the objects will be constructed in the table space they currently reside. If a temporary table space is specified for the table but this parameter is not specified, then the table space used for base reorg data will be used, unless the page sizes differ. In this situation, the DB2 database system will attempt to choose a temporary container of the appropriate page size to create the long objects in.
If USE longtbspace-name is specified, USE tbspace-name must also be specified. If it is not, the longtbspace-name argument is ignored.
KEEPDICTIONARY保持字典
If the COMPRESS attribute for the table is YES and the table has a compression dictionary then no new dictionary is built. All the rows processed during reorganization are subject to compression using the existing dictionary. If the COMPRESS attribute is YES and a compression dictionary doesn't exist for the table, a dictionary will only be created (and the table compressed) in this scenario if the table is of a certain size (approximately 1 to 2 MB) and sufficient data exists within this table. If, instead, you explicitly state REORG RESETDICTIONARY, then a dictionary is built as long as there is at least 1 row in the table. If the COMPRESS attribute for the table is NO and the table has a compression dictionary, then reorg processing will preserve the dictionary and all the rows in the newly reorganized table will be in noncompressed format. It is not possible to compress some data such as LOB data not stored in the base table row.
When the LONGLOBDATA option is not specified, only the table row data is reorganized. The following table describes the behavior of KEEPDICTIONARY syntax in REORG command when the LONGLOBDATA option is not specified.
RESETDICTIONARY
If the COMPRESS attribute for the table is YES then a new row compression dictionary is built. All the rows processed during reorganization are subject to compression using this new dictionary. This dictionary replaces any previous dictionary. If the COMPRESS attribute for the table is NO and the table does have an existing compression dictionary then reorg processing will remove the dictionary and all rows in the newly reorganized table will be in noncompressed format. It is not possible to compress some data such as LOB data not stored in the base table row.
If the LONGLOBDATA option is not specified, only the table row data is reorganized. The following table describes the behavior of RESETDICTIONARY syntax in REORG command when the LONGLOBDATA option is not specified.
ON DATA PARTITION partition-name
这个选项为数据分区表指定要重组的数据分区。
DB2 9.7 修补补丁1和之后的版本,这个子句可以用在REORG INDEXES ALL 命令之后来重组在某一个特定分区上非分区索引;可以用在REORG TABLE 命令之后来重组在一个特定分区上的数据。
如果指定的表上这个分区号码不存在时,当在这个分区表上 的REORG TABLE或REORG INDEXES ALL 命令之后使用这个子句时,重组失败并且返回SQL2222N错误,错误原因码为1 。 如果分区的分区号码状态为附属的或分离的状态时(is in the attached or detached state),重组失败并且返回SQL2222N错误,错误原因码为3。
如果执行带有 ON DATA PARTITION 的REORG INDEX 命令,重组失败并且返回SQL2222N错误,错误原因码为2。
如果分区表 在reorg挂起状态 ,并且表上没有没有非分区索引定义,REORG TABLE 命令失败并且返回SQL1549N (SQLSTATE 5U047) 错误 。
ALL DBPARTITIONNUMS
指定操作 对在db2nodes.cfg 文件中声明的所有的数据库分区生效。如果没有指定数据库分区子句 默认对所有声明的数据库分区生效。
EXCEPT
指定操作影响db2nodes.cfg 文件中声明的所有的数据库分区,除了在数据库分区list中声明的分区。
ON DBPARTITIONNUM | ON DBPARTITIONNUMS
在一个数据分区集合上执行操作。
-
db-partition-number1
指定一个再数据库分区list中的数据库分区号。
-
db-partition-number2
Specifies the second database partition number, so that all database partitions from db-partition-number1 up to and including db-partition-number2 are included in the database partition list. 指定第二个数据库分区号,从而从*db-partition-number1*开始到*db-partition-number2*包含*db-partition-number2*的所有的数据库分区 都被包含在数据库分区列表中。
Examples
example 1
要重组一个表从而回收空间(reclaim space),使用临时表空间mytemp1,输入以下的命令:
db2 reorg table homer.employee use mytemp1
example 2
To reorganize tables in a partition group consisting of database partitions 1, 2, 3, and 4 of a four-partition system, you can enter either of the following commands:
要重组一个有数据库分区组,该分区组由1,2,3,4四个分区构成的四分区系统,你可以输入下面的 命令
db2 reorg table employee index empid on dbpartitionnum (1,3,4)
db2 reorg table homer.employee index homer.empid on all
dbpartitionnums except dbpartitionnum (2)
example 3
在允许其他事务同时读表和更新表的前提下, 清理 在EMPLOYEE 表中的所有索引中的 pseudo(标记) 删除的键和pseudo(标记) 空的页,输入:
db2 reorg indexes all for table homer.employee allow write access cleanup only
example 4
在允许其他事务同时读表和更新表的前提下, 清理 在EMPLOYEE 表中的所有索引中的 pseudo(标记) 空的页,输入:
db2 reorg indexes all for table homer.employee allow write access cleanup only pages
example 5
使用系统临时表空间TEMPSPACE1 作为工作域来重组EMPLOYEE表 ,输入:
db2 reorg table homer.employee use tempspace1
example 6
使用默认模式HOMER 启动、中断、恢复 EMPLOYEE表的in place 重组 ,这个HOMER模式在之前的例子中明确的声明过,输入下面的命令:
db2 reorg table employee index empid inplace start
db2 reorg table employee inplace pause
db2 reorg table homer.employee inplace allow read access notruncate table resume
这个命令恢复reorg 包含附加的关键词 指明了只读访问 并且跳过清空步骤,这个表S-lock锁定。
Usage notes
Restrictions:
- The REORG utility does not support the use of nicknames.
- The REORG TABLE command is not supported for declared temporary tables or created temporary tables.
- The REORG TABLE command cannot be used on views.
- Reorganization of a table is not compatible with range-clustered tables, because the range area of the table always remains clustered.
- REORG TABLE cannot be used on a partitioned table in a DMS table space while an online backup of ANY table space in which the table resides, including LOBs and indexes, is being performed.
- REORG TABLE cannot use an index that is based on an index extension.
- If a table is in reorg pending state, an inplace reorg is not allowed on the table.
- Concurrent table reorganization sharing the same temporary DMS table space is not supported.
- Before running a reorganization operation against a table to which event monitors write, you need to deactivate the event monitors on that table.
-
For data partitioned tables:
- The table must have an ACCESS_MODE in SYSCAT.TABLES of Full Access.
- Reorganization skips data partitions that are in a restricted state due to an attach or detach operation. If the ON
- DATA PARTITION clause is specified, that partition must be fully accessible.
- If an error occurs during table reorganization, some indexes or index partitions might be left invalid. The nonpartitioned indexes of the table will be marked invalid if the reorganization has reached or passed the replace phase for the first data partition. The index partitions for any data partition that has already reached or passed the replace phase will be marked invalid. Indexes will be rebuilt on the next access to the table or data partition.
- If an error occurs during index reorganization when the ALLOW NO ACCESS mode is used, some indexes on the table might be left invalid. For nonpartitioned RID indexes on the table, only the index that is being reorganized at the time of the failure will be left invalid. For MDC tables with nonpartitioned block indexes, one or more of the block indexes might be left invalid if an error occurs. For MDC or ITC tables with partitioned indexes, only the index object on the data partition being reorganized will be left invalid. Any indexes marked invalid will be rebuilt on the next access to the table or data partition.
- When a data partitioned table with only partitioned indexes defined on the table is in the reorg pending state, issuing a REORG TABLE command with the ON DATA PARTITION clause brings only the specified data partition out of the reorg pending state. To bring the remaining partitions of the table out of the reorg pending state, either issue REORG TABLE command on the entire table (without the ON
DATA PARTITION clause), or issue a REORG TABLE command with the ON DATA PARTITION clause for each of the remaining partitions.
Information about the current progress of table reorganization is written to the history file for database activity. The history file contains a record for each reorganization event. To view this file, execute the LIST HISTORY command for the database that contains the table you are reorganizing.
You can also use table snapshots to monitor the progress of table reorganization. Table reorganization monitoring data is recorded regardless of the Database Monitor Table Switch setting.
If an error occurs, an SQLCA dump is written to the history file. For an inplace table reorganization, the status is recorded as PAUSED.
When an indexed table has been modified many times, the data in the indexes might become fragmented. If the table is clustered with respect to an index, the table and index can get out of cluster order. Both of these factors can adversely affect the performance of scans using the index, and can impact the effectiveness of index page prefetching. REORG INDEX or REORG INDEXES with the REBUILD option can be used to reorganize one or all of the indexes on a table. Index reorganization rebuild will remove any fragmentation and restore physical clustering to the leaf pages. Use the REORGCHK command to help determine if an index needs reorganizing. Be sure to complete all database operations and release all locks before invoking index reorganization. This can be done by issuing a COMMIT after closing all cursors opened WITH HOLD, or by issuing a ROLLBACK.
A classic table reorganization (offline reorganization) rebuilds the indexes during the last phase of the reorganization. When more than one temporary table space exists, it is possible that a temporary table space in addition to the one specified on the REORG TABLE command may be utilized for additional sorts that can accompanying table reorg processing. However, the inplace table reorganization (online reorganization) does not rebuild the indexes. It is recommended that you issue a REORG INDEXES command after the completion of an inplace table reorganization. An inplace table reorganization is asynchronous, therefore care must be taken to ensure that the inplace table reorganization is complete before issuing the REORG INDEXES command. Issuing the REORG INDEXES command before the inplace table reorganization is complete, might cause the reorganization to fail (SQLCODE -2219).
When the REORG rebuilds the indexes on an MDC table, the Full_Block hint bits are not set. Because the Full_Block hint is not set, you might experience degraded insert performance if you insert rows from existing dimension values after the REORG completes and the DB2_TRUST_MDC_BLOCK_FULL_HINT registry variable is turned on. The insert performance automatically improves for each dimension value after an insert of that dimension value completes. For more information, see DB2_TRUST_MDC_BLOCK_FULL_HINT performance variable.
Tables that have been modified so many times that data is fragmented and access performance is noticeably slow are candidates for the REORG TABLE command. You should also invoke this utility after altering the inline length of a structured type column in order to benefit from the altered inline length. Use the REORGCHK command to determine whether a table needs reorganizing. Be sure to complete all database operations and release all locks before invoking REORG TABLE. This can be done by issuing a COMMIT after closing all cursors opened WITH HOLD, or by issuing a ROLLBACK. After reorganizing a table, use RUNSTATS to update the table statistics, and REBIND to rebind the packages that use this table. The reorganize utility will implicitly close all the cursors.
With DB2 V9.7 Fix Pack 1 and later, REORG TABLE commands and REORG INDEXES ALL commands can be issued on a data partitioned table to concurrently reorganize different data partitions or partitioned indexes on a partition. When concurrently reorganizing data partitions or the partitioned indexes on a partition, users can access the unaffected partitions but cannot access the affected partitions. All the following criteria must be met to issue REORG commands that operate concurrently on the same table:
Each REORG command must specify a different partition with the ON DATA PARTITION clause.
Each REORG command must use the ALLOW
NO ACCESS mode restrict access to the data partitions.
The partitioned table must have only partitioned indexes if issuing REORG TABLE commands. No nonpartitioned indexes (except system-generated XML path indexes) can be defined on the table.
For a partitioned table T1 with no nonpartitioned indexes (except system-generated XML path indexes) and with partitions P1, P2, P3, and P4, the following REORG commands can run concurrently:
REORG INDEXES ALL FOR TABLE T1 ALLOW NO ACCESS ON DATA PARTITION P1
REORG TABLE T1 ALLOW NO ACCESS ON DATA PARTITION P2
REORG INDEXES ALL FOR TABLE T1 ALLOW NO ACCESS ON DATA PARTITION P3
Operations such as the following are not supported when using concurrent REORG commands:
Using a REORG command without the ON
DATA PARTITION clause on the table.
Using an ALTER TABLE statement on the table to add, attach, or detach a data partition.
Loading data into the table.
Performing an online backup that includes the table.
If the table contains mixed row format because the table value compression has been activated or deactivated, an offline table reorganization can convert all the existing rows into the target row format.
If the table is distributed across several database partitions, and the table or index reorganization fails on any of the affected database partitions, only the failing database partitions will have the table or index reorganization rolled back.
If the reorganization is not successful, temporary files should not be deleted. The database manager uses these files to recover the database.
If the name of an index is specified, the database manager reorganizes the data according to the order in the index. To maximize performance, specify an index that is often used in SQL queries. If the name of an index is not specified, and if a clustering index exists, the data will be ordered according to the clustering index.
The PCTFREE value of a table determines the amount of free space designated per page. If the value has not been set, the utility will fill up as much space as possible on each page.
To complete a table space rollforward recovery following a table reorganization, both regular and large table spaces must be enabled for rollforward recovery.
If the table contains LOB columns that do not use the COMPACT option, the LOB DATA storage object can be significantly larger following table reorganization. This can be a result of the order in which the rows were reorganized, and the types of table spaces used (SMS or DMS).
Indexes over XML data may be re-created by the REORG INDEXES/TABLE command. For details, see "Recreation of indexes over XML data".
An inplace REORG operation might not be able to fully reclaim space in a table because it cannot move internal records.