免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 13619 | 回复: 8
打印 上一主题 下一主题

如何查询出数据库的创建时间 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-25 16:57 |只看该作者 |倒序浏览
RT,如何才能得到一个数据库实例的创建时间呢?最好是使用sql语句查询。{:3_203:}

论坛徽章:
0
2 [报告]
发表于 2011-11-25 21:15 |只看该作者
实例的创建时间就是启动时间啊

  1. alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
  2. select startup_time from v$instance;
复制代码
不过我猜你可能要的是这个

  1. alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
  2. select created from v$database;
复制代码

论坛徽章:
5
天蝎座
日期:2014-01-23 12:27:392015亚冠之德黑兰石油
日期:2015-05-14 13:33:042015年亚洲杯之乌兹别克斯坦
日期:2015-03-06 14:10:38天秤座
日期:2014-01-24 15:03:052015亚冠之德黑兰石油
日期:2015-08-27 13:43:58
3 [报告]
发表于 2011-11-29 09:02 |只看该作者
借贵地发一点常用SQL查询

1、查看表空间的名称及大小
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;

2、查看表空间物理文件的名称及大小
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;

3、查看回滚段名称及大小
select segment_name, tablespace_name, r.status,
(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,
max_extents, v.curext CurExtent
From dba_rollback_segs r, v$rollstat v
Where r.segment_id = v.usn(+)
order by segment_name;

4、查看控制文件
select name from v$controlfile;

5、查看日志文件
select member from v$logfile;

6、查看表空间的使用情况
select sum(bytes)/(1024*1024) as free_space,tablespace_name
from dba_free_space
group by tablespace_name;

SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,
(B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"
FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C
WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME;

7、查看数据库库对象
select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;

8、查看数据库的版本 
Select version FROM Product_component_version
Where SUBSTR(PRODUCT,1,6)='Oracle';

9、查看数据库的创建日期和归档方式
Select Created, Log_Mode, Log_Mode From V$Database;  

10、捕捉运行很久的SQL
column username format a12
column opname format a16
column progress format a8
select username,sid,opname,
      round(sofar*100 / totalwork,0) || '%' as progress,
      time_remaining,sql_text
from v$session_longops , v$sql
where time_remaining <> 0
and sql_address = address
and sql_hash_value = hash_value
/

论坛徽章:
5
天蝎座
日期:2014-01-23 12:27:392015亚冠之德黑兰石油
日期:2015-05-14 13:33:042015年亚洲杯之乌兹别克斯坦
日期:2015-03-06 14:10:38天秤座
日期:2014-01-24 15:03:052015亚冠之德黑兰石油
日期:2015-08-27 13:43:58
4 [报告]
发表于 2011-11-29 09:05 |只看该作者
11。查看数据表的参数信息
SELECT   partition_name, high_value, high_value_length, tablespace_name,
        pct_free, pct_used, ini_trans, max_trans, initial_extent,
        next_extent, min_extent, max_extent, pct_increase, FREELISTS,
        freelist_groups, LOGGING, BUFFER_POOL, num_rows, blocks,
        empty_blocks, avg_space, chain_cnt, avg_row_len, sample_size,
        last_analyzed
   FROM dba_tab_partitions
  --WHERE table_name = :tname AND table_owner = :towner
ORDER BY partition_position

12.查看还没提交的事务
select * from v$locked_object;
select * from v$transaction;

13。查找object为哪些进程所用
select
p.spid,
s.sid,
s.serial# serial_num,
s.username user_name,
a.type  object_type,
s.osuser os_user_name,
a.owner,
a.object object_name,
decode(sign(48 - command),

1,
to_char(command), 'Action Code #' || to_char(command) ) action,
p.program oracle_process,
s.terminal terminal,
s.program program,
s.status session_status   
from v$session s, v$access a, v$process p   
where s.paddr = p.addr and
     s.type = 'USER' and   
     a.sid = s.sid   and
  a.object='SUBSCRIBER_ATTR'
order by s.username, s.osuser

14。回滚段查看
select rownum, sys.dba_rollback_segs.segment_name Name, v$rollstat.extents
Extents, v$rollstat.rssize Size_in_Bytes, v$rollstat.xacts XActs,
v$rollstat.gets Gets, v$rollstat.waits Waits, v$rollstat.writes Writes,
sys.dba_rollback_segs.status status from v$rollstat, sys.dba_rollback_segs,
v$rollname where v$rollname.name(+) = sys.dba_rollback_segs.segment_name and
v$rollstat.usn (+) = v$rollname.usn order by rownum

15。耗资源的进程(top session)
select s.schemaname schema_name,    decode(sign(48 - command), 1,
to_char(command), 'Action Code #' || to_char(command) ) action,    status
session_status,   s.osuser os_user_name,   s.sid,         p.spid ,         s.serial# serial_num,   
nvl(s.username, '[Oracle process]') user_name,   s.terminal terminal,   
s.program program,   st.value criteria_value  from v$sesstat st,   v$session s  , v$process p   
where st.sid = s.sid and   st.statistic# = to_number('38') and   ('ALL' = 'ALL'
or s.status = 'ALL') and p.addr = s.paddr order by st.value desc,  p.spid asc, s.username asc, s.osuser asc

16。查看锁(lock)情况
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

17。查看等待(wait)情况
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情况
SELECT NAME, BYTES FROM SYS.V_$SGASTAT ORDER BY NAME ASC

19。查看catched object
SELECT owner,              name,              db_link,              namespace,  
           type,              sharable_mem,              loads,              executions,   
          locks,              pins,              kept        FROM v$db_object_cache         

20。查看V$SQLAREA
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

论坛徽章:
5
天蝎座
日期:2014-01-23 12:27:392015亚冠之德黑兰石油
日期:2015-05-14 13:33:042015年亚洲杯之乌兹别克斯坦
日期:2015-03-06 14:10:38天秤座
日期:2014-01-24 15:03:052015亚冠之德黑兰石油
日期:2015-08-27 13:43:58
5 [报告]
发表于 2011-11-29 09:09 |只看该作者
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  

22。按用户查看object种类
select u.name schema,   sum(decode(o.type#, 1, 1, NULL)) indexes,   
sum(decode(o.type#, 2, 1, NULL)) tables,   sum(decode(o.type#, 3, 1, NULL))
clusters,   sum(decode(o.type#, 4, 1, NULL)) views,   sum(decode(o.type#, 5, 1,
NULL)) synonyms,   sum(decode(o.type#, 6, 1, NULL)) sequences,   
sum(decode(o.type#, 1, NULL, 2, NULL, 3, NULL, 4, NULL, 5, NULL, 6, NULL, 1))
others   from sys.obj$ o, sys.user$ u   where o.type# >= 1 and    u.user# =
o.owner# and   u.name <> 'PUBLIC'   group by u.name    order by
sys.link$ union select 'CONSTRAINT' , count(*) from sys.con$

23。有关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 = 71 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 = 71)

24.查询表空间使用情况
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-nvl(b.bytes_free,0))/1024/1024,2) "使用(M)",
Largest "最大扩展段(M)",
to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') "采样时间"
from  (select f.tablespace_name,
   sum(f.bytes) bytes_alloc,
   sum(decode(f.autoextensible,'YES',f.maxbytes,'NO',f.bytes)) maxbytes
from dba_data_files f
group by tablespace_name) a,
(select  f.tablespace_name,
    sum(f.bytes) bytes_free
from dba_free_space f
group by tablespace_name) b,
(select round(max(ff.length)*16/1024,2) Largest,
   ts.name tablespace_name
from sys.fet$ ff, sys.file$ tf,sys.ts$ ts
where ts.ts#=ff.ts# and ff.file#=tf.relfile# and ts.ts#=tf.ts#
group by ts.name, tf.blocks) c
where a.tablespace_name = b.tablespace_name and a.tablespace_name = c.tablespace_name

25. 查询表空间的碎片程度
select tablespace_name,count(tablespace_name) from dba_free_space group by tablespace_name
having count(tablespace_name)>10;

alter tablespace name coalesce;
alter table name deallocate unused;  

create or replace view ts_blocks_v as
select tablespace_name,block_id,bytes,blocks,'free space' segment_name from dba_free_space
union all
select tablespace_name,block_id,bytes,blocks,segment_name from dba_extents;
select * from ts_blocks_v;
select tablespace_name,sum(bytes),max(bytes),count(block_id) from dba_free_space
group by tablespace_name;

论坛徽章:
5
天蝎座
日期:2014-01-23 12:27:392015亚冠之德黑兰石油
日期:2015-05-14 13:33:042015年亚洲杯之乌兹别克斯坦
日期:2015-03-06 14:10:38天秤座
日期:2014-01-24 15:03:052015亚冠之德黑兰石油
日期:2015-08-27 13:43:58
6 [报告]
发表于 2011-11-29 16:00 |只看该作者
########### Basic SQL SELECT ################  

select col_name as col_alias from table_name ;  

select col_name from table_name where col1 like '_o%'; ----'_'匹配单个字符  

/*使用字符函数(右边截取,字段中包含某个字符,左边填充某字符到固定位数,右边填充某字符到固定位数)*/
select substr(col1,-3,5),instr(col2,'g'),LPAD(col3,10,'$'),RPAD(col4,10,'%') from table_name;



/*使用数字函数(往右/左几位四舍五入,取整,取余)*/
select round(col1,-2),trunc(col2),mod(col3) from table_name ;


/*使用日期函数(计算两个日期间相差几个星期,两个日期间相隔几个月,在某个月份上加几个月,某个日期的下一个日期,
某日期所在月的最后的日期,对某个日期的月分四舍五入,对某个日期的月份进行取整)*/

select (sysdate-col1)/7 week,months_between(sysdate,col1),add_months(col1,2),next_day(sysdate,'FRIDAY'),last_day(sysdate),
round(sysdate,'MONTH'),trunc(sysdate,'MONTH') from table_name;  

/*使用NULL函数(当expr1为空取expr2/当expr1为空取expr2,否则取expr3/当expr1=expr2返回空)*/
select nvl(expr1,expr2),nvl2(expr1,expr2,expr3),nullif(expr1,expr2) from table_name;


select column1,column2,column3, case column2 when '50' then column2*1.1
when '30' then column2*2.1
when '10' then column3/20
else column3
end as ttt
from table_name ; ------使用case函数  

select table1.col1,table2.col2 from table1
[CROSS JOIN table2] | -----笛卡儿连接
[NATURAL JOIN table2] | -----用两个表中的同名列连接
[JOIN table2 USING (column_name)] | -----用两个表中的同名列中的某一列或几列连接
[JOIN table2
ON (table1.col1=table2.col2)] |
[LEFT|RIGHT|FULL OUTER JOIN table2 ------相当于(+)=,=(+)连接,全外连接
ON (table1.col1=table2.col2)]; ------SQL 1999中的JOIN语法;



example:
select col1,col2 from table1 t1
join table2 t2
on t1.col1=t2.col2 and t1.col3=t2.col1
join table3 t3
on t2.col1=t3.col3;  

select * from table_name where col1 < any (select col2 from table_name2 where continue group by col3);  

select * from table_name where col1 < all (select col2 from table_name2 where continue group by col3);  

insert into (select col1,col2,col3 form table_name where col1> 50 with check option) values (value1,value2,value3);  

MERGE INTO table_name table1
USING table_name2 table2
ON (table1.col1=table2.col2)
WHEN MATCHED THEN
UPDATE SET
table1.col1=table2.col2,
table1.col2=table2.col3,
...

WHEN NOT MATCHED THEN
INSERT VALUES(table2.col1,table2.col2,table2.col3,...); -----合并语句

论坛徽章:
5
天蝎座
日期:2014-01-23 12:27:392015亚冠之德黑兰石油
日期:2015-05-14 13:33:042015年亚洲杯之乌兹别克斯坦
日期:2015-03-06 14:10:38天秤座
日期:2014-01-24 15:03:052015亚冠之德黑兰石油
日期:2015-08-27 13:43:58
7 [报告]
发表于 2011-11-29 16:02 |只看该作者
################ 增强的 group by 子句 ################

select [column,] group_function(column)...
from table
[WHERE condition]
[GROUP BY [ROLLUP] group_by_expression]
[HAVING having_expression];
[ORDER BY column]; -------ROLLUP操作字,对group by子句的各字段从右到左进行再聚合

example:
/*其结果看起来象对col1做小计*/
select col1,col2,sum(col3) from table group by rollup(col1,col2);

/*复合rollup表达式*/
select col1,col2,sum(col3) from table group by rollup((col1,col2));

select [column,] group_function(column)...
from table
[WHERE condition]
[GROUP BY [CUBE] group_by_expression]
[HAVING having_expression];
[ORDER BY column]; -------CUBE操作字,除完成ROLLUP的功能外,再对ROLLUP后的结果集从右到左再聚合  

example:
/*其结果看起来象对col1做小计后,再对col2做小计,最后算总计*/
select col1,col2,sum(col3) from table group by cube(col1,col2);
/*复合rollup表达式*/
select col1,col2,sum(col3) from table group by cube((col1,col2));
/*混合rollup,cube表达式*/
select col1,col2,col3,sum(col4) from table group by col1,rollup(col2),cube(col3);


/*GROUPING(expr)函数,查看select语句种以何字段聚合,其取值为0或1*/
select [column,] group_function(column)...,GROUPING(expr)
from table
[WHERE condition]
[GROUP BY [ROLLUP] group_by_expression]
[HAVING having_expression];
[ORDER BY column];  

example:
select col1,col2,sum(col3),grouping(col1),grouping(col2) from table group by cube(col1,col2);  

/*grouping sets操作,对group by结果集先对col1求和,再对col2求和,最后将其结果集并在一起*/
select col1,col2,sum(col3) from table group by grouping sets((col1),(col2));

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
8 [报告]
发表于 2011-11-29 23:01 |只看该作者
回复 3# moon38sun


亲,咱CU不是有blog嘛

论坛徽章:
5
天蝎座
日期:2014-01-23 12:27:392015亚冠之德黑兰石油
日期:2015-05-14 13:33:042015年亚洲杯之乌兹别克斯坦
日期:2015-03-06 14:10:38天秤座
日期:2014-01-24 15:03:052015亚冠之德黑兰石油
日期:2015-08-27 13:43:58
9 [报告]
发表于 2011-11-30 10:11 |只看该作者
那个Blog还没有发表过东西呢,下次直接去那里,谢谢,亲!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP