海量資料處理_表分割槽(分割槽自動維護與歷史分割槽歸檔)
1,分割槽自動維護
CREATE OR REPLACE PACKAGE partition_managment IS
/*======================================================*/
/* 增加分割槽 */
/*======================================================*/
PROCEDURE add_partition(p_tab_name IN VARCHAR2);
/*======================================================*/
/* 刪除分割槽 */
/*======================================================*/
PROCEDURE drop_partition(p_tab_name IN VARCHAR2);
END partition_managment;
/
CREATE OR REPLACE PACKAGE BODY partition_managment IS
PROCEDURE log(p_comm IN VARCHAR2, p_type IN VARCHAR2) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
/*INSERT INTO CLEAN_OUT_LOG
(WHEN, COMM, TYPE)
VALUES
(SYSDATE, P_COMM, P_TYPE);
COMMIT;
*/
dbms_output.put_line('info:' || p_comm || ' type:' || p_type);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
dbms_output.put_line('ERROR IN partition_management.LOG');
END log;
/*======================================================*/
/* 增加分割槽,增加新的分割槽 */
/*======================================================*/
PROCEDURE add_partition(p_tab_name IN VARCHAR2) IS
l_ddl VARCHAR2(200);
l_partition_name VARCHAR2(200);
l_split_date VARCHAR2(8);
l_partition_num INT;
BEGIN
--get partition name
SELECT to_char(add_months(SYSDATE, 2), 'YYYYMM') || '01',
'P' || to_char(add_months(SYSDATE, 1), 'YYYYMM')
INTO l_split_date, l_partition_name
FROM dual;
--does it exist?
SELECT COUNT(*)
INTO l_partition_num
FROM user_tab_partitions
WHERE table_name = p_tab_name
AND partition_name = l_partition_name;
IF l_partition_num > 0 THEN
log('partition ' || l_partition_name || ' 已經存在,skip', 'I');
RETURN;
END IF;
--create
BEGIN
l_ddl := 'alter table ' || p_tab_name ||
' split partition pmax at (to_date(''' || l_split_date ||
''',''YYYYMMDD'')) into (' || ' partition ' ||
l_partition_name || ', partition pmax)';
log('add new partition,ddl:' || l_ddl, 'I');
EXECUTE IMMEDIATE l_ddl;
log('add new partition ok,ddl:' || l_ddl, 'I');
EXCEPTION
WHEN OTHERS THEN
log('Error when create a new partition:' ||
substr(dbms_utility.format_error_stack, 1, 900),
'E');
END;
END add_partition;
/*======================================================*/
/* 刪除分割槽,每呼叫一次,刪除最早的一個超過13個月的分割槽 */
/*======================================================*/
PROCEDURE drop_partition(p_tab_name IN VARCHAR2) IS
l_ddl VARCHAR2(200);
l_partition_name VARCHAR2(200);
BEGIN
--find the oldest partition
BEGIN
SELECT partition_name
INTO l_partition_name
FROM (SELECT *
FROM user_tab_partitions
WHERE table_name = p_tab_name
ORDER BY partition_name)
WHERE rownum < 2;
EXCEPTION
WHEN OTHERS THEN
log('No partition found for table:' || p_tab_name, 'W');
END;
--is older than 13 month?
BEGIN
IF (add_months(SYSDATE, -13) >
to_date(substr(l_partition_name, 2) || '01', 'YYYYMMDD')) THEN
l_ddl := 'alter table ' || p_tab_name || ' drop partition ' ||
l_partition_name;
log('drop partition,ddl:' || l_ddl, 'I');
EXECUTE IMMEDIATE l_ddl;
log('drop partition ok:' || l_ddl, 'I');
ELSE
log('最早的分割槽不需要刪除:' || l_partition_name, 'I');
END IF;
EXCEPTION
WHEN OTHERS THEN
log('Error when drop partition:' ||
substr(dbms_utility.format_error_stack, 1, 900),
'E');
END;
END drop_partition;
END partition_managment;
/
2,歷史分割槽歸檔:
--create directory:
create directory ext_local as '/local/TEST';
create directory ext_store as '/store/TEXT';
--grant privilege:
grant read,write on ext_local to test;
grant read,write on ext_store to test;
--create external table:
create table ext_200201
organization external(type oracle_dump
default directory ext_local location('ext_200201.dmp'))
as
select /*+ full(orderitem) */ * from orderitem
where shipdate >= to_date('20020101','YYYYMMDD')
and shipdate < to_date('20020201','YYYYMMDD');
--drop table
drop table ext_200201;
--cp
cp -p ext_200201.dmp /store/TEST/
--drop partition
alter table orderitem drop partition pt200201;
--restore
create table ext_200201_c(
...)
organization external(type oracle_dump
default directory ext_store location('ext_200201.dmp'));
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18922393/viewspace-696995/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 海量資料處理_表分割槽
- oracle 交換分割槽歷史資料歸檔Oracle
- 資料表分割槽分割與刪除歷史資料
- 自動備份、截斷分割槽表分割槽資料
- 海量資料處理_表分割槽(線上重定義)
- oracle分割槽表的維護Oracle
- 【轉】Oracle分割槽表維護Oracle
- 分割槽表入無分割槽的資料庫資料庫
- Oracle分割槽表基礎運維-07增加分割槽(2 HASH分割槽)Oracle運維
- 如何查詢分割槽表的分割槽及子分割槽
- Oracle分割槽表基礎運維-07增加分割槽(3列表分割槽)Oracle運維
- Oracle分割槽表及分割槽索引Oracle索引
- INTERVAL分割槽表鎖分割槽操作
- oracle分割槽表和分割槽表exchangeOracle
- 全面學習分割槽表及分割槽索引(13)--分隔表分割槽索引
- Oracle分割槽表基礎運維-07增加分割槽(1範圍分割槽)Oracle運維
- Oracle分割槽表基礎運維-06分割槽表索引Oracle運維索引
- ORACLE分割槽表梳理系列(二)- 分割槽表日常維護及注意事項Oracle
- 全面學習分割槽表及分割槽索引(10)--交換分割槽索引
- PLSQL根據分割槽表的分割槽名批次truncate分割槽SQL
- 分割槽表truncate慢處理
- 全面學習分割槽表及分割槽索引(9)--刪除表分割槽索引
- 全面學習分割槽表及分割槽索引(11)--合併表分割槽索引
- 全面學習分割槽表及分割槽索引(12)--修改list表分割槽索引
- 學習筆記】分割槽表和分割槽索引——新增表分割槽(二)筆記索引
- Hash分割槽表分割槽數與資料分佈的測試
- oracle分割槽表和非分割槽表exchangeOracle
- 使用split對分割槽表再分割槽
- 簡單ORACLE分割槽表、分割槽索引Oracle索引
- rebuild分割槽表分割槽索引的方法Rebuild索引
- 分割槽表及分割槽索引建立示例索引
- Oracle分割槽表基礎運維-01分割槽表分類Oracle運維
- Oracle分割槽表基礎運維-04列表分割槽Oracle運維
- Hive的靜態分割槽與動態分割槽Hive
- Oracle分割槽表基礎運維-02範圍分割槽Oracle運維
- Oracle分割槽表基礎運維-03HASH分割槽Oracle運維
- Oracle分割槽表基礎運維-05組合分割槽Oracle運維
- Oracle分割槽表基礎運維-09刪除分割槽Oracle運維