Oracle 動態新增分割槽的實現方法

2021-08-15 14:28:27 字數 3545 閱讀 9909

oracle表分割槽目的:

在資料處理過程中,通常對於資料比較大的表進行分割槽管理,而分割槽的依據往往是資料日期,每一天或者每幾天資料儲存在乙個指定的分割槽中,當資料量一天天增加後,通過分割槽進行過濾,有利於快速查詢某一天的資料。

在向分割槽表中插入資料時,分割槽表必須有能夠裝載這條資料的分割槽,比如將2018-01-08的資料全部放在p20180102這個分割槽,而這個分割槽條件是資料日期小於等於2018-01-02,那麼這條資料日期為2018-01-08的資料就無法insert到這張表,這樣就會出現錯誤。

為了解決為分割槽表自動擴充套件分割槽的需求,我們編寫了乙個儲存過程,用來在向表中insert資料時,動態的對錶進行新增分割槽或清除分割槽。只需要在insert之前,執行下邊儲存過程即可。示例**如下:

create or replace procedure manage_table_partitions(

tname varchar2,

curdate date

) is

is_part_exists integer := 0;

is_table_exists integer := 0;

is_part_table integer := 0;

p_label varchar2(30) := to_char(curdate,'yyyymmdd');

max_partition_date date;

min_partition_date date;

target_table varchar2(40) := upper(trim(tname));

v_sql varchar2(3000) := '';

-- 定義異常型別變數

no_table_exception exception;

less_than_latest_exception exception;

-- 固定引數

add_freq integer := 1;

begin

-- 檢視這張表是否為分割槽表

select count(*) into is_part_table from user_part_tables

where table_name = target_table;

if is_part_table <> 1 then

select count(*) into is_table_exists from tab where tname = target_table;

if is_table_exists <> 1 then

dbms_output.put_line(tname||',這張表不存在');

raise no_table_exception;

end if;

dbms_output.put_line(tname||',這張表不是分割槽表,將直接清空表中資料');

v_sql := 'truncate table ' || tname;

execute immediate v_sql;

return ;

end if;

-- 檢視分割槽是否存在

select count(*) into is_part_exists

from user_tab_partitions

where table_name = target_table

and partition_name = 'p'||p_label

;if is_part_exists <> 1 then

-- 檢視分割槽表最大分割槽和最小分割槽

select

max(to_date(substr(partition_name,2),'yyyy-mm-dd'))

,min(to_date(substr(partition_name,2),'yyyy-mm-dd'))

into

max_partition_date

,min_partition_date

from user_tab_partitions

where table_name = target_table

group by table_name;

-- 檢查準備建立的分割槽是否小於當前表中分割槽最小日期

if min_partition_date > curdate then

dbms_output.put_line('資料日期已經小於分割槽表最小日期,請重建表,重新設定最小日期分割槽');

raise less_than_latest_exception;

end if;

dbms_output.put_line('新增分割槽,按照指定頻率新增分割槽');

max_partition_date := max_partition_date + add_freq;

while max_partition_date <= curdate loop

begin

v_sql := 'alter table '|| tname || ' add partition p' || to_char(max_partition_date,'yyyymmdd') || ' values less than ';

v_sql := v_sql || '(to_date(''' || to_char(max_partition_date + add_freq,'yyyy-mm-dd') ||''',''yyyy-mm-dd''))';

--dbms_output.put_line(v_sql);

execute immediate v_sql;

max_partition_date := max_partition_date + add_freq;

end;

end loop;

else

dbms_output.put_line('清除分割槽中的資料');

v_sql := 'alter table '||tname||' truncate partition p'||p_label;

dbms_output.put_line(v_sql);

execute immediate v_sql;

end if;

end manage_table_partitions;

上邊這段程式,預設情況下查詢的是使用者自己的表,如user_tab_partitions,user_part_tables,tab。所以,預設只能對使用者自己的表的分割槽進行動態擴充套件和分割槽資料清除。如果想要對其他使用者的表進行動態分割槽管理,需要將user_tab_partitions,user_part_tables,tab換成dba_tab_partitions,dba_part_tables,dba_tables,並且還需要有操作其他使用者下表的許可權。這樣會導致許可權放大,建議不要這麼操作。

如果各個使用者都需要使用動態分割槽擴充套件與清理,可以在每個使用者下邊部署這個儲存過程,這樣就不用跨使用者之間動態管理分割槽。

oracle新增分割槽

create table test msg id varchar2 16 result integer,ts varchar2 17 ts time timestamp 6 insert time date partition by range ts time partition p20180110...

oracle新增分割槽語句 按照子分割槽模板新增子分割槽

2019 12 11 11 27 上傳 如圖所示。這是建立 時的語句。已經有子分割槽模板了。環境 oracle11.2.0.4 我現在新增子分割槽時,不知道如何套用子分割槽模板,請各位大牛幫忙看下。我是採用很土的方法新增,需要8條如下語句 alter table coll cust balance ...

Linux 上新增分割槽,解除安裝分割槽

linux 新增分割槽,解除安裝分割槽 1 新增分割槽 1 通過 secure crt 使用root使用者登入遠端主機,如果是普通使用者先切換至管理員使用者。2 執行以下命令,檢視資料盤資訊。輸入命令fdisk l檢視您的資料盤資訊,注意 在沒有分割槽和格式化資料盤之前,使用df h 命令是無法看到...