顯示具有 PLSQL 標籤的文章。 顯示所有文章
顯示具有 PLSQL 標籤的文章。 顯示所有文章

星期三, 6月 26, 2013

[PLSQL] 使用cursor 與迴圈 刪除表格資料

前言:

    一般說來, 刪資料有delete table, truncate兩種, 如果要刪部分資料,只能用delete table的方式了。

    為何要寫另一個暫存索引表格test.usersid_pk 來刪資料?
因為線上的real.users 表格直接刪5筆檔案仍會hang...已經很確定瞬間沒有任何table lock...

#針對test.users pk (ID) 欄位 , 建一個暫存的index 表格

create table test.usersid_pk tablespace users_bck storage (initial 4M next 4M)  as (select id from test.users where DATE < '20130301')
alter table test.usersid_pk add (seq number);

#建立一個sequence for test.usersid_pk(seq) 欄位
CREATE SEQUENCE test.id_seq MINVALUE 1 START WITH 1 INCREMENT BY 1;


#針對括欄後的test.usersid_pk 表格, 每一筆資料都按照序號發號碼牌
declare
 v_counter number:=0;
cursor cur is
select seq from test.usersid_pk  for update;
BEGIN

for c_id in cur loop
UPDATE test.usersid_pk

 SET seq = (test.id_seq.nextval );
 IF MOD( v_counter, 1000) = 0 THEN    -- Commits once per 1000 records
 commit;

 END IF;
  v_counter:=v_counter + 1;
END LOOP;
commit;
END;
/

#使用cursor loop內真正的刪除資料於 real.users 表格
#select count(*)  test.usersid_pk = 345643
sqlplus "/ as sysdba" <<EOF
spool del_usr_tab.log
set timing on
set time on
set echo on
set serverout on
DECLARE
   v_myid number:=0;
   BEGIN
   for v_number in 1..345643 loop
      select myid into v_myid from test.usersid_pk where seq = v_number;
       delete from real.users where id= v_myid;
       IF MOD( v_number, 25 ) = 0 THEN    -- Commits once per 25 records
          commit;
       END IF;
     end loop;
   END;
/
commit;
EOF

後來看到網路上也有使用rowid來刪資料的例子~
感覺這應該比較快~
http://blog.xuite.net/charley_ocp/mydba01/40532189

星期四, 6月 20, 2013

[PLSQL] 每十筆delete 資料的做法~ FYI

spool del_usr_tab.log
sqlplus "/ as sysdba" <<EOF
set timing on
set time on
set serverout on
DECLARE
     v_counter number:=0;
   BEGIN
   for v_number in 1..100000 loop
         delete from test.users where DATE < '20130301' ;
         if (  MOD(v_number ,10 )  = 0 ) then         
           commit;
            DBMS_OUTPUT.PUT_LINE( sysdate);
        end if;
     end loop;
   END;
/
commit;
EOF

星期一, 4月 23, 2012

[PLSQL] 使用迴圈大量建立使用者

參考資料 : http://abu.tw/2010/04/plsql-table-oracle-array-like.html

set serveroutput on
DECLARE 
  -- 宣告Array TYPE 及變數 
  TYPE t_name_array IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER; 
  name_array          t_name_array; 
BEGIN 
  -- 塞值進 Array  
  name_array(1) := 'USER1'; 
  name_array(2) := 'USER2'; 
  -- 迴圈處理 
  FOR i IN 1..name_array.COUNT LOOP
    -- 建立表格空間
    DBMS_OUTPUT.put_line( 'create tablespace '|| name_array(i) ||'_TBS datafile ''D:\oracle\oradata\wgopd\'||LOWER(name_array(i))||'_tbs.dbf'' size 16M autoextend on next 64M;');  
    -- 新增使用者
    DBMS_OUTPUT.put_line( 'create user '|| name_array(i) ||' identified by '||name_array(i)||'520 default tablespace '||name_array(i)||'_TBS temporary tablespace temp;');
    -- 賦予權限
    DBMS_OUTPUT.put_line( 'grant connect , resource , dba to '|| name_array(i) ||' ;');
  END LOOP
 
END;

Output:

create tablespace USER1_TBS datafile 'D:\oracle\oradata\wgopd\user1_tbs.dbf'
size 16M autoextend on next 64M;
create user USER1 identified by USER1520 default tablespace USER1_TBS temporary
tablespace temp;
grant connect , resource , dba to USER1 ;
create tablespace USER2_TBS datafile 'D:\oracle\oradata\wgopd\user2_tbs.dbf'
size 16M autoextend on next 64M;
create user USER2 identified by USER2520 default tablespace USER2_TBS temporary
tablespace temp;
grant connect , resource , dba to USER2 ;

PL/SQL procedure successfully completed.

SQL>

LinkWithin-相關文件

Related Posts Plugin for WordPress, Blogger...