Download - Example R usage for oracle DBA UKOUG 2013

Transcript
Page 1: Example R usage for oracle DBA UKOUG 2013

Examples of R Usage with the Oracle Database

Bertrand Drouvot

Page 2: Example R usage for oracle DBA UKOUG 2013

Oracle DBA since 1999 OCP 9i,10g,11g Rac certified Expert Exadata certified implementation specialist Blogger since 2012 @bertranddrouvot BasketBall fan

About Me

Page 3: Example R usage for oracle DBA UKOUG 2013

Some examples of R usage with the oracle database

From a DBA point of view Retrieve system statistics/wait events with

some AWR queries Real time Data Dashboard of the database activity

Will present

Page 4: Example R usage for oracle DBA UKOUG 2013

R installation R programing R studio (powerful and productive user

interface for R)

Will not present

Page 5: Example R usage for oracle DBA UKOUG 2013

Because R is a powerful tool for statistical analysis with graphing and plotting packages built in.

Furthermore, R can connect to Oracle via a JDBC package which makes importing data very easy.

Why R ?

Page 6: Example R usage for oracle DBA UKOUG 2013

http://www.r-project.org/

Starting point with R

Page 7: Example R usage for oracle DBA UKOUG 2013

library(RJDBC) drv <-

JDBC("oracle.jdbc.driver.OracleDriver","/ec/prod/server/oracle/olrprod1/u000/product/11.2.0.3/jdbc/lib/ojdbc6.jar")

conn<-dbConnect(drv,conn_string,"sys as sysasm",sys_pwd)

Data_frame<-dbGetQuery(conn,myquery)

Connecting and retrieving the Data

Page 8: Example R usage for oracle DBA UKOUG 2013

select s.begin_interval_time,sta.stat_name,sta.VALUE,round(((sta.VALUE)/((extract(day from s.END_INTERVAL_TIME)-extract(day from s.BEGIN_INTERVAL_TIME))*86400 +(extract(hour from s.END_INTERVAL_TIME)-extract(hour from s.BEGIN_INTERVAL_TIME))*3600 +(extract(minute from s.END_INTERVAL_TIME)-extract(minute from s.BEGIN_INTERVAL_TIME))*60 +(extract(second from s.END_INTERVAL_TIME)-extract(second from s.BEGIN_INTERVAL_TIME)))),2) VALUE_PER_SECfrom(select instance_number,snap_id,stat_name,value - first_value(value) over (partition by stat_name order by snap_id rows 1 preceding) "VALUE"fromdba_hist_sysstatwhere stat_name like nvl('&stat_name',stat_name)and instance_number = (select instance_number from v$instance)) sta, dba_hist_snapshot swhere sta.instance_number=s.instance_numberand sta.snap_id=s.snap_idand s.BEGIN_INTERVAL_TIME >= trunc(sysdate-&sysdate_nb_day_begin_interval+1)and s.BEGIN_INTERVAL_TIME <= trunc(sysdate-&sysdate_nb_day_end_interval+1)order by s.begin_interval_time asc;

Example 1:Retrieve and visualize system statistics metrics from AWR

Page 9: Example R usage for oracle DBA UKOUG 2013

Launch the script

Page 10: Example R usage for oracle DBA UKOUG 2013

Example 1:Retrieve and visualize system statistics metrics from AWR

Page 11: Example R usage for oracle DBA UKOUG 2013

# Plot the 4 graphs par(mfrow =c(4,1)) plot(sqstat[,'DATEEVT'],sqstat[,'VALUE'],type="l",col="blue",x

axt="n",main=stat_name,cex.main=2,xlab="",ylab="VALUE") Axis(side=1,at=sqstat$DATEEVT,round(skip),format="%Y/

%m/%d %H:%M") plot(sqstat[,'DATEEVT'],sqstat[,'VALUE_PER_SEC'],type="l",col

="blue",xaxt="n",xlab="",ylab="VALUE_PER_SEC") Axis(side=1,at=sqstat$DATEEVT,round(skip),format="%Y/

%m/%d %H:%M") hist(sqstat[,'VALUE'],xlab="VALUE",main=NULL,border="blue

") hist(sqstat[,'VALUE_PER_SEC'],xlab="VALUE_PER_SEC",main=

NULL,border="blue")

HOW ?

Page 12: Example R usage for oracle DBA UKOUG 2013

select e.WAIT_CLASS,e.event_name,s.begin_interval_time,e.TOTAL_WAITS,e.TIME_WAITED_MS,e.TIME_WAITED_MS / TOTAL_WAITS "MS_PER_WAIT"from(select instance_number,snap_id,WAIT_CLASS,event_name,total_waits - first_value(total_waits) over (partition by event_name order by snap_id rows 1 preceding) "TOTAL_WAITS",(time_waited_micro - first_value(time_waited_micro) over (partition by event_name order by snap_id rows 1 preceding))/1000 "TIME_WAITED_MS"fromdba_hist_system_eventwhereWAIT_CLASS like nvl('&WAIT_CLASS',WAIT_CLASS)and event_name like nvl('&event_name',event_name)and instance_number = (select instance_number from v$instance)) e, dba_hist_snapshot swhere e.TIME_WAITED_MS > 0and e.instance_number=s.instance_numberand e.snap_id=s.snap_idand s.BEGIN_INTERVAL_TIME >= trunc(sysdate-&sysdate_nb_day_begin_interval+1)and s.BEGIN_INTERVAL_TIME <= trunc(sysdate-&sysdate_nb_day_end_interval+1) order by 1

Example 2: Retrieve and visualize wait events metrics from AWR

Page 13: Example R usage for oracle DBA UKOUG 2013

Example 2: Retrieve and visualize wait events metrics from AWR

Page 14: Example R usage for oracle DBA UKOUG 2013

# Plot the 4 graphs par(mfrow =c(4,1)) plot(sqevent[,'DATEEVT'],sqevent[,'TIME_WAITED_MS'],type="l",col="blue"

,xaxt="n",main=event,cex.main=2,xlab="",ylab="TIME_WAITED_MS") Axis(side=1,at=sqevent$DATEEVT,round(skip),format="%Y/%m/%d %H:

%M") plot(sqevent[,'DATEEVT'],sqevent[,'TOTAL_WAITS'],type="l",col="blue",xax

t="n",xlab="",ylab="NB_WAITS") Axis(side=1,at=sqevent$DATEEVT,round(skip),format="%Y/%m/%d %H:

%M") plot(sqevent[,'DATEEVT'],sqevent[,'MS_PER_WAIT'],type="l",col="blue",xa

xt="n",xlab="",ylab="MS_PER_WAIT") Axis(side=1,at=sqevent$DATEEVT,round(skip),format="%Y/%m/%d %H:

%M") hist(sqevent[,'MS_PER_WAIT'],xlab="MS_PER_WAIT",main=NULL,border="

blue")

How ?

Page 15: Example R usage for oracle DBA UKOUG 2013

Query is a little bit complicated (comes from OEM)

Example 3: Retrieve and visualize ASM Disk Group Usage per database

Page 16: Example R usage for oracle DBA UKOUG 2013

OEM view

Page 17: Example R usage for oracle DBA UKOUG 2013

Launch the script

Page 18: Example R usage for oracle DBA UKOUG 2013

R view

Page 19: Example R usage for oracle DBA UKOUG 2013

# compute percentages pct <- round(dg_space[,'SIZE_GB']/sum(dg_space[,'SIZE_GB'])*100)

# add % pct <- paste(pct,"%",sep="")

# Add db size to db_name db_name_size<-paste(dg_space[,'DB_NAME']," (",sep="") db_name_size<-paste(db_name_size,dg_space[,'SIZE_GB'],sep="") db_name_size<-paste(db_name_size," GB)",sep="")

# Set the colors colors<-rainbow(length(dg_space[,'DB_NAME']))

# Plot pie(dg_space[,'SIZE_GB'], labels = pct, col=colors, main=paste(dg," Disk Group

Usage",sep=""))

# Add a legend legend(x=1.2,y=0.5,legend=db_name_size,fill=colors,cex=0.8)

How ?

Page 20: Example R usage for oracle DBA UKOUG 2013

Basically the script takes a snapshot based on the v$sysstat view then computes and graphs the delta with the previous snapshot.

Example 4: Retrieve and visualize system statistics in real time

Page 21: Example R usage for oracle DBA UKOUG 2013

Launch the script

Page 22: Example R usage for oracle DBA UKOUG 2013

Example 4: Retrieve and visualize system statistics in real time

Page 23: Example R usage for oracle DBA UKOUG 2013

myquery<-"Select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') as DATEEVT, NAME,VALUE,1 VALUE_PER_SEC from v$sysstatwhere name='"# Keep themprev_date<<-qoutput[,'DATEEVT']prev_value<<-qoutput[,'VALUE']# Launch the loop for the real-time graphnb_refresh <- as.integer(nb_refresh)

for(i in seq(nb_refresh)) { # Get the new data Sys.sleep(refresh_interval) qoutput<-dbGetQuery(conn,myquery)# Keep the current value current_date<-qoutput[,'DATEEVT'] current_value<-qoutput[,'VALUE']

# compute difference between snap for value and value per sec #qoutput[,'DATEEVT']<-current_date qoutput[,'VALUE']<-current_value-prev_value

How ?

Page 24: Example R usage for oracle DBA UKOUG 2013

Basically the script takes a snapshot based on the v$system_event view then computes and graphs the delta with the previous snapshot.

Example 5: Retrieve and visualize in real time wait events metrics

Page 25: Example R usage for oracle DBA UKOUG 2013

Example 5: Retrieve and visualize in real time wait events metrics

Page 26: Example R usage for oracle DBA UKOUG 2013

myquery<-"Select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') as DATEEVT, EVENT,TOTAL_WAITS,TIME_WAITED_MICRO/1000 as TIME_WAITED_MS,1 MS_PER_WAIT from v$system_event where event='"# Keep themprev_tw<<-qoutput[,'TIME_WAITED_MS']prev_twaits<<-qoutput[,'TOTAL_WAITS']# So we want 4 graphspar(mfrow =c(4,1))

# Launch the loop for the real-time graphnb_refresh <- as.integer(nb_refresh)

for(i in seq(nb_refresh)) { # Get the new data Sys.sleep(refresh_interval)qoutput<-dbGetQuery(conn,myquery)# compute difference between snap for time_waited_ms, total_waits and then compute ms_per_wait qoutput[,'TIME_WAITED_MS']<-current_tw-prev_tw qoutput[,'TOTAL_WAITS']<-current_twaits-prev_twaits

How ?

Page 27: Example R usage for oracle DBA UKOUG 2013

myquery<-"select to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') as DATEEVT,wait_class as WAIT_CLASS,time_waited_micro/1000 as TIME_WAITED_MS,EVENT as EVENTfrom v$system_event where WAIT_CLASS != 'Idle'unionselect to_char(sysdate,'YYYY/MM/DD HH24:MI:SS') as DATEEVT,'CPU' as WAIT_CLASS,sum(value/1000) as TIME_WAITED_MS,'CPU' as EVENTfromv$sys_time_modelwherestat_name IN ('background cpu time', 'DB CPU')"

Example 6: Monitor the database activity in real time

Page 28: Example R usage for oracle DBA UKOUG 2013

Launch the script

Page 29: Example R usage for oracle DBA UKOUG 2013

Sub-graph for the time waited (in ms) per wait class:

Example 6: Monitor the database activity in real time

Page 30: Example R usage for oracle DBA UKOUG 2013

Sub-graph for the wait events distribution of the wait class having the max time waited during the last snap:

Example 6: Monitor the database activity in real time

Page 31: Example R usage for oracle DBA UKOUG 2013

Sub-graph for the wait class distribution since the script has been launched:

Example 6: Monitor the database activity in real time

Page 32: Example R usage for oracle DBA UKOUG 2013

Finally

Page 33: Example R usage for oracle DBA UKOUG 2013

# Split the screen no_display<-split.screen( figs = c( 2, 1 ) )

# Split the second screen no_display<-split.screen( figs = c( 1, 2 ),

screen=2 )

Much more complicated: source code is available through my blog.

How ?

Page 34: Example R usage for oracle DBA UKOUG 2013

Retrieve and visualize in real time the output of my asm_metrics.pl utility.

What is yours ?

Next Challenge