新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在服务中关闭:
创新互联建站是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括网站设计制作、成都做网站、电商网站制作开发、微信小程序定制开发、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!
OracleOracle_homeTNSListener 对应于数据库的监听程序
OracleServiceSID 对应于数据库的例程
OracleDBConsoleSID 对应于Oracle Enterprise Manager(OEM)
其中,Oracle_home表示Oracle主目录,如Oracle11g_home1;SID表示Oracle系统标识符,如OracleServiceMAG01的MAG01。
比较好的启动顺序是:OracleOracle_homeTNSListener,OracleServiceSID,OracleDBConsoleSID。
关闭时次序相反。
下面给出了自己写的已经排好顺序的启动和停止服务的两个批处理文件:
//启动Oracle
Start Oracle.bat
@echo off
echo 确定要启动Oracle 11g服务吗?
pause
net start OracleOraDb11g_home1TNSListener
net start OracleServiceMAG01
net start OracleDBConsolemag01
echo 启动Oracle 11g服务完成,请确认有没有错误发生。
Pause
//关闭Oracle
Stop Oracle.bat
@echo off
echo 确定要停止Oracle 11g 服务吗?
pause
net stop OracleDBConsolemag01
net stop OracleServiceMAG01
net stop OracleOraDb11g_home1TNSListener
echo 停止Oracle 11g服务完成,请确认有没有错误发生。
Pause
Oracle中函数/过程返回结果集的几种方式:
以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过.
(1) 返回游标:
return的类型为:SYS_REFCURSOR
之后在IS里面定义变量:curr SYS_REFCURSOR;
最后在函数体中写:
open cur for
select ......;
return cur;
例:
CREATE OR REPLACE FUNCTION A_Test(
orType varchar2
)RETURN SYS_REFCURSOR is
type_cur SYS_REFCURSOR; BEGIN
OPEN type_cur FOR
select col1,col2,col3 from testTable ; RETURN type_cur; END;
(2)返回table类型的结果集:
首先定义一个行类型:
CREATE OR REPLACE TYPE "SPLIT_ARR" AS OBJECT(nowStr varchar2(18))
其次以此行类型定义一个表类型:
CREATE OR REPLACE TYPE "SPLIT_TAB" AS TABLE of split_arr;
定义函数(此函数完成字符串拆分功能):
CREATE OR REPLACE FUNCTION GetSubStr( str in varchar2, --待分割的字符串
splitchar in varchar2 --分割标志 ) return split_tab IS
restStr varchar2(2000) default GetSubStr.str;--剩余的字符串
thisStr varchar2(18);--取得的当前字符串
indexStr int;--临时存放分隔符在字符串中的位置
v split_tab := split_tab(); --返回结果
begin
dbms_output.put_line(restStr); while length(restStr) != 0
LOOP top
indexStr := instr(restStr,splitchar); --从子串中取分隔符的第一个位置
if indexStr = 0 and length(restStr) != 0 then--在剩余的串中找不到分隔符
begin
v.extend;
v(v.count) := split_arr(Reststr); return v; end; end if;
if indexStr = 1 then---第一个字符便为分隔符,此时去掉分隔符
begin
restStr := substr(restStr,2); goto top; end; end if;
if length(restStr) = 0 or restStr is null then
return v; end if;
v.extend;
thisStr := substr(restStr,1,indexStr - 1); --取得当前的字符串
restStr := substr(restStr,indexStr + 1);---取剩余的字符串
v(v.count) := split_arr(thisStr); END LOOP; return v; end;
在PL/SQL developer中可以直接调用
cursor strcur is
select nowStr from Table(GetSubStr('111,222,333,,,',','));
(3)以管道形式输出:
create type row_type as object(a varchar2(10), v varchar2(10));--定义行对象
create type table_type as table of row_type; --定义表对象
create or replace function test_fun(
a in varchar2,b in varchar2
) return table_type pipelined is
v row_type;--定义v为行对象类型
begin
for thisrow in (select a, b from mytable where col1=a and col2 = b) loop
v := row_type(thisrow.a, thisrow.b); pipe row (v); end loop; return; end; select * from table(test_fun('123','456'));
首先登陆主机,执行top发现CPU资源几乎消耗殆尽,存在很多占用CPU很高的进程,而内存和I/O都不高,具体如下:
last pid: 26136; load averages: 8.89, 8.91, 8.12
216 processes: 204 sleeping, 8 running, 4 on cpu
CPU states: 0.6% idle, 97.3% user, 1.8% kernel, 0.2% iowait, 0.0% swap
Memory: 8192M real, 1166M free, 14M swap in use, 8179M swap free
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
25725 oracle 1 50 0 4550M 4508M cpu2 12:23 11.23% oracle
25774 oracle 1 41 0 4550M 4508M run 14:25 10.66% oracle
26016 oracle 1 31 0 4550M 4508M run 5:41 10.37% oracle
26010 oracle 1 41 0 4550M 4508M run 4:40 9.81% oracle
26014 oracle 1 51 0 4550M 4506M cpu6 4:19 9.76% oracle
25873 oracle 1 41 0 4550M 4508M run 12:10 9.45% oracle
25723 oracle 1 50 0 4550M 4508M run 15:09 9.40% oracle
26121 oracle 1 41 0 4550M 4506M cpu0 1:13 9.28% oracle
25745 oracle 1 41 0 4551M 4512M run 9:33 9.28% oracle
26136 oracle 1 41 0 4550M 4506M run 0:06 5.61% oracle
409 root 15 59 0 7168K 7008K sleep 173.1H 0.52% picld
25653 oracle 1 59 0 4550M 4508M sleep 1:01 0.46% oracle
25565 oracle 1 59 0 4550M 4508M sleep 0:07 0.24% oracle
25703 oracle 1 59 0 4550M 4506M sleep 0:08 0.13% oracle
25701 oracle 1 59 0 4550M 4509M sleep 0:23 0.10% oracle
于是先查看数据库的告警日志ALERT文件,并没有发现有什么错误存在,日志显示数据库运行正常,排除数据库本身存在问题。
然后查看这些占用CPU资源很高的Oracle进程究竟是在做什么操作,使用如下SQL语句:
select sql_text,spid,v$session.program,process from
v$sqlarea,v$session,v$process
where v$sqlarea.address=v$session.sql_address
and v$sqlarea.hash_value=v$session.sql_hash_value
and v$session.paddr=v$process.addr
and v$process.spid in (PID);
用top中占用CPU很高的进程的PID替换脚本中的PID,得到相应的Oracle进程所执行的SQL语句,发现占用CPU资源很高的进程都是执行同一个SQL语句:
SELECT d.domainname,d.mswitchdomainid, a.SERVICEID,a.SERVICECODE,a.USERTYPE,a.STATUS,a.NOTIFYSTATUS,to_char(a.DATECREATED,'yyyy-mm-dd hh24:mi:ss') DATECREATED,VIPFLAG,STATUS2,CUSTOMERTYPE,CUSTOMERID FROM service a, gatewayloc b, subbureaunumber c, mswitchdomain d WHERE b.mswitchdomainid = d.mswitchdomainid and b.gatewaysn = c.gatewaysn AND a.ServiceCode like c.code||'%' and a.serviceSpecID=1 and a.status!='4' and a.status!='10' and a.servicecode like '010987654321%' and SubsidiaryID=999999999
基本上可以肯定是这个SQL引起了系统CPU资源大量被占用,那究竟是什么原因造成这个SQL这么大量占用CPU资源呢,我们先来看看数据库的进程等待事件都有些什么:
SQL select sid,event,p1,p1text from v$session_wait;
SID EVENT P1 P1TEXT
---------- ----------------------------------------------------------------
12 latch free 4.3982E+12 address
36 latch free 4.3982E+12 address
37 latch free 4.3982E+12 address
84 latch free 4.3982E+12 address
102 latch free 4.3982E+12 address
101 latch free 4.3982E+12 address
85 latch free 4.3982E+12 address
41 latch free 4.3982E+12 address
106 latch free 4.3982E+12 address
155 latch free 4.3982E+12 address
151 latch free 4.3982E+12 address
149 latch free 4.3982E+12 address
147 latch free 4.3982E+12 address
1 pmon timer 300 duration
从上面的查询我们可以看出,大都是latch free的等待事件,然后接着查一下这些latch的等待都是什么进程产生的:
SQL select spid from v$process where addr in
(select paddr from v$session where sid in(84,102,101,106,155,151));
SPID
------------
25774
26010
25873
25725
26014
26016
由此看出latch free这个等待事件导致了上面的那个SQL语句都在等待,占用了大量的CPU资源。我们来看看究竟主要是那种类型的latch的等待,根据下面的SQL语句:
SQL SELECT latch#, name, gets, misses, sleeps
FROM v$latch
WHERE sleeps0
ORDER BY sleeps;
LATCH# NAME GETS MISSES SLEEPS
---------- ----------------------------------------------------------------
15 messages 96876 20 1
159 library cache pin allocation 407322 43 1
132 dml lock allocation 194533 213 2
4 session allocation 304897 48 3
115 redo allocation 238031 286 4
17 enqueue hash chains 277510 85 5
7 session idle bit 2727264 314 16
158 library cache pin 3881788 5586 58
156 shared pool 2771629 6184 662
157 library cache 5637573 25246 801
98 cache buffers chains 1722750424 758400 109837
由上面的查询可以看出最主要的latch等待是cache buffers chains,这个latch的等待表明数据库存在单独的BLOCK的竞争这些latch,我们来看这个latch存在的子latch及其对应的类型:
SQL SELECT addr, latch#, gets, misses, sleeps
FROM v$latch_children
WHERE sleeps0
and latch# = 98
ORDER BY sleeps desc;
ADDR LATCH# GETS MISSES SLEEPS
---------------- ---------- ---------- ---------- ----------
000004000A3DFD10 98 10840661 82891 389
000004000A698C70 98 159510 2 244
0000040009B21738 98 104269771 34926 209
0000040009B227A8 98 107604659 35697 185
000004000A3E0D70 98 5447601 18922 156
000004000A6C2BD0 98 853375 7 134
0000040009B24888 98 85538409 25752 106
000004000A36B250 98 1083351 199 96
000004000A79EC70 98 257970 64 35
000004000A356AD0 98 1184810 160 34
……………
接着我们来查看sleep较多的子latch对应都有哪些对象:
SQL select distinct a.owner,a.segment_name,a.segment_type from
dba_extents a,
(select dbarfil,dbablk
from x$bh
where hladdr in
(select addr
from (select addr
from v$latch_children
order by sleeps desc)
where rownum 5)) b
where a.RELATIVE_FNO = b.dbarfil
and a.BLOCK_ID = b.dbablk and a.block_id + a.blocks b.dbablk;
OWNER SEGMENT_NAME SEGMENT_TYPE
---------------------------------------------------------------------------
TEST I_SERVICE_SERVICESPECID INDEX
TEST I_SERVICE_SUBSIDIARYID INDEX
TEST SERVICE TABLE
TEST MSWITCHDOMAIN TABLE
TEST I_SERVICE_SC_S INDEX
TEST PK_MSWITCHDOMAIN INDEX
TEST GATEWAYLOC TABLE
…………………
我们看到在开始的那个SQL语句中的几个对象都有包括在内,于是来看看开始的那个SQL的执行计划:
SQL set autotrace trace explain
SQLSELECT d.domainname,d.mswitchdomainid, a.SERVICEID,a.SERVICECODE,a.USERTYPE,a.STATUS,a.NOTIFYSTATUS,to_char(a.DATECREATED,'yyyy-mm-dd hh24:mi:ss') DATECREATED,VIPFLAG,STATUS2,CUSTOMERTYPE,CUSTOMERID FROM service a, gatewayloc b, subbureaunumber c, mswitchdomain d WHERE b.mswitchdomainid = d.mswitchdomainid and b.gatewaysn = c.gatewaysn AND a.ServiceCode like c.code||'%' and a.serviceSpecID=1 and a.status!='4' and a.status!='10' and a.servicecode like '010987654321%' and SubsidiaryID=999999999;
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 NESTED LOOPS
2 1 NESTED LOOPS
3 2 NESTED LOOPS
4 3 TABLE ACCESS (FULL) OF 'SUBBUREAUNUMBER'
5 3 TABLE ACCESS (BY INDEX ROWID) OF 'GATEWAYLOC'