《Oralce系列》Oracle 常用语句汇总

本文详细讲解了如何在Oracle数据库中进行连接计数、资源查看、锁定与解锁操作,以及分析SQL执行情况、表数据统计、用户权限管理、索引使用情况和数据清理等任务的SQL语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询当前数据库的连接数

select count(*) from gv$process;

查询当前数据库允许的最大连接数

select value from gv$parameter where name = 'processes';

查询当前数据库的资源限制

select * from gv$resource_limit;

查询当前数据库的锁表语句

select b.owner, b.object_name, a.session_id, a.locked_mode
  from gv$locked_object a, dba_objects b
 where b.object_id = a.object_id;

查询当前数据库的锁表程序

select b.owner,
       b.object_name,
       a.session_id,
       c.machine,
       c.osuser,
       c.process,
       c.program,
       a.locked_mode,
       c.client_info
  from gv$locked_object a, dba_objects b, gv$session c
 where b.object_id = a.object_id
   and a.session_id = c.sid;

生成强制解锁语句

select 'alter system kill session ''' || SID || ',' || SERIAL# || ''';',
       lo.oracle_username,
       lo.os_user_name,
       ao.object_name,
       lo.locked_mode
  from gv$locked_object lo, dba_objects ao, gv$session sess
 where ao.object_id = lo.object_id
   and lo.session_id = sess.sid;

查询单个进程所占的会话数

select s.machine,
       s.client_info,
       s.process,
       s.program,
       s.username,
       count(*) count
  from gv$session s
 where s.program = '<进程名称>'
 group by s.machine, s.client_info, s.process, s.program, s.username
 order by count desc;

查询每台服务器进程占用的总会话数

select s.machine, s.client_info, count(*) count
  from gv$session s
 where s.program = '<进程名称>'
 group by s.machine, s.client_info
 order by count desc;

查询进程服务占的总会话数

select count(*) count
  from gv$session s
 where s.program = '<进程名称>';

查询当前数据库查询次数最多的SQL语句

select *
  from (select s.sql_text,
               s.executions "执行次数",
               s.parsing_user_id "用户名",
               rank() over(order by executions desc) exec_rank
          from v$sql s
          left join all_users u
            on u.user_id = s.parsing_user_id) t
 where exec_rank <= 100;

查询当前数据库执行查询最慢的前50条语句

select *
  from (select sa.SQL_TEXT,
               sa.SQL_FULLTEXT,
               sa.EXECUTIONS "执行次数",
               round(sa.ELAPSED_TIME / 1000000, 2) "总执行时间",
               round(sa.ELAPSED_TIME / 1000000 / sa.EXECUTIONS, 2) "平均执行时间",
               sa.COMMAND_TYPE,
               sa.PARSING_USER_ID "用户ID",
               u.username "用户名",
               sa.HASH_VALUE
          from v$sqlarea sa
          left join all_users u
            on sa.PARSING_USER_ID = u.user_id
         where sa.EXECUTIONS > 0
         order by (sa.ELAPSED_TIME / sa.EXECUTIONS) desc)
 where rownum <= 50;

查询表的数据条数、表名、中文表名

select a.num_rows, a.table_name, b.comments
  from user_tables a, user_tab_comments b
 where a.table_name = b.table_name
 order by table_name;

查询当前数据库中单表数据大约10万条的所有表

select table_name, num_rows
  from user_tables
 where num_rows >= 100000
 order by num_rows desc;

查询当前数据库的所有表空间

select * from v$tablespace;

查询当前用户下的所有table、view、sequence、trigger等信息

select * from user_tables;
select * from user_views;
select * from user_sequences;
select * from user_triggers;

查询当前数据库表空间的位置

select * from dba_data_files;

查询当前数据库某用户下的表空间

select tablespace_name
  from dba_segments
 where owner = '<用户名>'
 group by tablespace_name;

查询当前数据库的所有用户

select * from dba_users;
select * from all_users;
select * from user_users;

查询当前数据库中各表使用索引的数量

select table_name, count(*) index_count
  from user_indexes
 group by table_name
 order by index_count desc;

生成删表语句(表名中包含某个字符)

select 'drop table ' || lower(table_name) || ';'
  from user_tables
 where upper(table_name) like '%TEMP%'
 order by table_name asc;

生成清理表中数据语句(表名中包含某个字符)

select 'truncate table ' || lower(table_name) || ';'
  from user_tables
 where table_name like '%TEMP%'
 order by table_name asc;

生成删表语句(表名中包含数字)

select 'drop table ' || lower(table_name) || ';'
  from user_tables
 where regexp_like(upper(table_name), '[0-9]*[0-9]')
 order by table_name asc;

数据闪回

-- 闪回查询
select * from table_name as of timestamp to_timestamp('2025-05-17 08:00:00','yyyy-mm-dd hh24:mi:ss');
-- 启用行移动
alter table table_name enable row movement;
-- 闪回表到固定时间点
flashback table table_name to timestamp to_timestamp('2025-05-17 08:00:00','yyyy-mm-dd hh24:mi:ss');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

劉煥平CHN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值