---
title: "常用oracle操作(三)"
author: "hanxinyu"
date: "2007-10-08T16:44:24Z"
tags: ["OS", "Oracle", "SQL", "SQL Server", "Unix"]
slug: "oracle"
draft: false
lastmod: "2007-10-08T16:44:24Z"
id: 129759
url: "https://hanxinyu.iteye.com/blog/129759"
---

杀掉oracle进程：alter system kill session 'sid,serial#';

--从数据库段中获得段名为TB\_DW\_SC\_USER\_CUR的所有的分区名以及他们的容量大小  
select partition\_name,bytes/1024/1024 from dba\_segments where segment\_name='TB\_DW\_SC\_USER\_CUR'  
 order by 1;  
   
select * from v$open\_cursor where sid = :sid;

select   value from     v$sesstat  
where    sid = :sid  
order by statistic#;  
select l.\*, o.owner object\_owner, o.object\_Name  
from  sys.all\_objects o, v$lock l  
where l.sid = :sid and l.type = 'TM' and o.object\_id = l.id1

alter session set current\_schema=rene;  
##################################  
##会话相关,从会话找到对应的unix进程号及正在执行的sql语句  
##################################  
Select  sid,serial#,program ,logon\_time,username,osuser  
 From v$session  
 Where 1=1  
 -- and program Like 'sql%'  
 -- and username = 'HW\_QUERY'  
 -- And logon\_time&lt;sysdate-5/60  
 and osuser ='hwuser'  
 order by 3,4  
 ;  
Select paddr,sql\_address From v$session  
 Where sid =932;  
 paddr='07000001371FC968'  
SELECT spid  
  FROM v$process  
 WHERE (addr = '07000003F52BFF68');

SELECT   hash\_value,sql\_text  
    FROM v$sqltext\_with\_newlines  
   WHERE hash\_value = TO\_NUMBER (:HASH)  
ORDER BY piece

select SQL\_FULLTEXT from v$sqlarea\_plan\_hash t Where t.ADDRESS='07000003E1DD6818'  
 v$sqlarea

Select p.spid,s.sid,s.serial#,s.username,s.program ,s.logon\_time From v$session s,v$process p  
 Where s.paddr =p.addr  
 order by 4,5;

##################################  
##会话相关,spid====&gt;sqltext  
##################################

--人v$process找到session的地址  
SELECT addr,spid  
  FROM v$process  
 WHERE (spid= 296118);  
 --找当前执行的sql及前一sql的地址  
Select sql\_address,prev\_sql\_addr,sid,serial#,paddr From v$session Where  
 paddr='07000004EC287A80';  
\--从sql\_address找语句  
select sql\_fulltext from v$sqlarea\_plan\_hash t Where t.ADDRESS='07000004DB478478';

select sid,serial#,paddr,sql\_address,program,logon\_time from v$session  
where program like 'sqlpl%'  
and sql\_address &lt;&gt;'00';

##################################  
#被锁定的对象  
##################################  
select o.owner,                                              
       o.object\_name,                                        
       s.sid,                                                 
       s.serial#,                                          
       -- lo.xidusn,                                            
       -- lo.xidslot,                                           
       -- lo.xidsqn,                                            
       -- lo.session\_id,                                        
       --lo.oracle\_username,                                   
       -- lo.os\_user\_name,                                      
       --lo.process,                                           
       --lo.locked\_mode,                                       
       --s.machine,                                            
       --s.status,                                             
       --s.server,                                              
       s.program                                             
from all\_objects o,                                        
   v$locked\_object lo,                                   
   sys.gv\_$session s                                     
where ( o.object\_id = lo.object\_id )  
and        ( lo.process = s.process )  
\-- and s.sid=1013  
order by object\_name       
;  
\--EXPLAIN PLAN  
EXPLAIN PLAN  
    SET STATEMENT\_ID = 'pxs'  
    INTO plan\_table  
    FOR  
   
 ;  
column OPERATION format a48  
column options format a32  
   
SELECT LPAD(' ',2\*(LEVEL-1))||operation operation, options,  
object\_name, position  
    FROM plan\_table  
    START WITH id = 0 AND statement\_id = 'pxs'  
    CONNECT BY PRIOR id = parent\_id AND  
    statement\_id = 'pxs';

222120  
\--查看数据分区情况  
select partition\_name,bytes/1024/1024 from dba\_segments where segment\_name = upper('tb\_mk\_user\_last\_call\_day')

$ORACLE\_HOME/sqlplus/admin/glogin.sql  
##################################  
\--周处理  
to\_char(start\_date,'ww')--年内周  
to\_char(start\_date,'w') --月内周  
##################################  
#空间相关  
##################################

--查看空间占用  
select tablespace\_name,round(sum(bytes)/1024/1024/1024) as "free(G)"  
from dba\_free\_space  
group by tablespace\_name  
order by 1;

--查看表空间的名称及大小  
column db\_name format a10  
select sys\_context ('USERENV', 'db\_name') db\_name,t.tablespace\_name,round(sum(bytes/(1024\*1024\*1024)),0) "ts\_size(G)"  
from sys.dba\_tablespaces t, sys.dba\_data\_files d  
where t.tablespace\_name = d.tablespace\_name  
group by t.tablespace\_name  
order by 2;

--dba\_segments 数据块字典  
select segment\_name,tablespace\_name,bytes/1024/1024  
 from dba\_segments t1 ,  
('TB\_MK\_CL\_DOWNLOAD\_WEEK','TB\_MK\_NS\_SERVICE\_WEEK','TB\_MK\_SC\_OPP\_USER\_WEEK','TB\_MK\_SC\_USER\_CELL\_WEEK\_TEN','TB\_MK\_SC\_USER\_PLUS\_WEEK','TB\_MK\_SC\_USER\_WEEK')  
;

##################################  
#权限角色相关  
##################################

#数据库中定义的角色  
SELect * from dba\_role\_privs where GRANTED\_ROLE='DBA' order by 1,2;  
#角色具有的权限  
SELect * from dba\_sys\_privs where grantee ='BIM' order by 1,2;  
#表空间使用份额  
select * from dba\_ts\_quotas;  
#分区配置表的关键参数,bim用户下对dw/mk/kr的tb\_sys\_tab\_partitions 做了个union all的视图  
select table\_name,partition\_name,tablespace\_name,partition\_count,PARTITION\_TYPE\_ID,deal\_cycle,TABLESPACE\_TYPE\_ID,partition\_count  
\--select *  
from tb\_sys\_tab\_partitions  
where table\_name in('TB\_DW\_CL\_SHDPAY\_CUR','TB\_DW\_CL\_GROUP\_PERSON\_CUR\_H','TB\_DW\_CL\_SHDPAY\_CUR');

update dgdm\_dw.tb\_sys\_tab\_partitions set PARTITION\_TYPE\_ID='L' where table\_name ='TB\_DW\_CL\_GROUP\_PERSON\_CUR\_H';

##################################  
#分区相关  
##################################  
#新增没有配置的分区表  
select owner,segment\_name from dba\_segments  
 where segment\_type='TABLE PARTITION' and owner like 'DGDM%' and owner not like '%ODS'  
MINUS  
select owner,table\_name from tb\_sys\_tab\_partitions;

#分区表数据字典,查找分区类型  
select table\_name,partitioning\_type,partitioning\_type  
from dba\_part\_tables  
where table\_name in('TB\_DW\_CL\_SHDPAY\_CUR','TB\_DW\_CL\_GROUP\_PERSON\_CUR\_H','TB\_DW\_CL\_SHDPAY\_CUR');  
#分区字段字典.  
select * from dba\_part\_key\_columns;  
\--表分区字典  
\--select table\_name,partition\_name,compression from dba\_tab\_partitions where table\_name like 'TB\_MK%' and rownum&lt;5;  
select table\_owner,table\_name,partition\_name,compression from dba\_tab\_partitions  
 where table\_name like 'TB\\\_%' escape '\\'  
 and compression&lt;&gt;'ENABLED'  
 and table\_owner in ('DGDM\_DW','DGDM\_MK','DGDM\_KR');

\# 分区表的默认表空间,前段困扰我们的masa表空间不足的罪魁祸首  
Select table\_name, def\_tablespace\_name From all\_part\_tables  Where (substr(def\_tablespace\_name,-1) &lt;&gt;'0') And TABLE\_NAME Like 'TB\_DW%';  
Select 'alter table  dgdm\_dw.'||table\_name|| ' modify default attributes tablespace tbs\_data\_0;' def\_tablespace\_name From all\_part\_tables   
Where 0=0  
 --and (substr(def\_tablespace\_name,-1) &lt;&gt;'0')  
 And TABLE\_NAME Like 'TB\_DW%'  
 and def\_tablespace\_name ='TBS\_DW\_CUST\_DATA\_0';  
#更改分区表的默认表空间  
alter table test\_part modify default attributes tablespace test03;

##################################  
#用sql语句生成脚本然后拷贝到sqlplus中执行是一种很常用的技巧.  
##################################  
select 'alter table '||table\_owner||'.'||table\_name ||' move partition '||partition\_name||' compress;'  
 from dba\_tab\_partitions  
 where table\_name like 'TB\_MK%'  
  and compression &lt;&gt;'ENABLED'  
  and table\_name like 'TB\_MK%';

select 'alter table '||table\_owner||'.'||table\_name||' move partition '||partition\_name||' compress;'  
select sys\_context ('USERENV', 'db\_name') db\_name,table\_owner,table\_name,partition\_name,compression  
from dba\_tab\_partitions  
where  1=1  
and table\_name like 'TB\_\_\_\_%'  
and compression ='DISABLED'  
and table\_owner like 'DGDM%'  
;

##################################  
#日志  
##################################  
column table\_name format a32  
column task\_name format a32  
column task\_log format a60  
column task\_pos format a60  
column task\_sign format a60  
select  task\_sign,start\_time,end\_time,task\_name,table\_name ,task\_log  
from --dgdm\_dw.tb\_dw\_sys\_log  
  (  
   select * from gddm\_cb.tb\_cb\_sys\_log  
   union all  
   select * from dgdm\_ods.tb\_in\_sys\_log  
   union all  
   select * from dgdm\_dw.tb\_dw\_sys\_log  
   union all  
   select * from dgdm\_mk.tb\_mk\_sys\_log  
   union all  
   select * from dgdm\_kr.tb\_kr\_sys\_log  
  )      
where 1=1  
 and start\_time&gt;sysdate-1  
 and table\_name like 'tb\_kr\_data\_service\_mon'  
 -- and instr( task\_log ,'ORA-')&gt;0  
 order by 1 ;

#表空间中的对象  
select segment\_name from dba\_segments where partition\_name like '%200706%' and tablespace\_name='TBS\_MK\_FACT\_DATA\_5';

#分区大小  
select owner,segment\_name,partition\_name,tablespace\_name,bytes/1024/1024  
 from dba\_segments  
 where 1=1  
  --and segment\_name='TB\_DW\_PD\_USR\_MARKPLAN'  
  and tablespace\_name='TBS\_DATA\_H6'  
 order by 1,2,3;

#周表月份核对  
\--周表  
 select tablespace\_name,owner,segment\_name,partition\_name,segment\_type,bytes/1024/1024  
 ,(select to\_char(end\_date,'m') from gddm\_cb.tb\_dic\_week t2 where t2.STATIS\_WEEK= substr(partition\_name,instr(partition\_name,'\_',-1)+5,2)),substr(tablespace\_name,-1) wm,  
 (select tablespace\_name from tb\_sys\_tab\_partitions tt where tt.table\_name=t1.segment\_name) ts  
 ,  
 from dba\_segments t1  
 where SEGMENT\_TYPE='TABLE PARTITION'  
 and segment\_name like '%WEEK%'  
 and length(substr(partition\_name,instr(partition\_name,'\_',-1)+1))=6  
 and owner like 'DGDM%' and owner not like '%ODS'  
ORDER BY segment\_name,partition\_name;

--普通表  
select tablespace\_name,owner,segment\_name,partition\_name,segment\_type,bytes/1024/1024  
 ,substr(partition\_name,instr(partition\_name,'\_',-1)+5,2),substr(tablespace\_name,-1),  
 (select tablespace\_name from tb\_sys\_tab\_partitions ta where ta.table\_name= t1.segment\_name)||'\_'  
 from dba\_segments  t1  
 where SEGMENT\_TYPE='TABLE PARTITION'  
  --and substr(partition\_name,instr(partition\_name,'\_',-1)+5,2) - substr(tablespace\_name,-1) not in (0,6)  
  and segment\_name not like '%WEEK'  
  and owner ='DGDM\_MK'  
 ORDER BY segment\_name,partition\_name;

##################################  
#表空间整理  
##################################

select T1.owner,T1.segment\_name,T1.partition\_name,T1.bytes/1024/1024 MB,t1.tablespace\_name  
,t1.segment\_type  
,(SELECT TABLESPACE\_NAME FROM TB\_SYS\_TAB\_PARTITIONS T2 WHERE T1.SEGMENT\_NAME= T2.TABLE\_NAME)  
,substr(partition\_name,instr(partition\_name,'\_',-1)+5,2)  
,SYS\_CONTEXT ('USERENV', 'db\_name') db\_name  
from dba\_segments T1  
where 1=1  
and tablespace\_name LIKE'TBS\_KR\_DATA\_1%'  
\--and partition\_name like '%200708%'  
\-- and segment\_type='TABLE PARTITION'  
 -- and segment\_name in (  
\-- 'TB\_KR\_SC\_USER\_DAY'  
\-- )  
\-- and segment\_name not like 'TB\_MK\_LS%'  
\--and segment\_name in (select table\_name from tb\_sys\_tab\_partitions where table\_name like '%MK%MID')  
and segment\_name not in ('TB\_MK\_CL\_DOWNLOAD\_WEEK','TB\_MK\_NS\_SERVICE\_WEEK','TB\_MK\_SC\_OPP\_USER\_WEEK','TB\_MK\_SC\_USER\_CELL\_WEEK\_TEN','TB\_MK\_SC\_USER\_PLUS\_WEEK','TB\_MK\_SC\_USER\_WEEK')  
\--and owner like 'DGDM%'  
\--and T1.bytes/1024/1024 between 1 and 32  
order by 1,2,3;

update dgdm\_mk.tb\_sys\_tab\_partitions set tablespace\_name='TBS\_MK\_FACT\_DATA'  
 where tablespace\_name  in('TBS\_DW\_RES\_DATA'  
\-- ,'TBS\_DW\_DATA\_FUNC'  
\-- ,'TBS\_DW\_CUST\_DATA\_H'  
\-- ,'TBS\_DW\_CUST\_DATA'  
 );  
 update dgdm\_dw.tb\_sys\_tab\_partitions set tablespace\_name='TBS\_DW\_AC\_DATA'  
 where tablespace\_name  in('TBS\_DW\_RES\_DATA'  
\-- ,'TBS\_DW\_DATA\_FUNC'  
\-- ,'TBS\_DW\_CUST\_DATA\_H'  
 --,'TBS\_DW\_CUST\_DATA'  
 );  
   
update dgdm\_mk.tb\_sys\_tab\_partitions set tablespace\_name='TBS\_MK\_FACT\_DATA' where table\_name  
IN(  
'TB\_MK\_SC\_USER\_CELL\_WEEK\_TEN'  
,'TB\_MK\_SC\_USER\_DAY'  
,'TB\_MK\_SC\_USER\_DTAL'  
);

select segment\_name,partition\_name,bytes/1024/1024 from dba\_segments where segment\_name in (select table\_name from tb\_sys\_tab\_partitions where tablespace\_name ='TBS\_DATA\_H') order by 1,2;

select  T1.owner,T1.segment\_name,T1.partition\_name,T1.bytes/1024/1024 MB,t1.tablespace\_name  
,t1.segment\_type  
,(SELECT TABLESPACE\_NAME FROM TB\_SYS\_TAB\_PARTITIONS T2 WHERE T1.SEGMENT\_NAME= T2.TABLE\_NAME)  
 from dba\_segments t1 ,tb\_sys\_tab\_partitions t2  
  where t1.segment\_name= t2.table\_name  
  and t1.partition\_name like '%200707%'  
  and t1.tablespace\_name not like t2.tablespace\_name||'%'  
  and segment\_name not in ('TB\_MK\_CL\_DOWNLOAD\_WEEK','TB\_MK\_NS\_SERVICE\_WEEK','TB\_MK\_SC\_OPP\_USER\_WEEK','TB\_MK\_SC\_USER\_CELL\_WEEK\_TEN','TB\_MK\_SC\_USER\_PLUS\_WEEK','TB\_MK\_SC\_USER\_WEEK');

select SYS\_CONTEXT ('USERENV', 'db\_name') db\_name,'JUN',t2.tablespace\_name,round(sum(t1.bytes)/1024/1024/1024 )  
  from dba\_segments t1,tb\_sys\_tab\_partitions t2  
  where 1=1  
   and t1.segment\_name= t2.table\_name  
   and t1.partition\_name like '%200706%'  
 and t2.owner&lt;&gt;'DGDM\_ODS'  
  group by t2.tablespace\_name  
  order by 1,2,3;

alter table dgdm\_mk.TB\_DIC\_TEL\_PHONE\_SEGMENT move tablespace tbs\_etl\_data storage( initial 8k);

ALTER TABLE DGDM\_MK.TB\_MK\_CL\_USER\_MON DROP PARTITION CL\_USER\_MON\_200701;

#表空间与数据文件  
select tablespace\_name,file\_name from dba\_data\_files where tablespace\_name in(  
'TBS\_DW\_CUST\_DATA\_0'  
,'TBS\_DW\_CUST\_DATA\_1'  
,'TBS\_DW\_CUST\_DATA\_2'  
,'TBS\_DW\_CUST\_DATA\_3'  
,'TBS\_DW\_CUST\_DATA\_4'  
,'TBS\_DW\_CUST\_DATA\_5'  
,'TBS\_DW\_CUST\_DATA\_6'  
,'TBS\_DW\_CUST\_DATA\_H1'  
,'TBS\_DW\_CUST\_DATA\_H10'  
,'TBS\_DW\_CUST\_DATA\_H11'  
,'TBS\_DW\_CUST\_DATA\_H12'  
,'TBS\_DW\_CUST\_DATA\_H2'  
,'TBS\_DW\_CUST\_DATA\_H3'  
,'TBS\_DW\_CUST\_DATA\_H4'  
,'TBS\_DW\_CUST\_DATA\_H5'  
,'TBS\_DW\_CUST\_DATA\_H6'  
,'TBS\_DW\_CUST\_DATA\_H7'  
,'TBS\_DW\_CUST\_DATA\_H8'  
,'TBS\_DW\_CUST\_DATA\_H9'  
)  
order by 1,2;

##################################  
\---LOB  
##################################  
ALTER TABLE foo MOVE  
TABLESPACE new\_tbsp STORAGE(new\_storage)  
LOB (lobcol) STORE AS lobsegment  
(TABLESPACE new\_tbsp STORAGE (new\_storage));

##################################  
\---月空间占用情况  
##################################

select SYS\_CONTEXT ('USERENV', 'db\_name') db\_name,'JUN',t2.tablespace\_name,round(sum(t1.bytes)/1024/1024/1024 ) as GB  
  from dba\_segments t1,tb\_sys\_tab\_partitions t2  
  where 1=1  
   and t1.segment\_name= t2.table\_name  
   and t1.partition\_name like '%200706%'  
   and t2.tablespace\_name not like '%0'  
   and t1.segment\_type='TABLE PARTITION'  
 and t2.owner&lt;&gt;'DGDM\_ODS'  
  group by t2.tablespace\_name  
  order by 1,2,3;

select sys\_context ('USERENV', 'db\_name') db\_name,t.tablespace\_name,round(sum(bytes/(1024\*1024\*1024)),0) "ts\_size(G)"  
from sys.dba\_tablespaces t, sys.dba\_data\_files d  
where t.tablespace\_name = d.tablespace\_name  
and t.tablespace\_name like '%3'  
and t.tablespace\_name &lt;&gt;'TBS\_ODS\_DATA\_3'  
group by t.tablespace\_name  
order by 1,2;

select segment\_name,sum(bytes)/1024/1024 MB  
from dba\_segments  
where segment\_name in  
 (select TAble\_name from tb\_sys\_tab\_partitions  
  where tablespace\_name   like 'TBS\_MK\_LIST\_DATA'  
  --and table\_name like 'TB\_MK%'  
  )  
and partition\_name like '%200707%'  
group by segment\_name order by 1;

select segment\_name,sum(bytes)/1024/1024 MB  
from dba\_segments  
where segment\_name in  
 (select TAble\_name from tb\_sys\_tab\_partitions where 1=1  
  -- and tablespace\_name  like 'TBS\_MK\_LIST\_DATA'  
 and table\_name like 'TB\_MK\_LS%'  
 )  
and partition\_name like '%200707%'  
group by segment\_name order by 1;

update dgdm\_mk.tb\_sys\_tab\_partitions set tablespace\_name='TBS\_MK\_FACT\_DATA' where table\_name  
IN(  
'TB\_MK\_CL\_FX\_BILL'  
,'TB\_MK\_SC\_CELL\_DAY\_MID'  
,'TB\_MK\_SC\_USER\_CELL\_WEEK\_TEN'  
,'TB\_MK\_SC\_USER\_DAY'  
,'TB\_MK\_SC\_USER\_DTAL'  
,'TB\_MK\_SC\_USER\_PLUS\_DAY'  
);

TB\_MK\_CL\_FX\_BILL                |        16  
TB\_MK\_SC\_CELL\_DAY\_MID           |      5432  
TB\_MK\_SC\_USER\_CELL\_WEEK\_TEN     |         1  
TB\_MK\_SC\_USER\_DAY               |      6428  
TB\_MK\_SC\_USER\_DTAL              |      7224  
TB\_MK\_SC\_USER\_PLUS\_DAY          |      4814

--Using the GROUPING SETS Clause: Example  
SELECT channel\_desc, calendar\_month\_desc, co.country\_id,  
      TO\_CHAR(sum(amount\_sold) , '9,999,999,999') SALES$  
   FROM sales, customers, times, channels, countries co  
   WHERE sales.time\_id=times.time\_id  
      AND sales.cust\_id=customers.cust\_id  
      AND sales.channel\_id= channels.channel\_id  
      AND customers.country\_id = co.country\_id  
      AND channels.channel\_desc IN ('Direct Sales', 'Internet')  
      AND times.calendar\_month\_desc IN ('2000-09', '2000-10')  
      AND co.country\_id IN ('UK', 'US')  
  GROUP BY GROUPING SETS(  
      (channel\_desc, calendar\_month\_desc, co.country\_id),  
      (channel\_desc, co.country\_id),  
      ( calendar\_month\_desc, co.country\_id) );

CHANNEL\_DESC         CALENDAR CO SALES$  
\-------------------- -------- -- --------------  
Direct Sales         2000-09  UK      1,378,126  
Direct Sales         2000-10  UK      1,388,051  
Direct Sales         2000-09  US      2,835,557  
Direct Sales         2000-10  US      2,908,706  
Internet             2000-09  UK        911,739  
Internet             2000-10  UK        876,571  
Internet             2000-09  US      1,732,240  
Internet             2000-10  US      1,893,753  
Direct Sales                  UK      2,766,177  
Direct Sales                  US      5,744,263  
Internet                      UK      1,788,310  
Internet                      US      3,625,993  
                     2000-09  UK      2,289,865  
                     2000-09  US      4,567,797  
                     2000-10  UK      2,264,622  
                     2000-10  US      4,802,459