會話及物件查詢

dragon路發表於2011-07-16
oracle常用的sql查詢語句(2008-06-24 15:03:05)
標籤:

oracle查詢

it

找出訪問一個物件的會話的程式資訊及會話資訊

select distinct s.username, p.pid, osuser, p.spid, s.process, s.lockwait  

from v$process p, v$session s, v$access a

where a.sid = s.sid  

and p.addr = s.paddr  

and s.sid = 33 ;

 

 

看一個sql執行了多少次,導致的disk reads為多大:

SELECT executions, version_count, parse_calls, disk_reads,

BUFFER_GETS, ROWS_PROCESSED, hash_value, SQL_TEXT

FROM V$SQLAREA

where sql_text like '%spare2%obj$%' and sql_text not like '%FROM V$SQLAREA%';

 

 

一個session產生的i/o,通常用來檢視一個session(正在執行語句的)是否已經hang.

select sess_io.sid,

       sess_io.block_gets,

       sess_io.consistent_gets,

       sess_io.physical_reads,

       sess_io.block_changes,

       sess_io.consistent_changes

 from v$sess_io sess_io, v$session sesion

 where sesion.sid = sess_io.sid

   and sesion.username is not null;

 

 

在當前session中查詢當前sessionsid:

column pid new_value Pid

column sid new_value Sid

column serial# new_value Serial

 

select p.pid, s.sid, s.serial#

from sys.v_$session  s, sys.v_$process  p

where s.sid = (select sid from sys.v_$mystat where rownum = 1) and

  p.addr = s.paddr

/

 

-- or

 

select sid,serial# from v$session where audsid =

(select userenv('sessionid') from dual);

 

查詢session與相關的process的情況:

set linesize 290

col machine format a30 wrap

col USERNAME format a15 wrap

col sql format a40

col program format a45

col LOGON_TIME format a25

set pagesize 500

select s.sid, s.SERIAL#, s.username, b.name "BG Process", p.spid ServPID,

s.status, s.server, s.machine, s.program,

to_char(s.LOGON_TIME,'yyyy.mm.dd hh24:mi:ss') LOGON_TIME, s.osuser, 

trunc(s.last_call_et/60,1) idel_mins

from v$session s, v$process p, v$bgprocess b

where p.addr = s.paddr

and b.paddr(+) = p.addr;

 

idel_mins為該session空閒的秒數,如果該空閒的時間過長, 則說明有可能程式沒有及時的關閉資料庫連線, 這時可以藉助該session已經執行過的sql語句發現是那個應用或應用的那個部分發出的,從而解決問題.

 

 

只查詢後臺程式的資訊:

set linesize 290

col machine format a30 wrap

col name for a15

col USERNAME format a15 wrap

col sql format a40

col program format a45

col LOGON_TIME format a25

set pagesize 500

 

select s.sid, s.SERIAL#, b.name, s.username, p.spid ServPID,

       s.machine, s.program,

to_char(s.LOGON_TIME,'yyyy.mm.dd hh24:mi:ss') LOGON_TIME,

       s.osuser, trunc(s.last_call_et/60,1) idel_mins

from v$session s, v$process p, v$bgprocess b

where p.addr = s.paddr and p.addr = b.paddr;

 

 

生成殺死session的語句

set linesize 290

col machine format a30 wrap

col USERNAME format a15 wrap

col kill_sql format a40

set pagesize 500

select 'alter system kill session ''' || to_char(s.sid)||','||to_char(s.SERIAL#)||''';' kill_sql, s.username, machine, ‘kill -9 ‘||to_char(p.spid) kill_process, s.server

from v$session s, v$process p

where p.addr = s.paddr ;

 

查詢出一個表中指定範圍的行(rownum進行限制)

select t1.rn, t1.col1

from (select rownum rn, col1

from tab1) t1

where t1.rn between 5 and 7;

 

查詢當前使用者的使用者許可權:

1) 使用者擁有的角色

       User_role_privs

2) 使用者擁有的表許可權

       User_tab_privs

3) 使用者擁有的列許可權

       User_col_privs

5) 使用者擁有的系統許可權

User_sys_privs

6) 查詢角色中的系統許可權

Role_sys_privs

4.) 查詢當前使用者擁有的所有許可權(不包括表許可權與列許可權)

session_privs;

 

 

看一個排序操作佔用多少臨時表空間:

SELECT tablespace_name, extent_size, total_extents, used_extents,

               free_extents, max_used_size

FROM v$sort_segment;

 

檢視一個查詢是否並行執行:

在語句執行時, 執行:

Select * from v$pq_sesstat;

 

 

檢視並行查詢程式:

select * from V$PQ_SLAVE;

 

 

檢視使用者的各個隊象的依存情況:

select * from USER_DEPENDENCIES

where name <> referenced_name

         and referenced_type <> 'NON-EXISTENT'

         and referenced_name <> 'DUAL'

      and referenced_name <> 'DBMS_OUTPUT'

         and referenced_name <> 'STANDARD'

 

 

檢視一個session正在執行的sql語句:

通過v$sql,v$sql比v$area消耗的資源要小(v$sql中為每一條sql保留一個條目,而v$sqlarea中根據sql_text進行group by,即如果一條語句有不同的version,則在v$sql中表現為多條記錄,而在v$sqlarea中只有一條記錄)

 

8i:

利用v$open_cursor查詢session中執行的sql, 有可能有多條,也有可能一條沒有,這主要看查詢的sid,多長時間沒有活動了:

select s.sid, s.username, sql.hash_value,

sql.address, sql.sql_text

 from v$open_cursor sql, v$session s

 where sql.sid = s.sid and

s.sid =?;

 

利用v$sql查詢session中執行的sql, 此查詢節省資源:

select s.sid, s.username, sql.optimizer_mode, sql.executions, sql.disk_reads, sql.buffer_gets,

sql.hash_value,

sql.address, sql.sql_text

  from v$sql sql, v$session s

 where s.sql_hash_value = sql.hash_value

   and s.sql_address    = sql.address

   and s.username is not null

   and sid = ?;

 

利用v$sqlarea查詢session中執行的sql, 此查詢比較耗費資源:

select s.sid, s.username, sql.optimizer_mode, sql.executions, sql.disk_reads, sql.buffer_gets,

sql.hash_value,

sql.address, sql.sql_text

  from v$sqlarea sql, v$session s

 where s.sql_hash_value = sql.hash_value

   and s.sql_address    = sql.address

   and s.username is not null

   and sid = ?;

 

 

9i:

利用v$open_cursor查詢session中執行的sql, 有可能有多條,也有可能一條沒有,這主要看查詢的sid,多長時間沒有活動了:

select s.sid, s.username, sql.hash_value,

sql.address, sql.sql_text

 from v$open_cursor sql, v$session s

 where sql.sid = s.sid and

s.sid = ?;

 

利用v$sql查詢session中執行的sql, 此查詢節省資源:

select s.sid, s.username, optimizer_mode, executions, disk_reads, buffer_gets,

hash_value, sql_hash_value, prev_hash_value,

address, cpu_time, elapsed_time, sql_text

from v$sql sql, v$session s

where ((s.sql_hash_value = sql.hash_value and s.sql_address= sql.address) or

       (s.prev_hash_value = sql.hash_value and s.prev_sql_addr= sql.address))

   and s.username is not null

   and sid = ?;

 

 

利用v$sqlarea查詢session中執行的sql, 此查詢比較耗費資源:

select s.sid, s.username, optimizer_mode, executions, disk_reads, buffer_gets,

hash_value, sql_hash_value, prev_hash_value,

address, cpu_time, elapsed_time, sql_text

from v$sqlarea sql, v$session s

where ((s.sql_hash_value = sql.hash_value and s.sql_address= sql.address) or

       (s.prev_hash_value = sql.hash_value and s.prev_sql_addr= sql.address))

   and s.username is not null

   and sid = ?;

 

 

看資料庫上是否有nologging操作:

SELECT NAME, to_char(UNRECOVERABLE_CHANGE#) UNRECOVERABLE_CHANGE,

TO_CHAR (UNRECOVERABLE_TIME,'DD-MON-YYYY HH:MI:SS') UNRECOVERABLE_TIME

FROM V$DATAFILE;

 

知道了一個檔案號與資料塊號,如何知道該處被哪個物件佔有:

SELECT segment_owner, segment_name
    FROM dba_extents
   WHERE file_id=&FILE_ID
     AND &BLOCK_ID between block_id and block_id+blocks-1;

 

session本身的連線查詢其sid

column pid new_value Pid

column sid new_value Sid

column serial# new_value Serial

 

selectp.pid, s.sid, s.serial#

from sys.v_$session s,sys.v_$process p

where s.sid = (select sid from sys.v_$mystat where rownum = 1)

and p.addr = s.paddr;

 

-- or

 

 select sid,serial# from v$session where audsid =

 (select userenv('sessionid') from dual);

 

windows下自動啟動sql*plus並執行一個sql檔案:

1) start.bat

sqlplus /nolog "@C:\start.sql"

2) start.sql

conn / as sysdba

startup

 

 

 

 

log file改名:

在mount狀態下:

ALTER DATABASE RENAME FILE 

                 '/FULL_PATH_OF_OLD_LOCATION/AND_REDO_LOG_NAME.LOG' 

                 TO 

                 '/FULL_PATH_OF_NEW_LOCATION/AND_REDO_LOG_NAME.LOG'; 

 

data file改名:

在mount或資料檔案offline狀態下:

ALTER DATABASE RENAME FILE 

                 '/FULL_PATH_OF_OLD_LOCATION/AND_DATAFILE_NAME.DBF'

                 TO 

                 '/FULL_PATH_OF_NEW_LOCATION/AND_DATAFILE_NAME.DBF'

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25736250/viewspace-702375/,如需轉載,請註明出處,否則將追究法律責任。

相關文章