Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 3 ■ FILES 87 6 ( 7 TYPE ORACLE_LOADER 8 DEFAULT DIRECTORY data_dir 9 ACCESS PARAMETERS 10 ( 11 records delimited by newline 12 fields 13 REJECT ROWS WITH ALL NULL FIELDS 14 ) 15 LOCATION 16 ( 17 'alert_AskUs.log' 18 ) 19 ) 20 REJECT LIMIT unlimited 21 / Table created. We can now query that information anytime: ops$tkyte@ORA10G> select to_char(last_time,'dd-mon-yyyy hh24:mi') shutdown, 2 to_char(start_time,'dd-mon-yyyy hh24:mi') startup, 3 round((start_time-last_time)*24*60,2) mins_down, 4 round((last_time-lag(start_time) over (order by r)),2) days_up, 5 case when (lead(r) over (order by r) is null ) 6 then round((sysdate-start_time),2) 7 end days_still_up 8 from ( 9 select r, 10 to_date(last_time, 'Dy Mon DD HH24:MI:SS YYYY') last_time, 11 to_date(start_time,'Dy Mon DD HH24:MI:SS YYYY') start_time 12 from ( 13 select r, 14 text_line, 15 lag(text_line,1) over (order by r) start_time, 16 lag(text_line,2) over (order by r) last_time 17 from ( 18 select rownum r, text_line 19 from alert_log 20 where text_line like '___ ___ __ __:__:__ 20__' 21 or text_line like 'Starting ORACLE instance %' 22 ) 23 ) 24 where text_line like 'Starting ORACLE instance %' 25 ) 26 /

88 CHAPTER 3 ■ FILES SHUTDOWN STARTUP MINS_DOWN DAYS_UP DAYS_STILL_UP ----------------- ----------------- ---------- ---------- ------------- 06-may-2004 14:00 06-may-2004 14:24 06-may-2004 14:24 .25 .02 10-may-2004 17:18 10-may-2004 17:19 .93 4.12 26-jun-2004 13:10 26-jun-2004 13:10 .65 46.83 07-sep-2004 20:13 07-sep-2004 20:20 7.27 73.29 116.83 I won’t go into the nuances of the SQL query here, but the innermost query from lines 18 through 21 collect the “Starting” and date lines (remember, when using a LIKE clause, _ matches precisely one character—at least one and at most one). It also “numbers” the lines using ROWNUM. Then, the next level of query uses the built-in LAG() analytic function to reach back one and two rows for each row, and slide that data up so the third row of this query has the data from rows 1, 2, and 3. Row 4 has the data from rows 2, 3, and 4, and so on. We end up keeping just the rows that were like Starting ORACLE instance %, which now have the two preceding timestamps associated with them. From there, computing downtime is easy: we just subtract the two dates. Computing the uptime is not much harder (now that you’ve seen the LAG() function): we just reach back to the prior row, get its startup time, and subtract that from this line’s shutdown time. My Oracle 10g database came into existence on May 6 and it has been shut down four times (and as of this writing it has been up for 116.83 days in a row). The average uptime is getting better and better over time (and hey, it is SQL—we could easily compute that now, too). If you are interested in seeing another example of mining the alert log for useful information, go to http://asktom.oracle.com/~tkyte/alert_arch.html. This page shows a demonstration of how to compute the average time it took to archive a given online redo log file. Once you understand what is in the alert log, generating these queries on your own becomes easy. Data Files Data files, along with redo log files, are the most important set of files in the database. This is where all of your data will ultimately be stored. Every database has at least one data file associated with it, and typically it will have many more than one. Only the most simple “test” database will have one file. In fact, in Chapter 2 we saw the most simple CREATE DATABASE command by default created a database with two data files: one for the SYSTEM tablespace (the true Oracle data dictionary) and one for the SYSAUX tablespace (where other nondictionary objects are stored in version 10g and above). Any real database, however, will have at least three data files: one for the SYSTEM data, one for SYSAUX data, and one for USER data. After a brief review of file system types, we’ll discuss how Oracle organizes these files and how data is organized within them. To understand this, you need to know what a tablespace, segment, extent, and block are. These are the units of allocation that Oracle uses to hold objects in the database, and I describe them in detail shortly.

CHAPTER 3 ■ FILES 87<br />

6 (<br />

7 TYPE ORACLE_LOADER<br />

8 DEFAULT DIRECTORY data_dir<br />

9 ACCESS PARAMETERS<br />

10 (<br />

11 records delimited by newline<br />

12 fields<br />

13 REJECT ROWS WITH ALL NULL FIELDS<br />

14 )<br />

15 LOCATION<br />

16 (<br />

17 'alert_AskUs.log'<br />

18 )<br />

19 )<br />

20 REJECT LIMIT unlimited<br />

21 /<br />

Table created.<br />

We can now query that information anytime:<br />

ops$tkyte@ORA10G> select to_char(last_time,'dd-mon-yyyy hh24:mi') shutdown,<br />

2 to_char(start_time,'dd-mon-yyyy hh24:mi') startup,<br />

3 round((start_time-last_time)*24*60,2) mins_down,<br />

4 round((last_time-lag(start_time) over (order by r)),2) days_up,<br />

5 case when (lead(r) over (order by r) is null )<br />

6 then round((sysdate-start_time),2)<br />

7 end days_still_up<br />

8 from (<br />

9 select r,<br />

10 to_date(last_time, 'Dy Mon DD HH24:MI:SS YYYY') last_time,<br />

11 to_date(start_time,'Dy Mon DD HH24:MI:SS YYYY') start_time<br />

12 from (<br />

13 select r,<br />

14 text_line,<br />

15 lag(text_line,1) over (order by r) start_time,<br />

16 lag(text_line,2) over (order by r) last_time<br />

17 from (<br />

18 select rownum r, text_line<br />

19 from alert_log<br />

20 where text_line like '___ ___ __ __:__:__ 20__'<br />

21 or text_line like 'Starting ORACLE instance %'<br />

22 )<br />

23 )<br />

24 where text_line like 'Starting ORACLE instance %'<br />

25 )<br />

26 /

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!