常用oracle查询总结

 --查询表空间使用情况
  SELECT
UPPER(F.TABLESPACE_NAME) "表空间名",
  D.TOT_GROOTTE_MB
"表空间大小(M)",
  D.TOT_GROOTTE_MB
- F.TOTAL_BYTES "已使用空间(M)",
  TO_CHAR(ROUND((D.TOT_GROOTTE_MB
- F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),‘990.99‘) || ‘%‘ "使用比",
  F.TOTAL_BYTES
"空闲空间(M)",
  F.MAX_BYTES
"最大块(M)"
  FROM
(SELECT TABLESPACE_NAME,
  ROUND(SUM(BYTES)
/ (1024 * 1024), 2) TOTAL_BYTES,
  ROUND(MAX(BYTES)
/ (1024 * 1024), 2) MAX_BYTES
  FROM
SYS.DBA_FREE_SPACE
  GROUP
BY TABLESPACE_NAME) F,
  (SELECT
DD.TABLESPACE_NAME,
  
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
  FROM
SYS.DBA_DATA_FILES DD
  GROUP
BY DD.TABLESPACE_NAME) D
  WHERE
D.TABLESPACE_NAME = F.TABLESPACE_NAME
  ORDER
BY 1;
  --查询表空间的free
space
  select
tablespace_name,
  count(*)
as extends,
  round(sum(bytes)
/ 1024 / 1024, 2) as MB,
  sum(blocks)
as blocks
  from
dba_free_space
  group
by tablespace_name;

  --查询表空间的总容量
  select
tablespace_name, sum(bytes) / 1024 / 1024 as MB
  from
dba_data_files
  group
by tablespace_name;

  --查询表空间使用率
  select
total.tablespace_name,
  round(total.MB,
2) as Total_MB,考试大论坛
  round(total.MB
- free.MB, 2) as Used_MB,
  round((1
- free.MB / total.MB) * 100, 2) || ‘%‘ as Used_Pct
  from
(select tablespace_name, sum(bytes) / 1024 / 1024 as MB
  from
dba_free_space
  group
by tablespace_name) free,
  (select
tablespace_name, sum(bytes) / 1024 / 1024 as MB
  from
dba_data_files
  group
by tablespace_name) total
  where
free.tablespace_name = total.tablespace_name;

1.查找当前表级锁的SQL如下:
select sess.sid,
    sess.serial#,

    lo.oracle_username,
   
lo.os_user_name,
    ao.object_name,
   
lo.locked_mode
    from v$locked_object lo,

    dba_objects ao,
    v$session sess

where ao.object_id = lo.object_id and lo.session_id = sess.sid;

2.杀掉锁表进程:
alter system kill session ‘436,35123‘;

3.RAC环境中锁查找:
SELECT inst_id,DECODE(request,0,‘Holder: ‘,‘Waiter: ‘)||sid
sess,
        id1, id2, lmode, request,
type,block,ctime
FROM GV$LOCK
WHERE (id1, id2, type)
IN
       (SELECT id1, id2, type FROM GV$LOCK
WHERE request>0)
ORDER BY id1, request;

4.监控当前数据库谁在运行什么SQL语句
select osuser, username, sql_text 

from  v$session a, v$sqltext b
where  a.sql_address =b.address
order by address, piece;

5.找使用CPU多的用户session
select a.sid,spid,status,substr(a.program,1,40) prog,
a.terminal,osuser,value/60/100 value
from  v$session a,v$process
b,v$sesstat c
where  c.statistic#=12 and 

       c.sid=a.sid and 

       a.paddr=b.addr 

       order by value desc;

6.查看死锁信息
SELECT (SELECT
username
          FROM
v$session
         WHERE SID = a.SID)
blocker, a.SID, ‘is blocking‘,
       (SELECT
username
          FROM
v$session
         WHERE SID = b.SID)
blockee, b.SID
  FROM v$lock a, v$lock b
 WHERE a.BLOCK = 1 AND
b.request > 0 AND a.id1 = b.id1 AND a.id2 = b.id2;

7.具有最高等待的对象
SELECT   o.OWNER,o.object_name, o.object_type,
a.event,
         SUM (a.wait_time +
a.time_waited) total_wait_time
    FROM
v$active_session_history a, dba_objects o
   WHERE a.sample_time
BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
     AND
a.current_obj# = o.object_id
GROUP BY o.OWNER,o.object_name, o.object_type,
a.event
ORDER BY total_wait_time DESC;

SELECT   a.session_id, s.osuser, s.machine, s.program, o.owner,
o.object_name,
        
o.object_type, a.event,
         SUM
(a.wait_time + a.time_waited) total_wait_time
    FROM
v$active_session_history a, dba_objects o, v$session s
   WHERE
a.sample_time BETWEEN SYSDATE - 30 / 2880 AND
SYSDATE
     AND a.current_obj# =
o.object_id
     AND a.session_id = s.SID
GROUP BY
o.owner,
        
o.object_name,
        
o.object_type,
        
a.event,
        
a.session_id,
        
s.program,
        
s.machine,
         s.osuser
ORDER
BY total_wait_time DESC;

8.查询当前连接会话数
select s.value,s.sid,a.username
from
v$sesstat
S,v$statname N,v$session A
where
n.statistic#=s.statistic#
and
name=‘session pga memory‘
and s.sid=a.sid
order by s.value;

9.等待最多的用户
SELECT   s.SID, s.username, SUM (a.wait_time +
a.time_waited) total_wait_time
    FROM
v$active_session_history a, v$session s
   WHERE a.sample_time
BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
GROUP BY s.SID, s.username
ORDER
BY total_wait_time DESC;

10.等待最多的SQL
SELECT   a.program, a.session_id, a.user_id,
d.username, s.sql_text,
         SUM
(a.wait_time + a.time_waited) total_wait_time
    FROM
v$active_session_history a, v$sqlarea s, dba_users d
   WHERE
a.sample_time BETWEEN SYSDATE - 30 / 2880 AND
SYSDATE
     AND a.sql_id =
s.sql_id
     AND a.user_id = d.user_id
GROUP BY
a.program, a.session_id, a.user_id, s.sql_text, d.username;

11.查看消耗资源最多的SQL
SELECT hash_value, executions, buffer_gets, disk_reads,
parse_calls
FROM V$SQLAREA
WHERE buffer_gets > 10000000 OR disk_reads
> 1000000
ORDER BY buffer_gets + 100 * disk_reads DESC;

12.查看某条SQL语句的资源消耗
SELECT hash_value, buffer_gets, disk_reads, executions,
parse_calls
FROM V$SQLAREA
WHERE hash_Value = 228801498 AND address =
hextoraw(‘CBD8E4B0‘);

13.查询会话执行的实际SQL
SELECT   a.SID, a.username,
s.sql_text
    FROM v$session a, v$sqltext s
  
WHERE a.sql_address = s.address
     AND a.sql_hash_value
= s.hash_value
     AND a.status = ‘ACTIVE‘
ORDER BY
a.username, a.SID, s.piece;

14.显示正在等待锁的所有会话
SELECT * FROM DBA_WAITERS;

时间: 2024-10-15 17:21:39

常用oracle查询总结的相关文章

ORACLE常用系统查询

 1  查询系统所有对象 SELECT OWNER, OBJECT_NAME, OBJECT_TYPE, CREATED, LAST_DDL_TIME, TIMESTAMP, STATUS FROM DBA_OBJECTS WHERE OWNER=UPPER('SCOTT') 2  查看系统所有表 SELECT OWNER, TABLE_NAME, TABLESPACE_NAME FROM DBA_TABLES 3  查看所有用户的表 SELECT OWNER, TABLE_NAME, TA

常用数据库查询判断表和字段是否存在的SQL

常用数据库查询判断表和字段是否存在的SQL(如果结果为1表示存在,为0表示不存在) 1.MSSQL Server 表: SELECT COUNT(*) FROM dbo.sysobjects  WHERE name= 'table_name'; 字段: SELECT COUNT(*) FROM syscolumns  WHERE id=object_id('table_name') AND name= 'column_name'; 2.My SQL 表: SELECT COUNT(*) FROM

软件开发中oracle查询常用方法总结

上次新霸哥和大家讲解了一些关于oracle的知识发现大家对oracle还是比较感兴趣的,下面新霸哥就大家比较关系的oracle中常用的查询有哪几种?做个和oracle相关的开发的朋友可能会知道答案,但是刚接触过oracle的朋友可能还不知道是怎么回事.今天,新霸哥将在此给大家做一个详细的介绍. 多表连接查询 连接多表 1. 笛卡尔积 在没有任何过滤条件的情况下,左表的任意一行记录 与 右表的任意记录进行连接 2. 使用连接查询 下面新霸哥简单的介绍一下语法1:select columnList

ORACLE: 查询(看)表的主键、外键、唯一性约束和索引

ORACLE: 查询(看)表的主键.外键.唯一性约束和索引 1.查找表的所有索引(包括索引名,类型,构成列) select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 表名 2.查找表的主键(包括名称,构成列): select cu.* from user_co

Oracle 查询(SELECT)语句(二)

?  简介 在前面的 Oracle 查询 SELECT 语句(一) 中介绍了 SELECT 常用的一些基本查询语法,接下来再来看 SELECT 更深入的一些查询功能和技巧,包括以下内容: 1.   All 与 Any 运算符 2.   分页查询(rownum) 3.   集合操作符(UNION.UNION ALL.INTERSECT.MINUS) 1.   All 与 Any 运算符 1)   All 运算符,表示满足给出列表中的所有值.通常用于以下场景: 1.   查出大于30号部门所有员工最

oracle查询转换_inlist转换

oracle的optimizer会对一些sql语句进行查询转换,比如: 合并视图 子查询非嵌套化 inlist转换 下面讲讲遇到的in list转化优化的案例: create table test( col1 varchar2(12) col2 number ext varchar2(4000) ); create index test_ind on test(user_id, col2); create sequence seq_test cache 200; 第一步:准备一些测试数据(10个

oracle查询转换_view merge

oracle对于子查询的支持做的很好,oracle optimizer会对inline view进行query transfomation,即视图合并,不过也经常带来意想不到的问题.下面是一个inline view的merge的例子: 1, 创建临时表 1 create table test1 as select * from dba_objects; 2 create table test2 as select * from dba_objects; 2, 以下查询语句 select * fr

Oracle查询总结

-------------------------------多表查询-------------------------------------- --笛卡尔积查询. select * from emp,dept;--结果为64条,emp表有14条,dept表有4条 --别名查询 --改变查询后的结果显示的列名,在字段后面写要显示的列名(注意一定要用双引号括起来,oracle查询中之后这里用到双引号!) select ename "name" ,loc "地点" f

45个非常有用的 Oracle 查询语句小结

 这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快快收藏吧. 原文地址:http://www.jbxue.com/db/19890.html 日期/时间 相关查询 1.获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 "SYSDATE"来指定查询的日期. 复制代码代码如下: SELECT TRUNC