查看各项Oracle数据库信息



一、数据库
数据库名称 select name from v$database;
数据库唯一名称 select db_unique_name from v$database;
数据库ID select dbid from v$database;
数据库创建时间 select to_char(created,'yyyy/mm/dd hh24:mi:ss') from v$database;
日志归档模式 select log_mode from v$database;
打开模式 select open_mode from v$database;
数据库角色 select database_role from v$database;
服务器参数文件 select value from v$parameter where name='spfile';
控制文件列表 select value from v$parameter2 where name='control_files';
二、数据文件
表空间列表 select name from v$tablespace;
数据文件列表 select name from v$datafile;
临时文件列表 select name from v$tempfile;
日志组列表 select group#, bytes/1024/1024||'M' from v$log;
在线日志文件列表 select max(length(member)) from v$logfile;
column member format a36
select group#, member from v$logfile;
三、备份文件
归档日志文件列表 select count(*) from v$archived_log;
select count(*) from v$archived_log where name is not null;
select name from v$archived_log where name is not null;
备份文件列表 select count(*) from v$backup_piece;
select count(*) from v$backup_piece where handle is not null;
select handle from v$backup_piece where start_time >= sysdate-1 order by handle;
四、用户与模式对象
数据库用户列表 select username from dba_users order by created;
模式对象数量列表 desc dba_objects
select owner, object_type, count(*)
  from dba_objects
 group by owner, object_type
 order by owner, object_type;
五、实例与会话
主机名称 select host_name from v$instance;
实例名称 select instance_name from v$instance;
服务名称 select value from v$parameter where name='service_names';
数据库软件版本 select version from v$instance;
实例启动时间 select to_char(startup_time,'yyyy/mm/dd hh24:mi:ss') from v$instance;
实例状态 select status from v$instance;
当前会话列表 select sid, serial#, username from v$session;
文件的大小与分布
数据文件总大小 select sum(bytes)/1024/1024||'M' from v$datafile;
select round(sum(bytes)/1024/1024/1024)||'G' from v$datafile;
临时文件总大小 select sum(bytes)/1024/1024||'M' from v$tempfile;
select round(sum(bytes)/1024/1024/1024)||'G' from v$tempfile;
在线日志文件总大小 select sum(bytes*members)/1024/1024||'M' from v$log;
select round(sum(bytes*members)/1024/1024/1024)||'G' from v$log;
控制文件总大小 select sum(block_size*file_size_blks)/1024/1024||'M' from v$controlfile;
归档日志文件总大小 select count(*) from v$archived_log;
select min(block_size*blocks) from v$archived_log;
select max(block_size*blocks)/1024/1024||'M' from v$archived_log;
select sum(block_size*blocks)/1024/1024/1024||'G' from v$archived_log;
select round(nvl(sum(block_size*blocks),0)/1024/1024/1024)||'G' from v$archived_log where name is not null;
备份文件总大小 select count(*) from v$backup_piece;
select sum(bytes)/1024/1024/1024||'G' from v$backup_piece;
select round(sum(bytes)/1024/1024/1024)||'G' from v$backup_piece where handle is not null;
数据文件分布 select distinct substr(name, 1, instr(name, '/', -1) ) from v$datafile;
临时文件分布 select distinct substr(name, 1, instr(name, '/', -1) ) from v$tempfile;
在线日志文件分布 select distinct substr(member, 1, instr(member, '/', -1) ) from v$logfile;
控制文件分布 select distinct substr(name, 1, instr(name, '/', -1) ) from v$controlfile;
归档日志文件分布 select distinct substr(name, 1, instr(name, '/', -1) ) from v$archived_log;
备份文件分布 select distinct substr(handle, 1, instr(handle, '/', -1) ) from v$backup_piece;
服务器参数文件位置 select substr(value, 1, instr(value, '/', -1) ) from v$parameter where name='spfile';
告警日志位置 select value from v$parameter where name='background_dump_dest';
ho ls -l /opt/app/oracle/admin/DAL2AP/bdump/alert*
后台跟踪日志位置 select value from v$parameter where name='background_dump_dest';
用户跟踪日志位置 select value from v$parameter where name='user_dump_dest';
内核跟踪日志位置 select value from v$parameter where name='core_dump_dest';
磁盘空间使用情况
各表空间空间占用率 column "Total_Space" for a10
column "Free_Space" for a10
column "Used_Space" for a10
column "Free_Percentage" for a6
column "Used_Percentage" for a6
set pagesize 0
select df.tablespace_name,
       round(df.bytes/1024/1024,2)||'M' as "Total_Space",
       round(nvl(fs.bytes,0)/1024/1024,2)||'M' as "Free_Space",
       round((df.bytes-nvl(fs.bytes,0))/1024/1024,2)||'M' as "Used_Space",
       round(nvl(fs.bytes,0)/df.bytes,4)*100||'%' as "Free_Percentage",
       round(1-nvl(fs.bytes,0)/df.bytes,4)*100||'%' as "Used_Percentage"
  from (select tablespace_name, sum(bytes) as bytes
          from dba_data_files
         group by tablespace_name) df,
       (select tablespace_name, sum(bytes) as bytes
          from dba_free_space
         group by tablespace_name) fs
 where df.tablespace_name = fs.tablespace_name(+)
 order by df.tablespace_name;
主机资源使用情况
主机资源使用情况 desc v$resource_limit
select max(length(resource_name)) from v$resource_limit;
column resource_name format a21
select resource_name,
       current_utilization,
       max_utilization,
       initial_allocation,
       limit_value
  from v$resource_limit;
版本
数据库版本 desc v$version
select banner from v$version;
数据库选项 desc v$option
select max(length(parameter)), max(length(value)) from v$option;
column parameter format a35
column value format a5
select parameter, value from v$option;
系统参数
最大进程数 select value from v$parameter where name = 'processes';
最大会话数 select value from v$parameter where name = 'sessions';
数据块大小 select value from v$parameter where name = 'db_block_size';
游标共享模式 select value from v$parameter where name = 'cursor_sharing';
多块读批量 select value from v$parameter where name = 'db_file_multiblock_read_count';
回滚段表空间 select value from v$parameter where name = 'undo_tablespace';
回滚段保留时间 select value from v$parameter where name = 'undo_retention';
回滚段管理模式 select value from v$parameter where name = 'undo_management';
系统全局区目标大小 select value/1024/1024||'M' from v$parameter where name = 'sga_target';
系统全局区最大值 select value/1024/1024||'M' from v$parameter where name = 'sga_max_size';
程序全局区目标和 select value/1024/1024||'M' from v$parameter where name = 'pga_aggregate_target';
时间统计 select value from v$parameter where name = 'timed_statistics';
初始化参数文件 select value from v$parameter where name = 'ifile';
数据库名称 select value from v$parameter where name='db_name';
数据库唯一名称 select value from v$parameter where name='db_unique_name';
实例名称 select value from v$parameter where name='instance_name';
服务名称 select value from v$parameter where name='service_names';
当前用户概况
段的总数量 -- 查看段的数量
select count(*)  -- 段的总数量
  from user_segments;
各类型段的数量 -- 查看各种类型的段的数量
select segment_type,  -- 段的类型
       count(*)       -- 该类型的段的数量
  from user_segments
 group by segment_type
 order by segment_type;
大段列表 desc user_segments
select max(length(segment_name)) from user_segments;
column segment_name format a30
column mb format a8
select max(length(tablespace_name)) from dba_tablespaces;
column tablespace_name format a13
-- 按类型查看段的基本信息
select segment_name,     -- 段的名称
       tablespace_name,  -- 所在的表空间的名称
       round(bytes/1024/1024)||'MB' as MB,
       extents           -- 包含的区的数量
  from user_segments
 where segment_type = 'TABLE'
   and bytes >= (1024*1024) * 10
   and rownum <=5
 order by bytes desc;
段的基本信息 -- 查看某个段的基本信息
select segment_name,    -- 段的名称
       partition_name,
       segment_type,    -- 段的类型
       segment_subtype,
       tablespace_name, -- 表空间的名称
       bytes,           -- 段的大小
       blocks,          -- 块的数量
       extents          -- 区的数量
  from user_segments
 where segment_name = 'POSTPAY_BILLED_REVENUE';
区的列表 -- 查看某个段的区列表
select segment_name,  -- 段的名称
       extent_id,     -- 区的编号
       bytes          -- 区的大小
  from user_extents
 where segment_name = 'POSTPAY_BILLED_REVENUE'
 order by extent_id;
区的大小分布 -- 区的大小分布分析
select segment_name,    -- 段的名称
       bytes/1024/1024, -- 区的大小
       count(*)         -- 区的数量
  from user_extents
 where segment_name = 'POSTPAY_BILLED_REVENUE'
 group by segment_name, bytes
 order by bytes;
用户存储空间消耗 -- 计算整个用户占用的存储空间
select user,   -- 数据库用户名
       round(sum(bytes/1024/1024/1024),2) as space_GB -- 占用存储空间大小
  from user_segments;
前20个大段 -- 列出占用存储空间最多的前20个段
select segment_name,    -- 段的名称
       segment_type,    -- 段的类型
       tablespace_name, -- 所在表空间
       bytes,           -- 段的大小
       round(bytes/1024/1024/1024, 3) as space_GB
  from (
         select segment_name,
                segment_type,
                tablespace_name,
                bytes,
                blocks,
                extents
           from user_segments
          order by bytes desc
        )
 where rownum <= 10;
表的基本信息 -- 查看表的基本信息
select table_name,      -- 表的名称
       tablespace_name, -- 表空间的名称
       num_rows,        -- 记录数
       avg_row_len,     -- 行的平均长度
       last_analyzed,   -- 统计信息收集时间
       sample_size      -- 统计信息收集样本
  from user_tables
 where table_name = 'POSTPAY_BILLED_REVENUE';
字段的基本信息 -- 查看字段的基本信息
select table_name,   -- 表的名称
       column_id,    -- 字段的顺序
       column_name,  -- 字段的名称
       data_type,    -- 数据类型
       data_length,  -- 字段长度
       nullable,     -- 是否允许空值
       num_distinct, -- 不同值的个数
       num_nulls     -- 空值记录数
  from user_tab_columns
 where table_name = 'POSTPAY_BILLED_REVENUE'
 order by column_id;
表的备注 -- 查看表的备注
select table_name,  -- 名称
       table_type,  -- 类型
       comments     -- 备注
  from user_tab_comments
 where table_name = 'POSTPAY_BILLED_REVENUE';
字段的备注 -- 查看字段的备注
select table_name,   -- 表的名称
       column_name,  -- 字段的名称
       comments      -- 备注
  from user_col_comments
 where table_name = 'POSTPAY_BILLED_REVENUE';
表的约束 -- 查看表的约束
select owner,
       constraint_name, -- 约束的名称
       constraint_type, -- 约束的类型
       table_name       -- 归属表的名称
  from user_constraints
 where table_name = 'POSTPAY_BILLED_REVENUE'
   and constraint_type = 'P';
字段的约束 -- 查看约束的字段
select owner,
       constraint_name, -- 约束的名称
       table_name,      -- 归属表的名称
       column_name,     -- 字段的名称
       position         -- 字段的顺序
  from user_cons_columns
 where constraint_name = 'POSTPAY_BILLED_REVENUE_PK'
 order by position;
索引的列表 -- 查看索引的列表
select table_name,      -- 表的名称
       index_name,      -- 索引的名称
       index_type,      -- 索引类型
       uniqueness,      -- 是否唯一索引
       tablespace_name, -- 表空间名称
       leaf_blocks,       -- 叶子块数量
       distinct_keys,     -- 键值数
       clustering_factor, -- 聚合因子
       num_rows,          -- 记录数
       last_analyzed      -- 统计信息收集时间
  from user_indexes
 where table_name = 'POSTPAY_BILLED_REVENUE';
索引的字段 -- 查看索引的字段
select table_name,      -- 表名称
       index_name,      -- 字段名称
       column_position, -- 字段顺序
       column_name      -- 字段名称
  from user_ind_columns
 where table_name = 'POSTPAY_BILLED_REVENUE'
 order by index_name, column_position;
对象总数量 -- 查看对象的数量
select count(*)
  from user_objects;
对象分类数量 -- 查看各种类型对象的数量
select object_type, -- 对象的类型
       count(*)     -- 对象的数量
  from user_objects
 group by object_type
 order by object_type;
数据库链接列表 -- 查看数据库链接
select * from user_db_links;
大对象列表 -- 查看大对象
select * from user_lobs;
物化视图列表 -- 查看物化视图
select * from user_mviews;
视图列表 -- 查看视图
select * from user_views;
了解业务表的例子 --
-- 了解某个业务表的例子
--
-- 看一下段有多大
select segment_name,    -- 段的名称
       segment_type,    -- 段的类型
       tablespace_name, -- 表空间的名称
       round(bytes/1024/1024/1024,2)||'GB' as space_GB -- 段的大小
  from user_segments
 where segment_name = 'CUSTOMER'
   and segment_type = 'TABLE';

-- 看一下表有多少条记录
select table_name,      -- 表的名称
       tablespace_name, -- 表空间的名称
       num_rows,        -- 记录数
       last_analyzed    -- 统计信息收集时间
  from user_tables
 where table_name = 'CUSTOMER';

-- 找出主键的名称和字段列表
select owner,
       constraint_name, -- 约束的名称
       constraint_type, -- 约束的类型
       table_name       -- 归属表的名称
  from user_constraints
 where table_name = 'CUSTOMER'
   and constraint_type = 'P';

--
select owner,
       constraint_name, -- 约束的名称
       table_name,      -- 归属表的名称
       column_name,     -- 字段的名称
       position         -- 字段的顺序
  from user_cons_columns
 where constraint_name = 'SYS_C0010843'
   and table_name = 'CUSTOMER'
 order by position;

-- 看看表有哪些索引以及索引的字段
select table_name,      -- 表的名称
       index_name       -- 索引的名称
  from user_indexes
 where table_name = 'CUSTOMER';

--
select table_name,      -- 表名称
       index_name,      -- 字段名称
       column_position, -- 字段顺序
       column_name      -- 字段名称
  from user_ind_columns
 where table_name = 'CUSTOMER'
 order by index_name, column_position;

-- 随机检索10条记录的主键字段
select customer_id
  from customer
 where rownum <= 10;

-- 查看主键字段的数据类型
select table_name,   -- 表的名称
       column_name,  -- 字段的名称
       data_type,    -- 数据类型
       num_distinct, -- 不同值的个数
       num_nulls     -- 空值记录数
  from user_tab_columns
 where table_name = 'CUSTOMER'
   and column_name in ('CUSTOMER_ID');

-- 获取某条记录的详细信息
select *
  from customer
 where customer_id = '-22830735';

--
select customer_id,
       icms_customer_no,
       account_type,
       active_flag,
       icms_subtype,
       line_no,
       sa_create_date
  from customer
 where customer_id = '-22830735';
常用基本脚本
LONG的显示设置 -- 设置LONG类型字段的显示长度
show long
set long 7763
分页设置 -- 设置分页大小,避免频繁显示标题
show pagesize
set pagesize 0
视图长度 -- 查看视图定义的长度
select owner, view_name, text_length from dba_views where view_name='&view_name';
视图定义 -- 从数据字典视图中直接查询视图的定义
select text from dba_views where view_name='&view_name';
视图定义 -- 通过应用程序编程接口获取视图的定义
select dbms_metadata.get_ddl('VIEW','&view_name','CAMPAIGN') from dual;
表的归属 -- 根据表的名称,查找表在哪个用户下
select owner, table_name from dba_tables where table_name='&table_name';
段的大小 -- 根据段名查找段的大小
select owner, segment_name, segment_type, bytes from dba_segments where segment_name='&segment_name';
多个段的大小 select owner||'.'||segment_name as segment, segment_type, bytes/1024/1024||'M' as MB
  from dba_segments
 where segment_name in ('&segment_name');
索引的归属 -- 根据索引的名称,查找相关的表名和用户名
select owner, index_name, table_owner, table_name from dba_indexes where index_name='&index_name';
表的索引 -- 根据表的名称查找索引
select owner, index_name, table_owner, table_name from dba_indexes where table_name='&table_name';
表的状态 select owner, index_name, status, LAST_ANALYZED from dba_indexes where table_name='&table_name';
表的索引字段 -- 根据表的名称查找索引的字段
select index_name, column_name, column_position from dba_ind_columns where table_name='&table_name';
索引的字段 select index_name, column_name, column_position from dba_ind_columns where index_name='&index_name';
数据分布 explain plan for select /* + index(a CCONTACTHIST_IX3) */ contactdatetime from campaign.UA_CONTACTHISTORY a where contactdatetime>systimestamp-200 and rownum=1;
select PLAN_ID, operation, options, object_name, id, parent_id from plan_table;

explain plan for select CONTACTDATETIME,CUSTOMERID from campaign.UA_DTLCONTACTHIST where CONTACTDATETIME>sysdate-&daysbefore and rownum=1;
select PLAN_ID, operation, options, object_name, id, parent_id from plan_table;

select to_char(CONTACTDATETIME,'yyyy-mm-dd') as contactdatetime,CUSTOMERID from campaign.UA_DTLCONTACTHIST where CONTACTDATETIME>sysdate-&daysbefore and rownum=1;

select to_char(CONTACTDATETIME,'yyyy-mm-dd') as contactdatetime,CUSTOMERID from campaign.UA_DTLCONTACTHIST where CONTACTDATETIME 数据分布
select to_char(CONTACTDATETIME,'yyyy-mm-dd') as contact_datetime,
       count(CONTACTDATETIME) as contact_count
  from campaign.UA_DTLCONTACTHIST
 where CONTACTDATETIME>sysdate-&daysbefore
   and CONTACTDATETIME group by to_char(CONTACTDATETIME,'yyyy-mm-dd')
 order by contact_datetime desc

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29674916/viewspace-1564171/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29674916/viewspace-1564171/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要连接Oracle数据库,需要按照以下步骤操作: 1. 打开DataGrip软件,点击左上角的“Database”按钮,选择“New”创建新的数据库连接。 2. 在弹出的窗口中选择“Oracle”作为数据库类型,输入连接信息,包括主机名、端口号、数据库名称、用户名和密码等。 3. 点击“Test Connection”测试连接是否成功,如果成功会弹出提示框。 4. 点击“OK”保存连接信息,然后就可以在DataGrip中使用Oracle数据库了。 需要注意的是,连接Oracle数据库需要安装Oracle客户端软件,并且需要正确配置环境变量。如果连接不成功,可以检查一下环境变量是否配置正确。 ### 回答2: DataGrip是一种面向数据库管理员和开发人员的集成开发环境,可以用于连接和管理各种类型的数据库,包括Oracle数据库。在使用DataGrip连接Oracle数据库时,需要进行以下步骤: 1. 下载并安装DataGrip软件,打开软件之后,进入"Settings"菜单,点击"Plugins"选项卡。在搜索栏中输入"Oracle",并安装Oracle插件。 2. 在DataGrip中创建数据库连接。点击DataGrip主界面上的"Database"按钮,在弹出的菜单中选择"New"创建新的数据库连接。 3. 输入Oracle数据库的连接参数。在新建的数据库连接窗口中,选择Oracle数据库,然后按照提示输入连接参数,包括主机名、端口号、服务名、用户名、密码等信息,最后点击"Test Connection"测试连接是否成功。 4. 配置Oracle数据库的高级选项。在连接参数中,还可以配置Oracle数据库的高级选项,例如数据源、编码格式、SSL加密等。可以根据需要进行相应的配置。 5. 测试和管理Oracle数据库。配置完成后,可以通过DataGrip连接Oracle数据库,并进行数据库的测试、管理和维护工作。可以使用DataGrip的各项功能,例如SQL编辑器、表结构设计、数据导入导出等,对Oracle数据库进行各种操作。 需要注意的是,在使用DataGrip连接Oracle数据库时,需要确保连接设置正确,并且有权限访问Oracle数据库。同时,还需要熟悉Oracle数据库的相关知识和技术,才能更好地使用DataGrip进行数据开发和管理。 ### 回答3: Datagrip是一款功能强大的数据库开发工具,支持连接多种类型的数据库。其中之一就是Oracle数据库,这篇文章将介绍如何使用Datagrip连接Oracle数据库。 1. 准备工作 在开始连接Oracle数据库之前,需要先确定以下几点: - 已经安装Oracle数据库且已经启动 - 已经创建了需要连接的数据库实例 - 已经有了连接Oracle数据库的账户以及密码 2. 连接Oracle数据库 在Datagrip中连接Oracle数据库十分简单,只需要按照以下步骤操作: - 打开Datagrip,进入主界面 - 点击"New Project" - 在左侧导航栏中选择"Data source",然后点击"+"号添加数据源 - 在弹出的对话框中选择Oracle,并填写相应的连接信息,包括主机名、端口号、数据库名称、用户名以及密码 - 点击"Test Connection"按钮进行测试,如果连接成功,则会弹出提示框。如果连接失败,则需要检查连接信息是否填写正确 - 点击"OK"按钮保存数据源信息 3. 使用Oracle数据库 连接成功后,就可以使用Oracle数据库进行开发了。在Datagrip中,可以使用SQL编辑器进行SQL语句的编写和调试,并且还支持导入和导出数据等操作。 总结 使用Datagrip连接Oracle数据库非常方便,只需要几步简单的操作就可以完成连接,非常适合开发人员进行数据库开发和维护。无论是编写SQL语句还是进行数据导入导出,Datagrip都提供了非常丰富的功能,可以大大提高开发效率,让开发工作变得更加轻松。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值