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 #
- SELECT Sid, Serial#
- FROM V$session
- WHERE Audsid = Sys_Context('USERENV', 'SESSIONID');
2. query the OS process ID of the session.
- SELECT p.Spid "OS Thread", b.NAME "Name-User", s.Program, s.Sid, s.Serial#,
- s.Osuser, s.Machine
- FROM V$process p, V$session s, V$bgprocess b
- WHERE p.Addr = s.Paddr
- AND p.Addr = b.Paddr
- And (s.sid=&1 or p.spid=&1)
- UNION ALL
- SELECT p.Spid "OS Thread", s.Username "Name-User", s.Program, s.Sid,
- s.Serial#, s.Osuser, s.Machine
- FROM V$process p, V$session s
- WHERE p.Addr = s.Paddr
- And (s.sid=&1 or p.spid=&1)
- AND s.Username IS NOT NULL;
3. view the SQL statement that the connection is running based on the sid.
- 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 = &sid );
4. Find out which processes the object is used
- 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 = '&obj'
- ORDER BY s.Username, s.Osuser
5. Check which user connections are available.
- 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
6. view the resource usage of the corresponding connection according to v. sid.
- SELECT n.NAME, v.VALUE, n.CLASS, n.Statistic#
- FROM V$statname n, V$sesstat v
- WHERE v.Sid = &sid
- AND v.Statistic# = n.Statistic#
- ORDER BY n.CLASS, n.Statistic#
7. query the top sessions of resource-consuming processes)
- 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
8. view the lock status.
- 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
9. view the situation of waiting for wait.
- SELECT Ws.CLASS, Ws.COUNT COUNT, SUM(Ss.VALUE) Sum_Value
- FROM V$waitstat Ws, V$sysstat Ss
- WHERE Ss.NAME IN ('db block gets', 'consistent gets')
- GROUP BY Ws.CLASS, Ws.COUNT