1. 程式人生 > >資料割接筆記

資料割接筆記

--exprot date from oracle
exp tbcy/[email protected] file=OM_chetian.dmp log=20.log tables=chey indexes=n GRANTS=n CONSTRAINTS=n TRIGGERS=n
--import date to oracle(ignore:已經存在的表忽略)
imp com/[email protected] file=OM_chetian.dmp log=20.log tables=chey indexes=n GRANTS=n CONSTRAINTS=n TRIGGERS=n ignore=y
------------------------------------------------------------------------------------------------------------------
刪除表:drop lc_chetest purge;--正真刪除表
併發建立索引:
create index inx_che_oid on loc_chetest (oid) tablespace d_log_01 nologging parallel 16;
關閉併發:alter index inx_che_oid noparallel;

併發建立臨時表:
create table inf_che_all_his_1010 tablespace d_space nologging parallel 16 as
select b.oid,b.name, b.addr, b.phone,'0' status, sysdate his_date,0 deal_flag, 0 limitflag
from sducy.inf_che_all b
where exists ( select 1 from sducy.user_account c
where c.acctid=b.acctid);
關閉併發:
alter table inf_che_all_his_1010 noparallel;

說明:
新老庫表資料割接,把老庫的資料割接到新庫中。
1.表字段相同:直接exp老表資料,然後imp老表資料到新庫中(或者c++直接跨庫搬遷資料)
2.表字段不同:
2.1).新核對新老表欄位對映關係,資料對映關係表格。
2.2).老庫需要多張表對映一張新表資料的,根據表關係,查詢多個表關聯查詢,組合成一張臨時新表(新增處理資料狀態:flag)
2.3).exp臨時新表,然後再imp老表資料到新庫
2.4).迴圈編譯新庫臨時表待處理資料,獲取正式表的欄位資訊插入到正式表中,更臨時表處理狀態(0:待處理;1:處理成功;5:資料已經存在重複;9:處理失敗)
------------------------------------------------------------------------------------------------------------------
樣例:
1支付關係表 2.已經割接資料表 3.賬號表 4.使用者表

要求:1.割接支付關係表到新庫中
2.前期有小批量割接資料
3.確保支付關係中的賬戶是有效的。
4.確認賬號是有使用者在使用的。(即:有人使用的有效賬號,這樣的支付關係才需要割接)

other說明:1)由於多表關聯,且都是大表,即使開併發也處理不動,將多表關聯拆成多次關聯,建立臨時表,提高效率。

步奏:
1.撈取支付關係表,先過濾已經割接資料
create table 臨時表1 tablespace d_space nologging parallel 16 as
select b.oid,b.name, b.addr, b.phone,'0' status, sysdate his_date,0 deal_flag, 0 limitflag
from 支付關係表 b where not exists ( select 1 from 已經割接表 c
where c.payoid=b.payid);
--關閉臨時表1併發
2.撈取臨時表1,過濾賬號id不存在的資料,建立臨時表2
create table 臨時表2 tablespace d_space nologging parallel 16 as
select b.*
from 臨時表1 b where exists ( select 1 from 賬號表 c
where c.acctid=b.acctid);
--關閉臨時表2併發

3.撈取臨時表2,過濾賬號不被使用者使用的資料,建立臨時表3
create table 臨時表1 tablespace d_space nologging parallel 16 as
select b.*
from 臨時表2 b where exists ( select 1 from 使用者表 c
where c.acctid=b.acctid
and active = 0);
--關閉臨時表2併發
4.為臨時表3建立處理狀態標識.
alter table 建立臨時表3 add flag varchar2(1);--也可以在建立臨時表是,直接新增flag

5.將臨時表3匯出,在匯入到新庫中

6.寫一個塊程式,撈取臨時表,入庫到線網業務表。
declare
n_commitcnt := 0;--5000
begin
n_commitcnt :=0;
for cur in (select rowid,t.* from 臨時表 where flag =0 )--or flag is null
loop
begin
--引數校驗處理,如時間校驗null,資料唯一性校驗等
--當資料重複時,update 臨時表 falg=5 where rowid = cur.rowid;
.....
insert 線網表 ;

更新臨時表處理標識
update 臨時表 falg=1 where rowid = cur.rowid;

if n_commit >= 500 then
n_commit := 0;
end if;
exception
when others then
update 臨時表 falg=9 where rowid = cur.rowid;
end;

end loop;

commit;
end;
/
說明:1)臨時表資料量大,無索引,更新時使用rowid提供執行效率。

7.割接資料處理結果跟蹤
select /*+ parallel 10*/flag,count(*) from 臨時表 group by flag;

8.當線網表資料無唯一性約束時,可能存在相同資料的重複資料,需要進行去重處理(分兩步)
8.1):撈取重複資料:
create table mg_重複資料 nologging tablespace d_user parallel 16 as
select users_id ,0 flag
from user t
where rowid >(select mix(rowid)
from user a
where a.user_id = t.user_id
and t.regon = t.regon);
8.2):刪除重複資料
註釋:由於資料量較大,採取開通道0-19 ,並行處理提高效率。
declare
n_commitcnt := 0;--5000
v_count number(6);
v_userid varchar(32);
begin
n_commitcnt :=0;
v_count :=0;
v_userid :='';
for cur in (select rowid,t.* from mg_重複資料 where flag =0
and mod(user_id,20) = 0)--開通道0-19 ,並行處理提高效率。
loop
begin
--資料唯一性校驗等
select count(*) into v_count from user where user_id = cur.user_id
and inst_id = '88888888';
--
if v_count>0 then
delete user where user_id = cur.user_id
and inst_id = '88888888';

else
delete user t
where rowid <>(select max(rowid)
from user a
where a.user_id = cur.user_id
and t.regon = cur.regon);
end if;
insert 線網表 ;

更新臨時表處理標識
update mg_重複資料 falg=1 where rowid = cur.rowid;

if n_commit >= 500 then
n_commit := 0;
end if;
exception
when others then
update mg_重複資料 falg=9 where rowid = cur.rowid;
end;

end loop;

commit;
end;
/
8.3檢查結果
select /*+ parallel 10*/flag,count(*) from mg_重複資料 group by flag;


其他:
塊邏輯卡死問題查詢,根據執行計劃,思考優化方法,優化效能sql
--登陸sql使用者,查詢執行sql,檢視一直卡死的sql_id
select a.sql_id,a.SQL_TEXT,t.EVENT,t.*
from v$session t, v$sql a
where t.sql_id = a.sql_id
and t.OSUSER='cn_xiaocai';
--根據sqlID——查詢執行計劃
select * from table(dbms_xplan.display_cursor('fkduybquzyq',0,'typical'));