系统包的使用
DBMS_PROFILER测试存储过程效率
profsum.sql
set echo off
set linesize 5000
set trimspool on
set serveroutput on
set termout off
column owner format a11
column unit_name format a14
column text format a21 word_wrapped
column runid format 999999
column secs format 99999.99
column hsecs format 99999.99
column grand_total format 9999.99
column run_comment format a11 word_wrapped
column line# format 99999
column pct format 9999.9
column unit_owner format a11
spool profsum.out
update plsql_profiler_units set total_time = 0;
execute prof_report_utilities.rollup_all_runs;
prompt =
prompt =
prompt ====================
prompt Total time
select grand_total/1000000000 as grand_total
from plsql_profiler_grand_total;
prompt =
prompt =
prompt ====================
prompt Total time spent on each run
select runid,
substr(run_comment,1, 30) as run_comment,
run_total_time/1000000000 as secs
from (select a.runid, sum(a.total_time) run_total_time, b.run_comment
from plsql_profiler_units a, plsql_profiler_runs b
where a.runid = b.runid group by a.runid, b.run_comment )
where run_total_time > 0
order by runid asc;
prompt =
prompt =
prompt ====================
prompt Percentage of time in each module, for each run separately
select p1.runid,
substr(p2.run_comment, 1, 20) as run_comment,
p1.unit_owner,
decode(p1.unit_name, '', '<anonymous>',
substr(p1.unit_name,1, 20)) as unit_name,
p1.total_time/1000000000 as secs,
TO_CHAR(100*p1.total_time/p2.run_total_time, '999.9') as percentage
from plsql_profiler_units p1,
(select a.runid, sum(a.total_time) run_total_time, b.run_comment
from plsql_profiler_units a, plsql_profiler_runs b
where a.runid = b.runid group by a.runid, b.run_comment ) p2
where p1.runid=p2.runid
and p1.total_time > 0
and p2.run_total_time > 0
and (p1.total_time/p2.run_total_time) >= .01
order by p1.runid asc, p1.total_time desc;
column secs form 99999.99
prompt =
prompt =
prompt ====================
prompt Percentage of time in each module, summarized across runs
select p1.unit_owner,
decode(p1.unit_name, '', '<anonymous>', substr(p1.unit_name,1, 25)) as
unit_name,
p1.total_time/1000000000 as secs,
TO_CHAR(100*p1.total_time/p2.grand_total, '99999.99') as percentage
from plsql_profiler_units_cross_run p1,
plsql_profiler_grand_total p2
order by p1.total_time DESC;
prompt =
prompt =
prompt ====================
prompt Lines taking more than 1% of the total time, each run separate
select p1.runid as runid,
p1.total_time/10000000 as Hsecs,
p1.total_time/p4.grand_total*100 as pct,
substr(p2.unit_owner, 1, 20) as owner,
decode(p2.unit_name, '', '<anonymous>', substr(p2.unit_name,1, 20)) as
unit_name,
p1.line#,
( select p3.text
from all_source p3
where p3.owner = p2.unit_owner and
p3.line = p1.line# and
p3.name=p2.unit_name and
p3.type not in ( 'PACKAGE', 'TYPE' )) text
from plsql_profiler_data p1,
plsql_profiler_units p2,
plsql_profiler_grand_total p4
where (p1.total_time >= p4.grand_total/100)
AND p1.runID = p2.runid
and p2.unit_number=p1.unit_number
order by p1.total_time desc;
prompt =
prompt =
prompt ====================
prompt Most popular lines (more than 1%), summarize across all runs
select p1.total_time/10000000 as hsecs,
p1.total_time/p4.grand_total*100 as pct,
substr(p1.unit_owner, 1, 20) as unit_owner,
decode(p1.unit_name, '', '<anonymous>',
substr(p1.unit_name,1, 20)) as unit_name,
p1.line#,
( select p3.text from all_source p3
where (p3.line = p1.line#) and
(p3.owner = p1.unit_owner) AND
(p3.name = p1.unit_name) and
(p3.type not in ( 'PACKAGE', 'TYPE' ) ) ) text
from plsql_profiler_lines_cross_run p1,
plsql_profiler_grand_total p4
where (p1.total_time >= p4.grand_total/100)
order by p1.total_time desc;
execute prof_report_utilities.rollup_all_runs;
prompt =
prompt =
prompt ====================
prompt Number of lines actually executed in different units (by unit_name)
select p1.unit_owner,
p1.unit_name,
count( decode( p1.total_occur, 0, null, 0)) as lines_executed ,
count(p1.line#) as lines_present,
count( decode( p1.total_occur, 0, null, 0))/count(p1.line#) *100
as pct
from plsql_profiler_lines_cross_run p1
where (p1.unit_type in ( 'PACKAGE BODY', 'TYPE BODY',
'PROCEDURE', 'FUNCTION' ) )
group by p1.unit_owner, p1.unit_name;
prompt =
prompt =
prompt ====================
prompt Number of lines actually executed for all units
select count(p1.line#) as lines_executed
from plsql_profiler_lines_cross_run p1
where (p1.unit_type in ( 'PACKAGE BODY', 'TYPE BODY',
'PROCEDURE', 'FUNCTION' ) )
AND p1.total_occur > 0;
prompt =
prompt =
prompt ====================
prompt Total number of lines in all units
select count(p1.line#) as lines_present
from plsql_profiler_lines_cross_run p1
where (p1.unit_type in ( 'PACKAGE BODY', 'TYPE BODY',
'PROCEDURE', 'FUNCTION' ) );
spool off
set termout on
edit profsum.out
set linesize 131
utl_file
查看可用逻辑路径:
-------------------------------------------------------------------------------
select * from dba_directories;
练习 1:使用utl_file读取外部文件
-------------------------------------------------------------------------------
create or replace procedure test_utl_file_read
(path varchar2,filename varchar2)
is
vinhandle utl_file.file_type;
vnewline varchar2(250);
begin
vinhandle := utl_file.fopen(path, filename, 'R');
loop
begin
utl_file.get_line(vinhandle, vnewline);
dbms_output.put_line(vnewline);
exception
when others then
exit;
end;
end loop;
utl_file.fclose(vinhandle);
end test_utl_file_read;
/
path => 为有效的逻辑路径(IMG)
练习 2: 使用UTL_FILE,数据写入到文本中
-------------------------------------------------------------------------------
需要修改系统参数 utl_file_dir='/home/oracle'
alter system set utl_file_dir='/home/oracle' scope=spfile; --打开PL/SQL与操作系统i/o的接口!
declare
v_filehandle utl_file.file_type;
begin
v_filehandle:=utl_file.fopen('/home/oracle','output.txt','W');
utl_file.putf (v_filehandle,'SALARY REPORT: GENERATED ON%s\n', SYSDATE);
utl_file.new_line (v_filehandle);
utl_file.putf (v_filehandle, '%s\n','hello ');
utl_file.putf (v_filehandle, 'DEPARTMENT: %s\n','world ');
utl_file.putf(v_filehandle, 'aaaa%sbbb%sccc%sddd%seee%s','1','2','3','4','5');
utl_file.fclose (v_filehandle);
end;
/
练习 3:将DEPT表的数据写入到文本中
--------------------------------------------------------------------------------
declare
v_filehandle UTL_FILE.FILE_TYPE;
begin
v_filehandle:=utl_file.fopen('/home/oracle','output.txt','w');
for i in (select * from scott.dept) loop
UTL_FILE.PUTF (v_filehandle, '%s,%s,%s\n',i.deptno,i.dname,i.loc);
end loop;
UTL_FILE.FCLOSE (v_filehandle);
end;
/
练习 4:将DEPT表的数据追加写入到文本中
--------------------------------------------------------------------------------
declare
v_filehandle UTL_FILE.FILE_TYPE;
begin
v_filehandle:=utl_file.fopen('/home/oracle','output.txt','a');
UTL_FILE.PUTF (v_filehandle,'这是DEPT表的追加数据,导出时间为:%s\n', SYSDATE);
UTL_FILE.NEW_LINE (v_filehandle);
for i in(select * from scott.dept) loop
UTL_FILE.PUTF (v_filehandle, '%s %s %s\n',i.deptno,i.dname,i.loc);
end loop;
UTL_FILE.FCLOSE (v_filehandle);
end;
/
练习 5: 将EMP表的数据导入到文本中
--------------------------------------------------------------------------------
declare
v_filehandle UTL_FILE.FILE_TYPE;
begin
v_filehandle:=utl_file.fopen('/home/oracle','output.txt','w');
UTL_FILE.PUTF (v_filehandle,'This emp detail information,export time : %s\n', current_timestamp);
UTL_FILE.NEW_LINE (v_filehandle);
for i in (select * from scott.emp)
loop
UTL_FILE.PUTF (v_filehandle, '%s,%s,%s,%s,%s,',i.EMPNO,i.ENAME,i.JOB,i.MGR,to_char(i.HIREDATE,'yyyy-mm-dd hh24:mi:ss'));
UTL_FILE.PUTF (v_filehandle, '%s,%s,%s\n',i.SAL,i.COMM,i.DEPTNO);
end loop;
UTL_FILE.FCLOSE (v_filehandle);
end;
/
----------------------------------------------------------------------
dbms_random
1)'u', 'U' - returning string in uppercase alpha characters
2)'l', 'L' - returning string in lowercase alpha characters
3)'a', 'A' - returning string in mixed case alpha characters
4)'x', 'X' - returning string in uppercase alpha-numeric characters
5)'p', 'P' - returning string in any printable characters.
6)Otherwise the returning string is in uppercase alpha characters.
col a for a8
--随机生成8位大写字母
select dbms_random.string('u',8) x from dual;
select dbms_random.string('U',8) x from dual;
--随机生成8位小写字母
select dbms_random.string('l',8) x from dual;
select dbms_random.string('L',8) x from dual;
--随机生成8位小小写混合字母
select dbms_random.string('a',8) x from dual;
select dbms_random.string('A',8) x from dual;
--随机生成8位大写字母和数字
select dbms_random.string('x',8) x from dual;
select dbms_random.string('X',8) x from dual;
--随机生成8位任意符号
select dbms_random.string('p',8) x from dual;
select dbms_random.string('P',8) x from dual;
--返回的内容仍将是大写字母
select dbms_random.string('2',8) x from dual;