學習DBMS (二) 預覽

來源:互聯網
上載者:User

純個人筆記,只為個人記憶,所以表述很不嚴謹!

最近看書看的頭痛,就隨便翻了一下<<PL/SQL Packages and Types Reference 10g Release 2 (10.2)>>,隨便的記了一下筆記,因為只是為了自己記憶方便,所以很多地方表述的都不嚴謹,如果是誰不小心看到了,望見諒!

其實一些個包比如DBMS_STATS,DBMS_JOB,dbms_profiler,DBMS_SPACE的若干過程,函數還是經常用到的,但卻從來沒有仔細的看過這些個包的使用文檔,這次也算是小小的彌補了一下.

如果你只是一個DBA的話,其實一些個DBMS包是不需要你去記住的,比如說DBMS_DATAPUMP,其實EXPDP,IMPDP就是調用DBMS_DATAPUMP,DBMS_METADATA來實現的,所以其實你不需要去理解DBMS_DATAPUMP,只需要理解EXPDP,IMPDP這些個工具如何使用就可以了.再比如dbms_debug,dbms_trace是pl/sql developer,toad這些廠商調用的包,其實很多時候你只需要理解這些個工具如何使用就可以了!

dbms_application_info(看了)
dbms_job(看了) 不能停止一個正在啟動並執行JOB,breaken就是DISABLE,但如何重新ENABLE呢?WHAT=>'zsj_test_proc();',注意最後的;是必須的.
dbms_lock(看了) dbms_lock.sleep(60) sleep 1 分鐘
dbms_logmnr_d: V$logmnr_contents 包dbms_logmnr_d抽取LOGMNR資料字典資訊用於將來的分析(因為可能分析時某個對象在DB中已經不存在了);在另一個資料表空間中重建LOGMNR表
dbms_metadata get_ddl('TABLE','EMP','SCOTT')只能得到一個單獨的對象的定義
dbms_monitor
dbms_output: SQL*PLUS下set serveroutput on size 99999之後就不必調用dbms_output.enable(99999)了,但dbms_output.enable()不能代替set serveroutput on;
dbms_profiler(看了)
dbms_random(看了)
dbms_resumable(看了)
dbms_scheduler(看了) stop_job停止正在啟動並執行JOB
dbms_server_alert(看了) :沒有使用者空間配額使用的dbms_server_alert設定嗎?如果知道的,看到了麻煩回複一下,先謝謝了!
dbms_session(看了)
dbms_shared_pool(看了)
dbms_space:理解其中的含義,分配的盤區(TOTAL_BLOCKS),HWM(TOTAL_BLOCKS-UNUSED_BLOCKS),HWM下的空閑塊(FREE_BLOCKS可用於插入資料的塊).Last Used Ext BlockId:段頭塊(不能用來插入資料的),Last Used Block:總共使用的塊數(包括段頭塊)
dbms_space_admin(大致看了看)
dbms_tts(看了)
dbms_workload_repository(看了)
dbms_xplan(看了)
dbms_utility(看了)
dbms_trace(看了)
dbms_rowid(看了),注意object_number指的是data_object_id,而不是object_id(剛開始時,兩者是一樣的,但truncate table時,data_object_id+1,object_id不變)
dbms_system,dbms_standard(不提供文檔支援嗎??????)

dbms_advisor,dbms_sqltune,dbms_stats:這三個主要的包倒沒有看,因為實在是太多了,以後一定補上!

 

dbms_ddl的一個使用例子:
BEGIN
FOR X IN(SELECT OBJECT_NAME,OBJECT_TYPE FROM USER_OBJECTS WHERE STATUS='INVALID')
loop
BEGIN
Dbms_Ddl.alter_compile(x.object_type,NULL,x.object_name);
EXCEPTION
WHEN OTHERS THEN
Dbms_Output.PUT_LINE('無法編譯'||x.object_type||':'||x.object_name);
END;
END LOOP;
END;
/
當然ORACLE不建議使用這個過程,如果要是編譯整個模式對象的話,可以使用dbms_utility.compile_schema.

 

 

dbms_utility:
comma_to_table:將以逗號分割的列錶轉化為字元table
compile_schema:編譯特定模式下的所有procedure,function,trigger,package body
get_cpu_time
get_dependency
get_hash_value
get_time
table_to_comma

declare
v_name dbms_utility.lname_array;
v_list varchar2(2560);
v_len number;
begin
select ename bulk collect into v_name from scott.emp where deptno=10;
dbms_utility.table_to_comma(v_name,v_len,v_list);
dbms_output.put_line(v_list);
end;
/

 

 

 

 

dbms_alert的使用一個例子:
alert是基於事務的,也許發出alert的會話發出的alert會很多,但等待會話只接受到發出alert的會話commit前最後發出的alert,以前的alert都被拋棄了;如果在signal之後事務被rollback了,那麼沒有alert發出.多個session可以並發的往同一個alert中signal,但它們是串列執行的,也就是說直到一個會話commit,另一個會話的signal才能成功!是多讀者的.
DBMS_PIPE是獨立於事務的.一個message一旦被接受,它就從pipe中被刪除,所以一個message只能被接受一次.也就是說是一個單消耗者的.private pipe只有建立者使用者和sysdba特權使用者可以訪問.
create or replace trigger zsj_alert_trigger
after insert or delete on test
declare
v_count number;
begin
select count(1) into v_count from test;
if(inserting) then
dbms_alert.signal('zsj_alert','inserting:'||v_count);
elsif(deleting) then
dbms_alert.signal('zsj_alert','deleting:'||v_count);
end if;
end;
/

CREATE TABLE zsj_alert_table(TIME DATE,message VARCHAR2(256));

declare
v_message varchar2(256);
v_status number;
begin
dbms_alert.register('zsj_alert');
while(1=1)
loop
dbms_alert.waitone('zsj_alert',v_message,v_status);
if(v_status=0) then
insert into zsj_alert_table values(sysdate,v_message);
commit;
SELECT LTrim(RTrim(translate(v_message,'insertingdeleting: ',' '))) into v_status FROM dual;
exit when(v_status=0);
end if;
end loop;
dbms_alert.remove('zsj_alert');
end;
/

 

dbms_pipe的一個使用例子:
declare
v_status number;
begin
v_status:=dbms_pipe.create_pipe('zsj_test_pipe');
if(v_status=0) then
dbms_pipe.reset_buffer();
dbms_pipe.pack_message('I Love Myself!');
dbms_pipe.pack_message(6688);
v_status:=dbms_pipe.send_message('zsj_test_pipe');
end if;
end;
/

declare
v_str varchar2(50);
v_num number;
v_status number;
begin
dbms_pipe.reset_buffer();
v_status:=dbms_pipe.receive_message('zsj_test_pipe');
if(v_status=0) then
dbms_pipe.unpack_message(v_str);
dbms_pipe.unpack_message(v_num);
v_status:=dbms_pipe.remove_pipe('zsj_test_pipe');
dbms_output.put_line(v_str||','||v_num);
end if;
end;
/

 

 

 

dbms_trace的使用:
首先必須以sys使用者運行$ORACLE_HOME/rdbms/admin下的tracetab.sql,然後使用pl/sql developer等第三方工具就可以了,這裡要注意 ENABLED的意思:
alter session set plsql_debug=true; create or replace ***; 這裡的建立的程式單元算是Enabled的,是要被跟蹤的.
或者alter [PROCEDURE | FUNCTION | PACKAGE BODY] <libunit-name> compile debug;這裡的程式單元算是Enabled的,是要被跟蹤的.而跟蹤ALL的時候,產生的資料量太大了.注意它只跟蹤本會話內set_plsql_trace和CLEAR_PLSQL_TRACE之間ENABLED或者是ALL的東西,並不是說可以在全域啟動,然後跟蹤ALL的會話或者說這些會話中ENABLE的東西,不是這樣的.

exec dbms_trace.set_plsql_trace(dbms_trace.trace_enabled_sql);
exec dbms_trace.CLEAR_PLSQL_TRACE();
select sys.plsql_trace_runnumber.currval from dual;

 

 

 

dbms_profiler的使用:(也可以使用第三方工具)
首先運行$ORACLE_HOME/rdbms/admin下的profload.sql建立DBMS_PROFILER包,proftab.sql建立儲存表,序列,公用同義字

指令碼:
set heading off
set pagesize 0

--啟動profiler
declare
v_status number;
v_runid number;
begin
v_status:=DBMS_PROFILER.START_PROFILER(sysdate,'',v_runid);
if(v_status!=0) then
dbms_output.put_line('啟動失敗!錯誤碼:'||v_status);
else
dbms_output.put_line('啟動成功,RUNID:'||v_runid);
end if;
end;
/

--下面是你的測試代碼,測試的應該是預存程序,函數等,不應該是匿名塊
BEGIN
zsj_compile_proc();
END;
/

--關閉profiler:
select decode(DBMS_PROFILER.STOP_PROFILER,0,'關閉成功!','關閉失敗!') from dual;

--flush_data:
select decode(DBMS_PROFILER.flush_data,0,'flush_data成功!','flush_data失敗!') from dual;

undef runid
undef owner
undef name
set verify off
set linesize 200
set pagesize 0
spool d:zsj_profile_out.txt
select s.line "Line", p.total_occur "Occur", p.total_time "Msec", s.text "Text"
from  all_source s,
(select u.unit_owner, u.unit_name, u.unit_type, d.line#,
d.total_occur, d.total_time/1000000 total_time
from  plsql_profiler_data d, plsql_profiler_units u
where u.runid = &&runid
and  u.runid = d.runid
and  u.unit_number = d.unit_number) p
where s.owner = p.unit_owner (+)
and  s.name = p.unit_name (+)
and  s.type = p.unit_type (+)
and  s.line = p.line# (+)
and  s.name = upper('&&name')
and  s.owner = upper('&&owner')
order by s.line;
select exec.cnt/total.cnt * 100 "Code% coverage"
from (select count(1) cnt
from plsql_profiler_data d, plsql_profiler_units u
   where d.runid = &&runid
and u.runid = d.runid
   and u.unit_number = d.unit_number
  and u.unit_name = upper('&&name')
   and u.unit_owner = upper('&&owner')) total,
(select count(1) cnt
  from plsql_profiler_data d, plsql_profiler_units u
  where d.runid = &&runid
    and u.runid = d.runid
    and u.unit_number = d.unit_number
    and u.unit_name = upper('&&name')
    and u.unit_owner = upper('&&owner')
    and d.total_occur > 0) exec;
undef runid
undef owner
undef name

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.