Common SQL code example for Oracle Database Maintenance (1)

Source: Internet
Author: User

Oracle Database MaintenanceIt is a skill that must be mastered as a database administrator. There are many database maintenance operations. This article summarizes 18 common database maintenance operationsSQL code exampleNext, let's take a look at this part.

1. Find the SID of the current session, SERIAL #

 
 
  1. SELECT Sid, Serial#  
  2. FROM V$session  
  3. WHERE Audsid = Sys_Context('USERENV', 'SESSIONID'); 

2. query the OS process ID of the session.

 
 
  1. SELECT p.Spid "OS Thread", b.NAME "Name-User", s.Program, s.Sid, s.Serial#,  
  2. s.Osuser, s.Machine  
  3. FROM V$process p, V$session s, V$bgprocess b  
  4. WHERE p.Addr = s.Paddr  
  5. AND p.Addr = b.Paddr  
  6. And (s.sid=&1 or p.spid=&1)  
  7. UNION ALL  
  8. SELECT p.Spid "OS Thread", s.Username "Name-User", s.Program, s.Sid,  
  9. s.Serial#, s.Osuser, s.Machine  
  10. FROM V$process p, V$session s  
  11. WHERE p.Addr = s.Paddr  
  12. And (s.sid=&1 or p.spid=&1)  
  13. AND s.Username IS NOT NULL; 

3. view the SQL statement that the connection is running based on the sid.

 
 
  1. SELECT /*+ PUSH_SUBQ */  
  2. Command_Type, Sql_Text, Sharable_Mem, Persistent_Mem, Runtime_Mem, Sorts,  
  3. Version_Count, Loaded_Versions, Open_Versions, Users_Opening, Executions,  
  4. Users_Executing, Loads, First_Load_Time, Invalidations, Parse_Calls,  
  5. Disk_Reads, Buffer_Gets, Rows_Processed, SYSDATE Start_Time,  
  6. SYSDATE Finish_Time, '>' || Address Sql_Address, 'N' Status  
  7. FROM V$sqlarea  
  8. WHERE Address = (SELECT Sql_Address  
  9. FROM V$session  
  10. WHERE Sid = &sid ); 

4. Find out which processes the object is used

 
 
  1. SELECT p.Spid, s.Sid, s.Serial# Serial_Num, s.Username User_Name,  
  2. a.TYPE Object_Type, s.Osuser Os_User_Name, a.Owner,  
  3. a.OBJECT Object_Name,  
  4. Decode(Sign(48 - Command), 1, To_Char(Command), 'Action Code #' || To_Char(Command)) Action,  
  5. p.Program Oracle_Process, s.Terminal Terminal, s.Program Program,  
  6. s.Status Session_Status  
  7. FROM V$session s, V$access a, V$process p  
  8. WHERE s.Paddr = p.Addr  
  9. AND s.TYPE = 'USER' 
  10. AND a.Sid = s.Sid  
  11. AND a.OBJECT = '&obj' 
  12. ORDER BY s.Username, s.Osuser 

5. Check which user connections are available.

 
 
  1. SELECT s.Osuser Os_User_Name,  
  2. Decode(Sign(48 - Command),1,To_Char(Command),  
  3. 'Action Code #' || To_Char(Command)) Action,  
  4. p.Program Oracle_Process, Status Session_Status, s.Terminal Terminal,  
  5. s.Program Program, s.Username User_Name,  
  6. s.Fixed_Table_Sequence Activity_Meter, '' Query, 0 Memory,  
  7. 0 Max_Memory, 0 Cpu_Usage, s.Sid, s.Serial# Serial_Num  
  8. FROM V$session s, V$process p  
  9. WHERE s.Paddr = p.Addr  
  10. AND s.TYPE = 'USER' 
  11. ORDER BY s.Username, s.Osuser 

6. view the resource usage of the corresponding connection according to v. sid.

 
 
  1. SELECT n.NAME, v.VALUE, n.CLASS, n.Statistic#  
  2. FROM V$statname n, V$sesstat v  
  3. WHERE v.Sid = &sid  
  4. AND v.Statistic# = n.Statistic#  
  5. ORDER BY n.CLASS, n.Statistic# 

7. query the top sessions of resource-consuming processes)

 
 
  1. SELECT s.Schemaname Schema_Name,  
  2. Decode(Sign(48 - Command),  
  3. 1, To_Char(Command), 'Action Code #' || To_Char(Command)) Action,  
  4. Status Session_Status, s.Osuser Os_User_Name, s.Sid, p.Spid,  
  5. s.Serial# Serial_Num, Nvl(s.Username, '[Oracle process]') User_Name,  
  6. s.Terminal Terminal, s.Program Program, St.VALUE Criteria_Value  
  7. FROM V$sesstat St, V$session s, V$process p  
  8. WHERE St.Sid = s.Sid  
  9. AND St.Statistic# = To_Number('38')  
  10. AND ('ALL' = 'ALL' OR s.Status = 'ALL')  
  11. AND p.Addr = s.Paddr  
  12. ORDER BY St.VALUE DESC, p.Spid ASC, s.Username ASC, s.Osuser ASC 

8. view the lock status.

 
 
  1. SELECT /*+ RULE */  
  2. Ls.Osuser Os_User_Name, Ls.Username User_Name,  
  3. Decode(Ls.TYPE,  
  4. 'RW', 'Row wait enqueue lock', 'TM', 'DML enqueue lock',  
  5. 'TX', 'Transaction enqueue lock', 'UL', 'User supplied lock') Lock_Type,  
  6. o.Object_Name OBJECT,  
  7. Decode(Ls.Lmode,  
  8. 1, NULL, 2, 'Row Share', 3, 'Row Exclusive',  
  9. 4, 'Share', 5, 'Share Row Exclusive', 6, 'Exclusive',  
  10. NULL) Lock_Mode,  
  11. o.Owner, Ls.Sid, Ls.Serial# Serial_Num, Ls.Id1, Ls.Id2  
  12. FROM Sys.Dba_Objects o,  
  13. (SELECT s.Osuser, s.Username, l.TYPE, l.Lmode, s.Sid, s.Serial#, l.Id1,  
  14. l.Id2  
  15. FROM V$session s, V$lock l  
  16. WHERE s.Sid = l.Sid) Ls  
  17. WHERE o.Object_Id = Ls.Id1  
  18. AND o.Owner <> 'SYS'  
  19. ORDER BY o.Owner, o.Object_Name 

9. view the situation of waiting for wait.

 
 
  1. SELECT Ws.CLASS, Ws.COUNT COUNT, SUM(Ss.VALUE) Sum_Value  
  2. FROM V$waitstat Ws, V$sysstat Ss  
  3. WHERE Ss.NAME IN ('db block gets', 'consistent gets')  
  4. GROUP BY Ws.CLASS, Ws.COUNT 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.