常用SQL語句2

 根据SID找ORACLE的某个进程:
SQL> SELECT PRO.SPID FROM V$SESSION SES,V$PROCESS PRO WHERE SES.SID=21 AND SES.PADDR=PRO.ADDR;

监控当前数据库谁在运行什么SQL语句:
SQL>SELECT OSUSER, USERNAME, SQL_TEXT FROM V$SESSION A, V$SQLTEXT B
WHERE A.SQL_ADDRESS =B.ADDRESS ORDER BY ADDRESS, PIECE;

如何查看数据库中某用户,正在运行什么SQL语句
SQL>SELECT SQL_TEXT FROM V$SQLTEXT T, V$SESSION S WHERE T.ADDRESS=S.SQL_ADDRESS AND T.HASH_VALUE=S.SQL_HASH_VALUE AND S.MACHINE='XXXXX' OR USERNAME='WACOS';

如何查出前台正在发出的sql语句:
SQL> SELECT USER_NAME,SQL_TEXT FROM V$OPEN_CURSOR WHERE SID IN(SELECT SID FROM (SELECT SID,SERIAL# FROM V$SESSION WHERE STATUS='ACTIVE'));

查询当前所执行的SQL语句:
SQL> SELECT PROGRAM ,SQL_ADDRESS FROM V$SESSION WHERE PADDR IN (SELECT ADDR FROM V$PROCESS WHERE SPID=3556);
PROGRAM                                          SQL_ADDRESS
------------------------------------------------ ----------------
SQLPLUS@CTC20 (TNS V1-V3)                        000000038FCB1A90
SQL> SELECT SQL_TEXT FROM V$SQLAREA WHERE ADDRESS='000000038FCB1A90';

找出消耗CPU最高的进程对应的SQL语句:
SET LINE 240
SET VERIFY OFF
COLUMN SID FORMAT 999
COLUMN PID FORMAT 999
COLUMN S_# FORMAT 999
COLUMN USERNAME FORMAT A9 HEADING "ORA USER"
COLUMN PROGRAM FORMAT A29
COLUMN SQL      FORMAT A60
COLUMN OSNAME FORMAT A9 HEADING "OS USER"
SELECT P.PID PID,S.SID SID,P.SPID SPID,S.USERNAME USERNAME,S.OSUSER OSNAME,P.SERIAL# S_#,P.TERMINAL,P.PROGRAM PROGRAM,P.BACKGROUND,S.STATUS,RTRIM(SUBSTR(A.SQL_TEXT, 1, 80)) SQLFROM V$PROCESS P, V$SESSION S,V$SQLAREA A WHERE P.ADDR = S.PADDR AND S.SQL_ADDRESS = A.ADDRESS (+) AND P.SPID LIKE '%&1%';

ENTER VALUE FOR 1: PID¡(这里输入占用CPU最高的进程对应的PID)
SET TERMOUT OFF
SPOOL MAXCPU.TXT
SELECT '++'||S.USERNAME USERNAME,RTRIM(REPLACE(A.SQL_TEXT,CHR(10),''))||';'FROM V$PROCESS P, V$SESSION S,V$SQLAREA A WHERE P.ADDR = S.PADDR AND S.SQL_ADDRESS = A.ADDRESS (+) AND P.SPID LIKE '%&&1%';
 Enter value for 1: PID(这里输入占用CPU最高的进程对应的PID)
spool off(这句放在最后执行)

CPU用率最高的2条SQL语句的获取
执行:top,通过top获得CPU占用率最高的进程的pid。
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);
COL MACHINE FORMAT A30
COL PROGRAM FORMAT A40
SET LINE 200
SQL>SELECT SID,SERIAL# ,USERNAME,OSUSER,MACHINE,PROGRAM,PROCESS,TO_CHAR(LOGON_TIME,'YYYY/MM/DD HH24:MI:SS') FROM V$SESSION WHERE PADDR IN(SELECT ADDR FROM V$PROCESS WHERE SPID IN([$SPID]));

SELECT SQL_TEXT FROM V$SQLTEXT_WITH_NEWLINES
WHERE HASH_VALUE=(SELECT SQL_HASH_VALUE FROM V$SESSION WHERE SID=&SID)
ORDER BY PIECE;

16、查看锁lock情况:
SQL>SELECT /*+ RULE */
 LS.OSUSER OS_USER_NAME,
 LS.USERNAME USER_NAME,
 DECODE(LS.TYPE,
        'RW','ROW WAIT ENQUEUE LOCK',
        'TM','DML ENQUEUE LOCK',
        'TX','TRANSACTION ENQUEUE LOCK',
        'UL','USER SUPPLIED LOCK') LOCK_TYPE,
 O.OBJECT_NAME OBJECT,
 DECODE(LS.LMODE,
        1,NULL,
        2,'ROW SHARE',
        3,'ROW EXCLUSIVE',
        4,'SHARE',
        5,'SHARE ROW EXCLUSIVE',
        6,'EXCLUSIVE',
        NULL) LOCK_MODE,
 O.OWNER,
 LS.SID,
 LS.SERIAL# SERIAL_NUM,
 LS.ID1,
 LS.ID2
 FROM SYS.DBA_OBJECTS O,
       (SELECT S.OSUSER,
               S.USERNAME,
               L.TYPE,
               L.LMODE,
               S.SID,
               S.SERIAL#,
               L.ID1,
               L.ID2
          FROM V$SESSION S, V$LOCK L
         WHERE S.SID = L.SID) LS
 WHERE O.OBJECT_ID = LS.ID1
   AND O.OWNER <> 'SYS'
 ORDER BY O.OWNER, O.OBJECT_NAME;

SQL>SELECT SYS.V_$SESSION.OSUSER,
       SYS.V_$SESSION.MACHINE,
       V$LOCK.SID,
       SYS.V_$SESSION.SERIAL#,
       DECODE(V$LOCK.TYPE,
              'MR','MEDIA RECOVERY',
              'RT','REDO THREAD',
              'UN','USER NAME',
              'TX','TRANSACTION',
              'TM','DML',
              'UL','PL/SQL USER LOCK',
              'DX','DISTRIBUTED XACTION',
              'CF','CONTROL FILE',
              'IS','INSTANCE STATE',
              'FS','FILE SET',
              'IR','INSTANCE RECOVERY',
              'ST','DISK SPACE TRANSACTION',
              'TS','TEMP SEGMENT',
              'IV','LIBRARY CACHE INVALIDA-TION',
              'LS','LOG START OR SWITCH',
              'RW','ROW WAIT',
              'SQ','SEQUENCE NUMBER',
              'TE','EXTEND TABLE',
              'TT','TEMP TABLE',
              'UNKNOWN') LOCKTYPE,
       RTRIM(OBJECT_TYPE) || ' ' || RTRIM(OWNER) || '.' || OBJECT_NAME OBJECT_NAME,
       DECODE(LMODE,
              0,'NONE',
              1,'NULL',
              2,'ROW-S',
              3,'ROW-X',
              4,'SHARE',
              5,'S/ROW-X',
              6,'EXCLUSIVE',
              'UNKNOWN') LOCKMODE,
       DECODE(REQUEST,
              0,'NONE',
              1,'NULL',
              2,'ROW-S',
              3,'ROW-X',
              4,'SHARE',
              5,'S/ROW-X',
              6,'EXCLUSIVE',
              'UNKNOWN') REQUESTMODE,
       CTIME,
       BLOCK B
 FROM V$LOCK, ALL_OBJECTS, SYS.V_$SESSION
 WHERE V$LOCK.SID > 6
   AND SYS.V_$SESSION.SID = V$LOCK.SID
   AND V$LOCK.ID1 = ALL_OBJECTS.OBJECT_ID;

以DBA角色, 查看当前数据库里锁的情况可以用如下SQL语句:
COL OWNER FOR A12
COL OBJECT_NAME FOR A16
SELECT B.OWNER,B.OBJECT_NAME,L.SESSION_ID,L.LOCKED_MODE
FROM V$LOCKED_OBJECT L, DBA_OBJECTS B
WHERE B.OBJECT_ID=L.OBJECT_ID;

SQL>SELECT T2.USERNAME,T2.SID,T2.SERIAL#,T2.LOGON_TIME FROM V$LOCKED_OBJECT T1,V$SESSION T2 WHERE T1.SESSION_ID=T2.SID ORDER BY T2.LOGON_TIME;

SQL>SELECT SQL_ADDRESS FROM V$SESSION WHERE SID=;

SQL>SELECT * FROM V$SQLTEXT WHERE ADDRESS=;

SQL>SELECT COMMAND_TYPE,PIECE,SQL_TEXT FROM V$SQLTEXT WHERE ADDRESS=(SELECT SQL_ADDRESS FROM V$SESSION A WHERE SID=18);   

SQL>SELECT OBJECT_ID FROM V$LOCKED_OBJECT;

SQL>SELECT OBJECT_NAME,OBJECT_TYPE FROM DBA_OBJECTS WHERE OBJECT_ID='';
如果有长期出现的一列,可能是没有释放的锁。我们可以用下面SQL语句杀掉长期没有释放非正常的锁: SQL>ALTER SYSTEM KILL SESSION 'SID,SERIAL#';

17、查看等待(wait)情况:
SQL>SELECT V$WAITSTAT.CLASS,V$WAITSTAT.COUNT COUNT, SUM(V$SYSSTAT.VALUE) SUM_VALUE FROM V$WAITSTAT,V$SYSSTAT WHERE V$SYSSTAT.NAME IN('DB BLOCK GETS','CONSISTENT GETS') GROUP BY V$WAITSTAT.CLASS,V$WAITSTAT.COUNT;

18、查看sga情况:
SQL>SELECT NAME, BYTES FROM SYS.V_$SGASTAT ORDER BY NAME ASC;

19、查看catched object:
SQL>SELECT OWNER,NAME,DB_LINK,NAMESPACE,TYPE,SHARABLE_MEM,LOADS, EXECUTIONS,LOCKS,PINS,KEPT FROM V$DB_OBJECT_CACHE;
 
20、查看V$SQLAREA:
SQL>SELECT SQL_TEXT,SHARABLE_MEM,PERSISTENT_MEM,RUNTIME_MEM,SORTS,
VERSION_COUNT,LOADED_VERSIONS,OPEN_VERSIONS,USERS_OPENING,EXECUTIONS,
USERS_EXECUTING,LOADS,FIRST_LOAD_TIME,INVALIDATIONS,PARSE_CALLS,
DISK_READS,BUFFER_GETS,ROWS_PROCESSED FROM V$SQLAREA;
 
21、查看object分类数量:
SELECT DECODE(O.TYPE#,1,'INDEX',2,'TABLE',3,'CLUSTER',4,'VIEW',5,'SYNONYM',6, 'SEQUENCE','OTHER') OBJECT_TYPE , COUNT(*) QUANTITY FROM SYS.OBJ$ O WHERE O.TYPE# > 1 GROUP BY DECODE(O.TYPE#,1,'INDEX',2,'TABLE',3,'CLUSTER' ,4,'VIEW',5,'SYNONYM',6,'SEQUENCE','OTHER') UNION SELECT 'COLUMN', COUNT(*) FROM SYS.COL$ UNION SELECT 'DB LINK' , COUNT(*) FROM ALL_OBJECTS;

22、有关connection的相关信息:
1)查看有哪些用户连接
SELECT S.OSUSER OS_USER_NAME,DECODE(SIGN(48 - COMMAND),1,TO_CHAR(COMMAND),
 'ACTION CODE #' || TO_CHAR(COMMAND))ACTION,P.PROGRAM ORACLE_PROCESS, STATUS SESSION_STATUS,S.TERMINAL TERMINAL,S.PROGRAM PROGRAM, S.USERNAME USER_NAME,S.FIXED_TABLE_SEQUENCE ACTIVITY_METER,''QUERY,0 MEMORY,0 MAX_MEMORY,0 CPU_USAGE,S.SID,S.SERIAL# SERIAL_NUM FROM V$SESSION S,V$PROCESS P WHERE S.PADDR=P.ADDR AND S.TYPE = 'USER' ORDER BY S.USERNAME, S.OSUSER;

2)根据v.sid查看对应连接的资源占用等情况
SELECT N.NAME,V.VALUE,N.CLASS,N.STATISTIC# FROM V$STATNAME N,V$SESSTAT V WHERE V.SID=18 AND V.STATISTIC# = N.STATISTIC# ORDER BY N.CLASS, N.STATISTIC#;

3)根据sid查看对应连接正在运行的sql
SELECT /*+ PUSH_SUBQ */ COMMAND_TYPE,SQL_TEXT,SHARABLE_MEM, PERSISTENT_MEM,RUNTIME_MEM,SORTS,VERSION_COUNT,LOADED_VERSIONS,OPEN_VERSIONS,USERS_OPENING,EXECUTIONS, USERS_EXECUTING,LOADS,FIRST_LOAD_TIME,INVALIDATIONS,PARSE_CALLS,DISK_READS,BUFFER_GETS,ROWS_PROCESSED,SYSDATE START_TIME,SYSDATE FINISH_TIME,'>'|| ADDRESS SQL_ADDRESS, 'N' STATUS FROM V$SQLAREA WHERE ADDRESS = (SELECT SQL_ADDRESS FROM V$SESSION WHERE SID=8);

根据pid查看sql语句:

SELECT SQL_TEXT FROM V$SQL WHERE ADDRESS IN (SELECT SQL_ADDRESS FROM V$SESSION WHERE SID IN (SELECT SID FROM V$SESSION WHERE PADDR IN (SELECT ADDR FROM V$PROCESS WHERE SPID=&PID)));

时间: 2024-09-18 07:31:54

常用SQL語句2的相关文章

常用SQL語句3

 23.查询表空间使用情况:SELECT A.TABLESPACE_NAME "空间名称", 100-ROUND((NVL(B.BYTES_FREE,0)/A.BYTES_ALLOC)*100,2) "占用率(%)", ROUND(A.BYTES_ALLOC/1024/1024,2) "容量(M)",ROUND(NVL(B.BYTES_FREE,0)/1024/1024,2) 空闲(M)",ROUND((A.BYTES_ALLOC-NV

常用SQL語句4

 46.造成等待的LOCK的信息,比如LOCK类型等:COL EVENT FORMAT A30 SET LINE 160 COL MACHINE FORMAT A10 COL USERNAME FORMAT A15 SELECT B.SID,B.SERIAL#,B.USERNAME,MACHINE,EVENT,WAIT_TIME,CHR(BITAND(P1,-16777216)/16777215)||CHR(BITAND(P1, 16711680)/65535) "ENQUEUE TYPE&qu

常用SQL語句

查看表空间的名称及大小:   SQL> SELECT T.TABLESPACE_NAME, ROUND(SUM(BYTES/(1024 * 1024)), 0) TS_SIZE FROM DBA_TABLESPACES T, DBA_DATA_FILES D WHERE T.TABLESPACE_NAME = D.TABLESPACE_NAME GROUP BY T.TABLESPACE_NAME; 查看表空间物理文件的名称及大小: SQL> SELECT TABLESPACE_NAME,FI

如何得到sqlcommandbuilder﹐sqldataadpter自動產生的updatecommand,deletecommand,insertcommand的完整sql語句。

问题描述 如何得到sqlcommandbuilder﹐sqldataadpter自動產生的updatecommand,deletecommand,insertcommand的完整sql語句. 解决方案 解决方案二:up解决方案三:SqlConnectionconnection=newSqlConnection(connectionString);SqlDataAdapteradapter=newSqlDataAdapter();adapter.SelectCommand=newSqlComman

web 程序出現以下錯誤,從錯誤來看是sqlce出錯,可是把SQL語句執行后并沒有出錯

问题描述 失敗的應用程式名稱:w3wp.exe,版本:7.5.7601.17514,時間戳記:0x4ce7a5f8失敗的模組名稱:sqlceme40.dll,版本:4.0.8876.1,時間戳記:0x4fcd14b9例外狀況碼:0xc0000005錯誤位移:0x000057be失敗的處理程序識別碼:0x139c失敗的應用程式開始時間:0x01d18f0f07873327失敗的應用程式路徑:c:windowssystem32inetsrvw3wp.exe失敗的模組路徑:C:ProgramFiles

DBA常用SQL语句

语句    自己总结的常用SQL语句,发现对自己工作帮助挺大的!   查看表空间的名称及大小: SQL>select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name;   查看表空间物理文件的名称及大小:

[SQL Server]管理常用SQL语句

server|语句 [SQL Server]管理常用SQL语句 1. 查看数据库的版本        select @@version 2. 查看数据库所在机器操作系统参数        exec master..xp_msver 3. 查看数据库启动的参数         sp_configure 4. 查看数据库启动时间         select convert(varchar(30),login_time,120) from master..sysprocesses where spi

Access及SQL Server操作数据库结构的常用SQL语句

access|server|sql|数据|数据库|数据库结构|语句 下面是Sql Server 和 Access 操作数据库结构的常用Sql,希望对你有所帮助.内容由海娃整理,不正确与不完整之处还请提出,谢谢. 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default '默认值' null ,[字段2] ntext null ,[字段3] datetime,[字段4] mon

Oracle 常用SQL函数

oracle|函数 时间:2005-02-18 Oracle的SQL函数分为单行函数和多行函数.单行函数只对单条记录有效,多行函数对多条记录有效. 单行函数包括,字符.数字.日期.转换和普通函数. 字符函数举例:• 全小写  LOWER('SQL Course')   sql course• 全大写 UPPER('SQL Course')     SQL COURSE •首字母大写 INITCAP('SQL Course')    Sql Course拼接 CONCAT('Good', 'Str