數據庫管理員常用的 sql 語句 (查殺進程、連接數、死鎖)

---查找oracle進程
SELECT A.OWNER,A.OBJECT_NAME,B.XIDUSN,B.XIDSLOT,B.XIDSQN,B.SESSION_ID,B.ORACLE_USERNAME,
B.OS_USER_NAME,B.PROCESS,B.LOCKED_MODE,C.MACHINE,C.STATUS,C.SERVER,C.SID,C.SERIAL#,C.PROGRAM
FROM ALL_OBJECTS A,V$LOCKED_OBJECT B,SYS.GV_$SESSION C WHERE (A.OBJECT_ID=B.OBJECT_ID) AND (B.PROCESS=C.PROCESS)  ;


--AND A.OBJECT_NAME like 'GAP_CDR_%B'
ORDER BY 1,2;
----手工殺掉 oracle進程




alter system kill session 'sid,serial#'
alter system kill session '1095,648';
alter system kill session '3265,39190';
-------------------------------


-------------------------------
select spid, osuser, s.program 
from v$session s,v$process p
where s.paddr=p.addr and s.sid=1654
----------------------------------------
SELECT C.SID,C.SERIAL# 
FROM ALL_OBJECTS A,
V$LOCKED_OBJECT B,
SYS.GV_$SESSION C
WHERE (A.OBJECT_ID=B.OBJECT_ID)
AND (B.PROCESS=C.PROCESS) 
--AND A.OBJECT_NAME='GAP_CDR_MORE_GSM_B'
and c.sid='19'
ORDER BY 1,2
---------------------------------------
SELECT C.SID,C.SERIAL#
FROM ALL_OBJECTS A,
V$LOCKED_OBJECT B,
SYS.GV_$SESSION C
WHERE (A.OBJECT_ID=B.OBJECT_ID)
AND (B.PROCESS=C.PROCESS) 
--AND A.OBJECT_NAME=upper('GAP_CDR_more_GSM_a');
ORDER BY 1,2   87
-------------------------------------------------------------------------
SELECT s.sid, s.username, s.osuser, s.machine, s.program,
t.xidusn,t.ubafil,t.ubablk,t.used_ublk, t.log_io, t.phy_io, t.cr_get, t.cr_change,
r.name, q.sql_text
FROM v$session s,v$transaction t, v$RollName r, v$sqlarea q
WHERE s.saddr=t.ses_addr
and t.xidusn = r.usn
and s.sql_address = q.address(+)
and s.sql_hash_value = q.hash_value(+);
--And r.name = 'RBS_BATCH1';
-------------------------------------------------------------------
select l.session_id, l.os_user_name, l.oracle_username, o.owner, o.object_name, t.xidusn,t.ubafil,t.ubablk,t.used_ublk, t.used_urec,
t.log_io, t.phy_io, t.cr_get, t.cr_change, r.name
from v$locked_object l, dba_objects o, v$transaction t, v$RollName r
where l.object_id = o.object_id
and l.xidusn = t.xidusn
and l.xidslot = t.xidslot
and l.xidsqn = t.xidsqn
and t.xidusn = r.usn;
------------  查看錶的最後修改時間   last_ddl_time就是   -----------  gsm_prod_freeres  t_ac_freeres_a
SELECT a.* ,b.table_name FROM DBA_OBJECTS a,all_indexes b
WHERE a.OWNER='TRANS'
--AND OBJECT_NAME='PM_PROD_FREERES'
AND a.OBJECT_TYPE='TABLE' and a.object_name='GSM_PROD_FREERES'--and a.object_name=b.index_name  
order by a.last_ddl_time;
 
select * from all_indexes where index_name='MNLAR_USER_NUMBER20090312';
 
select s.username, u.name from v$transaction t,v$rollstat r,
v$rollname u,v$session s where s.taddr=t.addr and
t.xidusn=r.usn and r.usn=u.usn order by s.username;


select usn,xacts,status,rssize/1024/1024/1024,hwmsize/1024/1024/1024,shrinks
  from v$rollstat order by rssize;


------------------------------------------------------------------------------------------
----  
SELECT 'truncate table '||segment_name||';',SUM(BYTES)/1024/1024  FROM USER_SEGMENTS
where tablespace_name = 'ACCT_DATA'


select * from    USER_SEGMENTS where tablespace_name = 'BSS_TEST_01'




--and segment_name like '%2009031%'
--and segment_name not like '%RA_20090210'
group by segment_name
order by SUM(BYTES) desc ;


Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name order by Sum(bytes)/1024/1024 desc ;


Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents where Segment_Name like upper('WANTUP') Group By Segment_Name order by Sum(bytes)/1024/1024 desc ;


--------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
-- 查詢哪些對象被鎖
select * from V$locked_Object a,dba_objects b ,v$session c where a.OBJECT_ID=b.object_id and a.SESSION_ID=c.SID; 
 -- 錕斤拷錕斤拷錕斤拷SQL錕斤拷洌拷錕斤拷ID  2
 SELECT a.sql_address, b.spid, c.Session_id  FROM v$session a,v$process b, V$locked_Object c
   WHERE a.paddr=b.addr and a.sid=149; -- 爲1錕叫碉拷Session_id 
 -- 錕介看錕斤拷目錕斤拷  3
 SELECT * FROM all_Objects a, V$locked_Object b
   WHERE a.Object_ID = b.object_id;
 -- 錕斤拷錕斤拷錕斤拷錕斤拷SQL錕斤拷淶礆拷錕斤拷錕?4 
 SELECT a.* FROM v$sql a, v$session b , V$locked
 
 -----------------------------------------
 select spid, osuser, s.program 
from v$session s,v$process p
where s.paddr=p.addr and s.sid=165




 
 
-----------------------------------------------------------------------------------------------------------------------
select usn,xacts,status,rssize/1024/1024/1024,hwmsize/1024/1024/1024,shrinks
 from v$rollstat order by rssize;
 
select s.username, u.name from v$transaction t,v$rollstat r,
 v$rollname u,v$session s where s.taddr=t.addr and
 t.xidusn=r.usn and r.usn=u.usn order by s.username;
 
select usn,xacts,status,rssize/1024/1024/1024,hwmsize/1024/1024/1024,shrinks
from v$rollstat order by rssize;


select *
FROM V$LOCKED_OBJECT T1, V$SESSION T2
WHERE T1.SESSION_ID = T2.SID;
------------------------------------------------------------------------------------------------
 
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 4 desc;




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






-- 查詢表空間物理文件的名稱、大小及使用情況
select b.file_id "文件編號",
       b.file_name "數據文件絕對路徑",
       b.tablespace_name "表空間名稱",
       round(b.bytes / (1024 * 1024), 0) "總大小(M)",
       TO_CHAR((DECODE(F.BYTES, NULL, 0, F.BYTES) / 1024 / 1024), '999,990.9') "剩餘大小 (M)",
       TO_CHAR(((((A.BYTES - DECODE(F.BYTES, NULL, 0, F.BYTES)) / 1024 / 1024)) /  (A.BYTES / 1024 / 1024)) * 100,  '99,990.9') "使用率(%)"
  from dba_data_files b,SYS.DBA_TABLESPACES D, SYS.SM$TS_AVAIL A, SYS.SM$TS_FREE F
  where b.tablespace_name = d.tablespace_name and D.TABLESPACE_NAME = a.tablespace_name and D.TABLESPACE_NAME = f.tablespace_name
  order by b.file_name;
  
  
--------------------------------------------------------------------------
--給用戶解鎖
ALTER USER das_js ACCOUNT UNLOCK;
--
select * from dba_ts_quotas a;
--
select owner,segment_name,segment_type,tablespace_name,bytes/1024/1024 from dba_segments
where tablespace_name='USERS';


--
select * from nls_database_parameters
where parameter='NLS_CHARACTERSET' for update ;
 
select * from v$nls_parameters; 
 
select userenv('language') from dual;
select * from nls_instance_parameters;


drop user raptest cascade;
alter system kill session '20,22';
select username,sid,serial# from v$session;
select 'alter system kill session '''||sid||','||serial#||''';'from v$session where username='TEST';
select saddr,sid,serial#,paddr,username,status from v$session where username is not null;




--
create tablespace "TEL_RAS_XJDX"
logging
datafile 'D:\OracleXe\oradata\datafile\TS_rasxj.dbf' 
size 100m 
autoextend on 
next 1m extent management local;


create user rasxj_test identified by "rasxj_test" default tablespace TEL_RAS_XJDX;


grant resource,connect to rasxj_test;
grant select any table to rasxj_test;
grant select any dictionary to rasxj_test;
grant create any trigger,create any view,create table to rasxj_test;
grant exp_full_database,imp_full_database,resource,create any sequence to rasxj_test;
  


--刪除表空間,使用命令drop tablespace ‘表空間名’  但是有3個選項需要注意: 
--INCLUDING CONTENTS:指刪除表空間中的segments; 
--INCLUDING CONTENTS AND DATAFILES:指刪除segments和datafiles;
--CASCADE CONSTRAINTS:刪除所有與該空間相關的完整性約束條件。 
DROP TABLESPACE TEL_RAS_XJDX_SCENE INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS;  


drop user rasxj_scene cascade;
CREATE UNIQUE INDEX RAP.PBSYSCATCOLDICT_IDX ON RAP.PBCATCOL (PBC_TNAM, PBC_OWNR, PBC_CNAM);




alter user user01 identified by user10;




-- 
SELECT 'alter table ' || t.table_name || ' disable constraint ' || t.constraint_name || ';'
FROM user_constraints t;


SELECT 'alter table ' || t.table_name || ' enable constraint ' || t.constraint_name || ';'
FROM user_constraints t;


--字符集查詢 
select * from nls_database_parameters;
select userenv('language') from dual; 




--1、查詢oracle的連接數 
select count(*) from v$session; 
--2、查詢oracle的併發連接數 
select count(*) from v$session where status='ACTIVE'; 
--3、查看不同用戶的連接數 
select username,count(username) from v$session where username is not null group by username; 
--4、查看所有用戶: 
select * from all_users; 
--5、查看用戶或角色系統權限(直接賦值給用戶或角色的系統權限): 
select * from dba_sys_privs; 
select * from user_sys_privs; 
--6、查看角色(只能查看登陸用戶擁有的角色)所包含的權限 
select * from role_sys_privs; 
--7、查看用戶對象權限: 
select * from dba_tab_privs; 
select * from all_tab_privs; 
select * from user_tab_privs; 
--8、查看所有角色: 
select * from dba_roles; 
--9、查看用戶或角色所擁有的角色: 
select * from dba_role_privs; 
select * from user_role_privs; 
--10、查看哪些用戶有sysdba或sysoper系統權限(查詢時需要相應權限) 
select * from V$PWFILE_USERS; 


----
select count(1),osuser,machine from V$session group by osuser,machine ;


select * from v$session where           status='ACTIVE'; 












查看一下數據庫現有的進程數,是否已經達到參數processes的大小。 


1.   select count(*) from v$process;                         取得數據庫目前的進程數。 
2.    select value from v$parameter where name = 'processes'; 取得進程數的上限。 
3.如已達到上限,修改initSID.ora中的processes的大小。 
4.重新啓動數據庫到nomount狀態下,執行create spfile from pfile; 並startup open。 




--自查重sql
select *
  from RV_FUND_AUDIT_SUM a
 where a.rowid != (select max(rowid)
                     from RV_FUND_AUDIT_SUM b
                    where a.unit_id = b.unit_id
                      and a.deal_date = b.deal_date);
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
                      
 create tablespace "TEL_RAS_XJDX_SCENE"
logging
datafile 'D:\OracleXe\oradata\datafile\TS_rasxj_scene.dbf' 
size 100m 
autoextend on 
next 1m extent management local;


create user rasys identified by "rasys123" default tablespace TEL_RAS_XJDX_SCENE;


grant resource,connect to rasys;
grant select any table to rasys;
grant select any dictionary to rasys;
grant create any trigger,create any view,create table to rasys;
grant exp_full_database,imp_full_database,resource,create any sequence to rasys;


create tablespace "TEL_RAS_XJDX_SCENE" logging
datafile '/data/tablespace/rasxj_scene.dbf' size 100m 
autoextend on extent management local;


CREATE USER rasxj_scene IDENTIFIED BY rasxj_scene DEFAULT TABLESPACE "TEL_RAS_XJDX_SCENE" ACCOUNT UNLOCK;
    
grant 
resource,connect,dba,
select any table,select any dictionary,
create any trigger,create any view,create table,create any sequence,
drop any view,exp_full_database,imp_full_database 
to rasxj_scene;










1、先查詢空閒空間
select tablespace_name,file_id,block_id,bytes,blocks from dba_free_space;


2、增加表空間
先查詢數據文件名稱、大小和路徑的信息,語句如下:
select tablespace_name,file_id,bytes,file_name from dba_data_files;


修改文件大小語句如下
alter database datafile '需要增加的數據文件路徑,即上面查詢出來的路徑' resize 800M;


4、創建表空間
create tablespace test
datafile '/home/app/oracle/oradata/oracle8i/test01.dbf' size 8M
autoextend on
next 5M
maxsize 10M;


create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize unlimited
maxsize unlimited 是大小不受限制


create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
extent management local uniform;
unform表示區的大小相同,默認爲1M


create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
extent management local uniform size 500K;
unform size 500K表示區的大小相同,爲500K


create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
extent management local autoallocate;
autoallocate表示區的大小由隨表的大小自動動態改變,大表使用大區小表使用小區


create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
temporary;
temporary創建字典管理臨時表空間


create temporary tablespace sales
tempfile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
創建本地管理臨時表空間,如果是臨時表空間,所有語句中的datafile都換爲tempfile


8i系統默認創建字典管理臨時表空間,要創建本地管理臨時表空間要加temporary tablespace關鍵字
創建本地管理臨時表空間時,不得使用atuoallocate參數,系統默認創建uniform管理方式


爲表空間增加數據文件:
alter tablespace sales add
datafile '/home/app/oracle/oradata/oracle8i/sales02.dbf' size 800M
autoextend on next 50M
maxsize 1000M;




5、更改自動擴展屬性:
alter database datafile
/home/app/oracle/oradata/oracle8i/sales01.dbf',
/home/app/oracle/oradata/oracle8i/sales02.dbf'
/home/app/oracle/oradata/oracle8i/sales01.dbf
autoextend off;


6、查詢表空間狀態
select tablespace_name,status from dba_tablespaces;


7、更改表空間的狀態
alter tablespace book offline/online/read only/read write;
離線/在線/只讀/讀寫


8、數據文件移動
select tablespace_name,file_name from dba_data_files;
alter tablespace book offline;
將要修改的文件移動到新目錄下
alter tablespace book rename datafile '/u01/oradata/oracle8i/sales01.dbf' to '/u02/oradata/oracle8i/sales01.dbf';


9、修改數據文件的online/offline屬性
archivelog模式下
alter database datafile '/u01/oradata/oracle8i/sales01.dbf' offline;


noarchivelog模式下
alter database datafile '/u01/oradata/oracle8i/sales01.dbf' offline drop;


10、數據字典表空間與本地化管理表空間的轉化爲
select tablespace_name,extent_management,allocation_type from dba_tablespace;
exec dbms_space_admin.tablespace_migrate_to_local('book') 字典轉本地
系統表空間system和臨時表空間temp不得轉換


exec dbms_space_admin.tablespace_migrate_from_local('book') 本地轉字典




11、刪除表空間
drop tablespace student including contents;
including contents將表空間及實體刪除


create table test(id number(3)) tablespace test_tablesapce;






--------------------------------------------------------------------------------------------------------------
--------------------------------------
--------------------------------------------------------------------------------------------------------------
select dbf.tablespace_name,
dbf.totalspace "總量(M)",
dbf.totalblocks as 總塊數,
dfs.freespace "剩餘總量(M)",
dfs.freeblocks "剩餘塊數",
(dfs.freespace / dbf.totalspace) * 100 "空閒比例" 
from (select t.tablespace_name,
sum(t.bytes) / 1024 / 1024 totalspace,
sum(t.blocks) totalblocks
from dba_data_files t
group by t.tablespace_name) dbf,
(select tt.tablespace_name,
sum(tt.bytes) / 1024 / 1024 freespace,
sum(tt.blocks) freeblocks
from dba_free_space tt
group by tt.tablespace_name) dfs
where trim(dbf.tablespace_name) = trim(dfs.tablespace_name);






SELECT Total.name "Tablespace Name",
Free_space, (total_space-Free_space) Used_space, total_space
FROM
(select tablespace_name, sum(bytes/1024/1024) Free_Space
from sys.dba_free_space
group by tablespace_name
) Free,
(select b.name, sum(bytes/1024/1024) TOTAL_SPACE
from sys.v_$datafile a, sys.v_$tablespace B
where a.ts# = b.ts#
group by b.name
) Total
WHERE Free.Tablespace_name = Total.name










----------------------------------------------  wantup ---------------------------------------
create tablespace "WANTUP"
logging
datafile '/home/oracle/oracledata/wantup.dbf' 
size 100m 
autoextend on 
next 1m extent management local;


create user wantup identified by "wantup" default tablespace WANTUP;


grant resource,connect to wantup;
grant select any table to wantup;
grant select any dictionary to wantup;
grant create any trigger,create any view,create table to wantup;
grant exp_full_database,imp_full_database,resource,create any sequence to wantup;







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章