天天看點

DB2聯機移動表——admin_move_table1.ADMIN_MOVE_TABLE 過程-線上移動表2.通過使用 ADMIN_MOVE_TABLE 過程來以聯機方式移動表

admin_move_table存儲過程将資料從一個活動表 移動到一個相同名稱的新表對象中,而被移動的這個表在這個過程中是聯機且可以被通路的。這個存儲過程建立一個協定表(protocol table),這個協定表由包含 與将被移動的表相關的 狀态資訊和配置選項 構成。這個過程的傳回集是 行集合,該行集合 來自 與将要被移動的表相關的 協定表 。

這個存儲過程使用下列的術語(terminology):

源表source table

目标表target table

階段表staging table

有兩種等效的方式來啟動admin_move_table存儲過程:

使用第一種方法來修改 目标表的表定義的一些确定的内容。例如,如果你有一個表,表定義很大(幾kb大小),但是你想做的僅僅是修改表的表空間,你可以這樣做:不一定要決定需要去重建源表的整個create table 語句,你隻需要找出data_tbsp(資料表空間)、索引表空間和大對象表空間參數,讓其他的可選參數為空。

第二種方法讓你有更多控制性和靈活性。你可以預先建立目标表,而不是讓存儲過程建立目标表。通過預先建立目标表,你可以建立一個使用第一種方法不能建立的表。

第一種方法的文法:

第二種方法的文法:

方法的模式名為: sysproc.

index_tbsp

lob_tbsp

organize_by_clause

partkey_cols

data_part

coldef

這個參數提供一個移動過程中被用來作為目标表的已經存在的表名稱。可以對傳入的目标表做下面的改變:

資料、索引和大對象表空間可以被改變

多元列 specification 可以被添加或改變(multi dimensional column (mdc) )

分區鍵列 specification 可以被添加或删除

資料分區specification 可以被添加或删除

資料壓縮可以被添加或删除

可以指定一個新的列定義;但是 當指定一個列定義參數 有 相同的限制(however the same restrictions as when specifying the coldef parameter apply here).

下面的限制适用于命名的表:

表與源表必須存在于同一模式下

空必須是空表

類型表(typed tables), 物化查詢表, staging tables, 遠端表或叢集表是不允許的

表不能有參考限制(reference constraints)

如果這個參數被設定為null或空字元串,存儲過程使用與源表相同的表定義。大小寫敏感,varchar(128).

這個輸入參數是 由逗号分隔開的字元串,這個字元串定義了存儲過程用到的選項。選項的清單大小寫不敏感,varchar(32672)。可以為null或空字元串。下面的值是有效的:

這個選項以一個不同的表名稱來保留原始表的備份,如果源表的名稱是t1,那麼在資料移動完成之後表會被自動重命名為類似 t1aaaavxo的名稱.在傳回的協定表中你可以得到這個準确的名稱, under the original key。 you may set this option at any point up to and including the swap phase.

如果你為copy_use_load指定任何的load選項,admin_move_table 使用一個admin_cmd load來将資料從源表拷貝到目标表中,如果你不指定任何選項,那麼 nonrecoverable 選項 db2load api被用來 将資料從源表拷貝到目标表中。 在早于db2 9.7的版本中, 如果使用了copy_use_load那麼就一定要指定 force選項。

messages on server

copy yes

聲明loaded 資料的一份拷貝是否要儲存。如果前滾恢複被禁用那麼這個選項無效

nonrecoverable

這個選項在拷貝源表之前先建立索引,相對來說,預設是拷貝表完成之後建立索引的。這個選項的好處就是拷貝之後建索引時每個索引都需要進行一次全表掃描,而索引建立是一個事務需要活動日志空間。 如果 logindexbuild資料庫配置參數是打開的,建立索引 将導緻在短時間内需要大量的日志空間。這個選項的一個缺點就是拷貝性能會被降低因為需要同時維護目标表上的索引。同時,得到的索引 包含很多已經标記删除了的鍵,索引不像 拷貝之後建的索引 那樣保持平衡。你可以 在任何時候包括swap階段 設定copy_with_indexes 選項 .

如果設定了force option,swap 段将不會檢查 源表的表定義是否發生改變。 在db29.7版本之前,如果使用了copy_use_load,force選項必須被指定。你可以 在任何時候包括swap階段 設定這個選項.

這個選項不啟用目标表的runstats或其他統計拷貝。如果你使用了auto_runstats資料庫配置參數,db2随後将自動建立新的統計資訊。針對回退能力(for backwards compatibility),stats_no 也接受。你可以 在任何時候包括swap階段 設定這個no_stats選項.

這個選項在執行交換之前 從源表拷貝統計資訊 到目标表 。 這可能引起不準确的實體統計,尤其是在頁大小改變的情況下。但是,設定這個選項可以節約計算時間 因為将不會調用runstats來計算新的統計資訊。當然,優化器可能會選擇相同的通路計劃,因為統計資訊時相同的。倒過來書寫的順序 stats_copy 也可以識别。你可以 在任何時候包括swap階段 設定 stats_copy 選項.

這個選項阻止表的自動重新生效,取而代之的是,重建所有觸發器和視圖。隻可以在init階段 設定 no_auto_reval選項。

reorg

這個選項在執行交換之前 在目标表上 建立了一個額外的離線reorg。如果你使用這個選項來改善你的 壓縮字典, 建議使用預設 樣闆 方法 是一個更好的建立 最佳壓縮字典的方法(if you use this option to improve your compression dictionary, be advised that using the default sampling approach is a better method to create an optimal compression dictionary.)。但是,如果你需要一個最佳的xml壓縮字典,reorg是唯一的方法。你可以 在任何時候包括swap階段 設定reorg選項 在任何時候包括swap階段。

no_target_locksize_table

這個選項 在copy和swap階段 不保留 目标表上的 locksize表選項。 預設在目标表上 使用locksize表選項 來組織鎖overhead, 當沒有在目标上指定唯一索引時。

this option reads the data from the source table with an order by clause when a copy index has been specified using admin_move_table_util, a clustering index exists on the source table or a unique index or primary key is defined in the source table.

注:

a copy index will override a clustering index; a clustering index will be used in preference to a primary key; a primary key will be used in preference to a unique index.

non_cluster

load_msgpath

this option can be used to define the load message file path.

load_msgpath can be used together with copy_use_load in the following situations:

copy_use_load is specified without further options (in other words, the default copy_use_load nonrecoverable is assumed)

copy_use_load nonrecoverable is specified explicitly

load_msgpath cannot be used together with copy_use_load when optional keywords are also specified with copy_use_load.

if the load_msgpath option is not specified, then the default path is determined by the db2_util_msgpath registry variable.

load_temppath

use this option to define the load temporary file path.

this option is available starting in db2 version 10.1 fix pack 4.

allow_rlbac

use this option to move a table that has row-level label-based access control (lbac) security label specified. before you use this option, ensure that you have adequate privilege; if you lack the privilege to access all the table row entries, data loss can occur.

this input parameter specifies which operation the stored procedure is to execute. there are two ways of calling the stored procedure: using the move command to execute all the operations at one time; or by using the individual commands to execute the table move one step at a time. the main advantage of this second method is that you control when the swap phase actually occurs, thereby determining when the table is briefly taken offline. this allows you to make the move during a period of low system activity. if you use the individual commands, they must be called in the following order: init, copy, replay, verify (optional), and swap.

move: performs the entire table move (init, copy, replay, and swap operations) in one step.

init: 驗證能夠進行表移動,然後 初始化所有 在表移動過程中 需要的資料 (目标表、staging表、源表上的觸發器)

copy: 将内容從源表拷貝到目标表中去。在這個拷貝的階段中發生的對源表的更改(update、delete、insert) 存儲到staging table中,在拷貝階段結束時建立新索引,除非指定了copy_with_indexes選項。當然,如果需要的話,會在源表和目标表上建立輔助索引來改善relay階段的性能。隻能夠在init階段完成後使用copy操作。

redirect: forwards changes directly to the target table instead of capturing the changes in the staging table.

note

revert: reverts to the original behavior wherein the staging table captures the changes.

replay: copies into the target table any rows that have changed in the source table since the copy phase began. replay can be used only after the copy phase has completed.

verify: 可選擇的,檢查源表的内容和目标表内容是否一緻。這個過程會獲得源表和目标表上的s鎖, 重放發生在源表上的更改,然後進行比較。如果表有一個唯一索引,這個指令比較兩個表中都有的列的所有值。否則,這個指令比較 兩個表中形同的列的值(除了long、lob和xml列)。這個操作的代價比較大,注意應該小心的決定這個比較對于你的移動來說是否是必要的。verify隻能在copy和relay階段之後調用。

swap: executes the replay phase until the number of changes applied during the last scan of the staging table is less than the replay_threshold value stored in the protocol table. the source table is then taken offline briefly to finish the final replay, and then this command swaps the source table with target table and brings the table back online. swap can be used after the copy phase has completed, but ideally after the replay phase has been called.

cleanup: drops the staging table, any non-unique indexes or triggers created on the source table by the stored procedure, and the source table if the keep option has not been set. cleanup can be called if the command failed during the swap phase.

cancel: cancels a multi-step table move while between phases, or cancels a failed table move operation. executing this command requires that the operation status is not in completed or cleanup state. cancel clears up all intermediate data (the indexes, the staging table, the target table, and the triggers on the source table).

你必須有sqladm或dbadm權限來引用admin_move_table存儲過程。你必須有合适的對象建立權限,包括在源表上執行select語句、在目标表上執行insert語句的權限。

在一個非嚴格的資料庫中,當存儲過程被自行建立時,execute特權被賦權為public。

這個執行個體使用第一種放調用存儲過程, 這種方式 目标表在存儲過程中定義,移動一個位于模式svalent1下的名為t1的表。

下面是這個過程的一個輸出的示例:

下面這個例子使用第二種方式調用了存儲過程,這種方式裡面目标表是外部建立的,然後 傳入target_tabname參數,用來移動同名表,和第一個例子中一樣:

(1).第一步:首先手工建立表:

(2).然後調用存儲過程傳入目标表的名稱:

避免同時移動到相同的表空間,這樣可以避免目标表上的空間碎片。

在表上的低活期運作該過程。避免使資料負載聚集或删除,進而并行讀通路不會造成問題(avoid mass data loads or deletes so that parallel read access is not a problem) .

使用多步驟移動操作。init和copy階段可以在任何時候調用。多次執行relay階段來保持staging table的表在較小的水準,然後在表業務的低峰期執行swap操作。

檢查确認是否對你的表移動需求來說離線移動是一個更好的辦法,尤其是 要顧慮 沒有唯一索引的表和沒有索引的表。

存儲過程依賴觸發器來捕獲源表上的更改。但是有一些對表的更改操作不會觸發觸發器。這将導緻源表和目标表之間的不一緻,而且不容易被存儲過程探測到,這些操作包含:

truncate table (without restrict when delete triggers)

import ... replace into ...

load table

alter table

reorg (both online and offline)

這些在源表上的操作是受限的,并且使用一個新的表級别的狀态标簽。這個标簽被 在init階段 設定 ,在cleanup或cancel階段被清空。受限操作會失敗,傳回碼:sql0668n reason code 10 (sqlstate 57016).

在移動正在進行的時候,有一些操作會引起存儲過程的失敗。這些操作包括:

删除 systoolspace (系統工具)表空間

删除或重命名

删除或重命名任何由otm在init階段建立的臨時對象(目标表,階段表,源表上的觸發器,協定表)

修改協定表中非使用者可配置的值

為避免建立臨時對象時的命名沖突,使用下面的命名慣例:

postfix字尾

"t" 目标

"s" for staging

"o" 原始的,初始的

"g" for generated

"i" for insert trigger

"d" for delete trigger

"u" for before update trigger

"v" for after update trigger

names are built consisting of <code>&lt;characters from name of object&gt;&lt;base64 encoded hash key over name of object&gt;&lt;postfix&gt;</code>.

if length of name would exceed object length (128 bytes) <code>&lt;characters from name of object&gt;</code>gets shorter.

hash value gets calculated from the object name and is encoded similar to base64 encoding.

  可通過使用 admin_move_table 過程來以聯機或脫機方式移動表。如果相對于成本、空間、移動性能和事務開銷,您更重視可用性,請使用表的聯機移動而不是表的脫機移動。

  請確定有足夠的磁盤空間來容納表和索引的副本、登台表以及附加日志條目(accommodate the copies of the table and index, the staging table, and the additional log entries.)。

  可通過調用存儲過程一次或多次來以聯機方式移動表,該過程執行的每個操作都對應一次調用。使用多個調用允許您有其他選擇,例如,取消移動或控制何時使目标表脫機以進行更新。

  當調用 sysproc.admin_move_table 過程時,會:

建立源表的影子副本(shadow table) 在哪裡?

在複制階段期間,會使用觸發器來捕獲對源表的更改(更新、插入或删除)并将其放置到staging table中

當複制階段完成後,會對影子副本重放登台表(staging table)中捕獲的更改。

存儲過程會迅速使源表脫機并将源表名稱和索引名稱指定給影子副本及其索引。這一步中的鎖定情況,以及索引的重組情況?

然後,使影子表聯機,進而替換源表。預設情況下,會删除源表,但可以使用 keep 選項來以另一個名稱保留該源表。

注意:對于不帶索引(特别是唯一索引)的表,請不要執行聯機移動。對不帶唯一索引的表執行聯機移動可能導緻死鎖和複雜或費用很高的重放。 就是說,如果要執行聯機索引,那麼最好是唯一索引,沒有唯一索引的表執行聯機移動可能導緻死鎖和複雜或費用很高的重放,為什麼?

  用于存放對源表的沖突鎖定的應用程式可能會失敗,且傳回原因碼 sql0911n 68,因為 admin_move_table 更有可能會在鎖定逾時沖突中成功。要在 swap 操作期間防止死鎖,可使用 force_all 選項。請參見 force_all 以擷取更多詳細資訊。

要以聯機方式移動表,請執行下列步驟:

1.采用下列其中一種方式調用 admin_move_table 過程:

調用 admin_move_table 過程一次,并至少指定源表的模式名稱、源表名稱以及操作類型 move。例如,使用以下文法将資料移到同一表空間内的現有表中:

調用 admin_move_table 過程多次(每個操作都對應一次調用),并至少指定源表的模式名稱、源表名稱以及操作名稱。例如,使用以下文法将資料移到同一表空間内的新表中:

  其中 operation name 為下列值之一:init、copy、replay、verify 和 swap。必須按照此操作順序調用該過程,例如,在第一次調用中必須指定 init 作為操作名稱。

2.如果聯機移動失敗,那麼重新運作該移動:

解決導緻表移動失敗的問題。

通過查詢 systools.admin_move_table 協定表以了解狀态來确定表移動失敗時正在進行的階段。

再次調用存儲過程,并指定适用選項:

如果過程的狀态為 init,那麼使用 init 選項。

如果過程的狀态為 copy,那麼使用 copy 選項。

如果過程的狀态為 replay,那麼使用 replay 或 swap 選項。

如果過程的狀态為 cleanup,那麼使用 cleanup 選項。

如果表的聯機移動狀态不是 completed 或 cleanup,那麼可以通過對存儲過程指定 cancel 選項來取消該移動。

示例 1:在不使 t1 脫機的情況下,将 t1 表從模式 svalenti 移到 accounting 表空間中。指定 data、index 和 long 表空間以将表移動到新的表空間。

示例 2:在不使 t1 脫機的情況下,将 t1 表從模式 ebabani 移到 accounting 表空間中,并且在移動後保留原始表的副本。使用 copy_use_load 和 load_msgpath 選項來設定裝入消息檔案路徑。指定 data、index 和 long 表空間以将表移動到新的表空間。原始表将保持類似于 'ebabani'.'t1aaaavxo' 的名稱。

示例 3:在同一表空間中移動 t1 表。将 t1 中的 c1 列(使用不推薦的資料類型 long varchar)更改為使用相容的資料類型。

示例 4:您通過以下語句建立 t1 表:

在同一表空間内移動該表,并删除列 c5 和 c6:

示例 5:您的範圍分區表具有表空間 ts1 和 ts2 中定義的兩個範圍。将該表移至表空間 ts3,但保留 ts1 中的第一個範圍。

将 t1 表從模式 ebabani 移至 ts3 表空間。指定分區定義。