oracle占用CPU较高

来源:互联网 发布:程序员培训班怎么样 编辑:程序博客网 时间:2024/04/27 19:54

原文出处:http://blog.csdn.net/chen3888015/article/details/7491245

                    http://blog.csdn.net/george188/article/details/4494361

这类问题通常都是因为系统中存在性能低下甚至存在错误的SQL语句, 消耗了大量的CPU所致.

本文通过一个案例就如何捕获这样的SQL给出一个通用的方法.

----------------------------------------------------方法一----------------------------------------------

下面是模拟过程
1,在一个session中模拟CPU高使用率,如下:

[sql] view plaincopyprint?
  1. declare  
  2. num int:=0;  
  3. begin  
  4. loop  
  5. num:=num+1;  
  6. end loop;  
  7. end;  
  8. /  

2,在shell窗口用top命令查看CPU使用情况: 

[plain] view plaincopyprint?
  1. PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME CPU COMMAND  
  2. 7913 oracle    25   0 24364  23M 22144 R    91.7  2.3   0:44   0 oracle  

3,通过Oracle提供的视图查看Session情况:

[plain] view plaincopyprint?
  1. SQL> select addr,spid from v$process where spid='7913';                                                                                                                
  2. ADDR     SPID  
  3. -------- ------------  
  4. 2AA1A6EC 7913  
  5. SQL> select sid,sql_id from v$session where paddr='2AA1A6EC';   
  6.        SID SQL_ID  
  7. ---------- -------------  
  8.        157 948mkp962vqgy  
  9. SQL> select sql_text from v$sql where sql_id='948mkp962vqgy';  
  10. SQL_TEXT  
  11. --------------------------------------------------------------------------------  
  12. declare num int:=0; begin loop num:=num+1; end loop; end;  

4,从上面分析可以知道是执行的SQL有死循环.
将对应的服务进程kill掉 kill -9 7913
[在碰到CPU高占用的时候可以根据上面的步骤,分析对应的session执行的SQL进行不通的处理]

----------------------------------------------方法二---------------------------------------------------------------

-----查看系统中使用mem资源比较多的进程:

ps -elf|awk '$10 >16000 {print  }'| sort –rnk 10

-----查看系统中使用CPU资源比较多的进程:
top –n500 –f top.out

------查看具体执行的是哪些sql语句占用CPU过高(针对oracle进程)

1.先得到进程的pid

select id,
       serial#,
       username,
       osuser,
       machine,
       program,
       process,
       to_char(logon_time, 'yyyy/mm/dd hh24:mi:ss') logon
  from v$session
 where paddr in (select addr from v$process where spid in ('&pid'));

2.再得到session的sqltext语句

 select sql_text
   from v$sqltext_with_newlines
  where hash_value in
        (select SQL_HASH_VALUE
           from v$session
          where paddr in (select addr from v$process where spid = '&pid'))
  order by piece;

------------提高CPU使用率的sql语句---------------------------------
建表,插数据,查询,truncate,再插数据
-- Create table
create table FORTEST
(
  USERNAME VARCHAR2(30),
  PASSWORD VARCHAR2(30),
  INTIME   DATE,
  OID      NUMBER(27)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    minextents 1
    maxextents unlimited
  );
 
 
 
  create or replace procedure pro_fortest is
  var_tablename varchar2(100) := 'truncate table fortest';
begin
  execute  immediate var_tablename;
  for i in 0 .. 9999 loop
    insert into fortest
      (username, password, intime, oid)
    values
      ('fdjsalfjlasdfj', 'asfdsfdsf', sysdate, seq_fortest.currval);
    commit;
  end loop;
end;

---建job
declare
  job1 number;
begin
  dbms_job.submit(job1, 'pro_fortest;', sysdate, 'sysdate+1/17280');
  COMMIT;
end;
/

---查询语句:
select count(*) from fortest;

select t.*,t.rowid from fortest t;
select t.*,t.rowid from dba_jobs t;

-----------终止的话,就在对象中将job删掉

0 0
原创粉丝点击