Rabu, 23 April 2014

Oracle Database Monitoring

  • Why Monitor Oracle Database
  • Database need to be monitored to check the performance of the monitored database, so we can change the database parameter based on result from monitoring and keep the database in it’s best performance.
    Database monitoring also needed so we always know the condition of database, and know the symptom of our database and we can take action right before more problem happens to database.
  • Types of Oracle Database Monitoring
  • Database Monitoring can be grouped into three types :
  • 1. Status monitoring
  • This monitoring type monitors current status of an events and give reports or alert when it exceeds a defined threshold
    Examples of Status Monitoring are, check tablespace where it’s about to fills up, check segment near their maximum extents.
  • 2. Performance monitoring
  • Monitors oracle instance memory (sga, buffer, etc), sql query running in database, session which consumes a lot of memory, etc.
    Examples of Performance monitoring are, query from v_$sqlarea to check what SQL has been running in our Oracle Machine, query from v$sgastat and query to get hit ratio for cache , library and buffer cache.
  • 3. Trend Analysis
  • Collects historical data on specific event that occur in database like SGA status, segment that cannot extends, filled up tablespace and make analysis from that historical data so that we can predict the status of SGA in any given time or we can now when the tablespace will filled up.
    Examples of Trend Analysis are, make a histroy table for events we monitored. We can make history table for tablespace so that from that history table we could gain information and make prediction when will the time the tablespace will be filled up. Or we can make history table from v_$session, so from that table we can know what time our database accessed by so many user.

Selasa, 22 Desember 2009

Explain Plan in Oracle

Explain plan in Oracle shows execution plan in Oracle which is choose by oracle optimizer for select statements, insert, update and delete statements. Informations we can get from explain plan in Oracle are :
  • Table used for sql statement we want to explain
  • Access method in tables we access.
  • Steps for executing the sql
  • join method
  • sorting and agregating
Steps using explain plan in Oracle :
  • Create table plan by execute script utlxplan.sql locates in $ORACLE_HOME/rdbms/admin/utlxplan.sql
  • Use explain plan by using this sql : EXPLAIN PLAN
    SET STATEMENT_ID = 'some identifier'
    INTO plan_table
    FOR sql_statement;
  • Check explain plan result : Select * from PLAN_TABLE
    where statement_id = 'some identifier';
  • If we use cost based optimizer we can use this sql to check explain plan result : SELECT LPAD(' ',2*(level-1)) || operation || ' ' || options || ' '
    ||object_name||' '||
    DECODE(id,0,'Cost = '|| position) AS "Query Plan"
    FROM plan_table
    START WITH id = 0
    AND statement_id='SQL Pak Hari'
    CONNECT BY PRIOR ID = PARENT_ID
    AND statement_id = 'SQL Pak Hari';
  • Thats all

Apache on Solaris 10

Solaris 10 already have Apache in it, in order to use it you just need to start the service. The step to start the Apache server are :
1. Go to /usr/apache/bin : #cd /usr/apache/bin
2. To start the service : #./apachectl start
You will get error message if you don’t have httpd.conf located in /etc/apache/
To create htttpd.conf just copy httpd.conf-example located in /etc/apache/ :
#cp /etc/apache/httpd.conf-example /etc/apache/httpd.conf
After that try again run command #./apachectl start
If it success than in your solaris will show message ./apachectl start: httpd started
3.After that you can try your Apache server using your browser and type : http://yourIPserver/

That’s all

RMAN setup using catalog database quick guide

To setup RMAN using catalog database follow these steps :
1. Create user for catalog database repository in catalogue database
-Create tablespace for catalog database
sql>CREATE TABLESPACE RMAN_CAT
DATAFILE ‘RMAN_CAT’ SIZE 500 M
-Create user for catalog database
sql>create user rman_cat identified by rman_cat
temporary tablespace temp
default tablespace rman_cat
quota unlimited on rman_cat
-Grant appropriate privilege for user created
sql>grant recovery_catalog_owner,resource,create session to rman_cat
-Connect to recovery catalog using RMAN
$rman catalog rman_cat/rman_cat@rman_cat
RMAN>create catalog tablespace rman_cat
2. Connect to target database (Database to be backup) and catalog database using RMAN
-Connect using rman and register database
$rman target sys/pass@target catalog rman_cat/rman_cat@rman_cat
RMAN>register database;
-Check RMAN configuration
RMAN> Show all;
Command above will see RMAN configuration

Automate kill inactive user in oracle database


Step by step to automatically kill inactive user in oracle database
To see inactive user in oracle database you can use this query “select * from v$session where status=’INACTIVE’ or user=’sys’”
1. Create procedure for killing inactive oracle database user (oracle user have to be grant dbms_sql to execute this procedure)
CREATE OR REPLACE PROCEDURE kill_session_1 ( session_id in varchar2,serial_num in varchar2)
AS
cur INTEGER;
ret INTEGER;
string VARCHAR2(100);
BEGIN

– Comment out the following three lines to
– not use KILL

string :=’ALTER SYSTEM KILL SESSION’ || CHR(10) ||CHR(39)||session_id||’,'||serial_num||CHR(39);

– Uncomment the following 4 lines to use DISCONNECT

–string := ‘ALTER SYSTEM DISCONNECT SESSION’ || CHR(10) || CHR(39)||session_id||’,'||serial_num||CHR(39)||CHR(10)||’ POST_TRANSACTION’;
cur := dbms_sql.open_cursor;
dbms_sql.parse(cur,string,dbms_sql.native);
ret := dbms_sql.execute(cur) ;
dbms_sql.close_cursor(cur);
END;
/
2. Create sql script ORA_KILL.sql for spooling command to execute procedure created (see no 1)
REM
REM ORA_KILL.SQL
REM FUNCTION: Kills nonessential Oracle sessions (those that aren’t
REM owned)
REM : by SYS or “NULL”
REM DEPENDENCIES: Depends on kill_session procedure
REM MRA 9/12/96
REM
SET HEADING OFF TERMOUT OFF VERIFY OFF ECHO OFF
SPOOL kill_all.sql
SELECT ‘EXECUTE kill_session_1(‘||chr(39)||sid||chr(39)||’,'||
chr(39)||serial#||chr(39)||’);’ FROM v$session
WHERE username = ‘ECARE2′ and status=’INACTIVE’
/
SPOOL OFF
START kill_all.sql

3. Create shell script to automate execute sql script using cronjob (for unix user)
sqlplus -s $schema/$pass@$dbLink as sysdba<set timing on
@ORA_KILL

Jumat, 17 April 2009

SQL query for monitoring (next article Oracle Database Monitoring For Beginner)

This article is the sequel of my article Oracle Database Monitoring for Beginner

1. Query for Status Monitoring
Examples of Status Monitoring are, check tablespace where it's about to fills up, check segment near their maximum extents.
-Tablespace related
This query will show tablespace with percentage free less than 20 percent or equal 20 percent
select ddf.TABLESPACE_NAME,
ddf.BYTES,
ddf.BYTES-DFS.BYTES "BYTES_USED",
round(((ddf.BYTES-dfs.BYTES)/ddf.BYTES)*100,2) "PERCENT_USED",
dfs.BYTES "BYTES_FREE",
round((1-((ddf.BYTES-dfs.BYTES)/ddf.BYTES))*100,2) "PERCENT_FREE"
from (select TABLESPACE_NAME,
sum(BYTES) bytes
from dba_data_files
where autoextensible='NO'
group by TABLESPACE_NAME) ddf,
(select TABLESPACE_NAME,
sum(BYTES) bytes
from dba_free_space
group by TABLESPACE_NAME) dfs
where ddf.TABLESPACE_NAME=dfs.TABLESPACE_NAME and round((1-((ddf.BYTES-dfs.BYTES)/ddf.BYTES))*100,2) < 20 or round((1-((ddf.BYTES-dfs.BYTES)/ddf.BYTES))*100,2) = 20
order by ((ddf.BYTES-dfs.BYTES)/ddf.BYTES) desc

Query to show tablespace and the datafile
SELECT dfs.TABLESPACE_NAME, SUBSTR (df.NAME, 1, 70) file_name, df.bytes / 1024 / 1024 allocated_mb,
((df.bytes / 1024 / 1024) - NVL (SUM (dfs.bytes) / 1024 / 1024, 0))
used_mb,
NVL (SUM (dfs.bytes) / 1024 / 1024, 0) free_space_mb
FROM v$datafile df, dba_free_space dfs
WHERE df.file# = dfs.file_id(+)
GROUP BY dfs.file_id, df.NAME, df.file#, df.bytes, dfs.TABLESPACE_NAME
ORDER BY file_name

-Oracle Objects Extents related
Show objects more than 50% of Max Extents
select OWNER,
TABLESPACE_NAME,
SEGMENT_NAME,
SEGMENT_TYPE,
BYTES,
EXTENTS,
MAX_EXTENTS,
(EXTENTS/MAX_EXTENTS)*100 \"PERCENTAGE\"
from dba_segments
where SEGMENT_TYPE in ('TABLE','INDEX')
and EXTENTS > MAX_EXTENTS/2
order by (EXTENTS/MAX_EXTENTS) desc

Show Segment near their maximum extents
SELECT e.owner,
e.segment_type,
Substr(e.segment_name, 1, 30) "SEGMENT_NAME",
Trunc(s.initial_extent/1024) "INITIAL K",
Trunc(s.next_extent/1024) "NEXT K",
s.max_extents,
Count(*) as extents
FROM dba_extents e,
dba_segments s
WHERE e.owner = s.owner
AND e.segment_name = s.segment_name
AND e.owner NOT IN ('SYS', 'SYSTEM')
GROUP BY e.owner, e.segment_type, e.segment_name, s.initial_extent, s.next_extent, s.max_extents
HAVING Count(*) > s.max_extents - 10
ORDER BY e.segment_type, e.owner, e.segment_type, Count(*) DESC

-Show Oracle session related and memory used
SELECT NVL(a.username,'(oracle)') AS username,
a.sid,
a.serial#,
a.machine,
a.module,
a.program,
a.LOGON_TIME,
a.status,
a.process,
Trunc(b.value/1024) AS memory_kb
FROM v$session a,
v$sesstat b,
v$statname c
WHERE a.sid = b.sid
AND b.statistic# = c.statistic#
AND c.name = 'session pga memory'
AND a.program IS NOT NULL
ORDER BY MEMORY_KB desc, a.MODULE asc,username desc

2. Query for Performance Monitoring
-Show Oracle Hit Ratio
Dictionary Hit Ratio
The ratio of cache hits to cache lookup attempts in the data dictionary cache. In other words, the rate the database goes to the dictionary instead of the hard disk to retrieve data. A low ratio suggests more RAM should be added
The Dictionary Hit Ratio value have to be more than 90 percent
SELECT sum(gets) "GETS",sum(getmisses) "CACHE MISSES",round((1 - (sum(getmisses) / sum(gets))) * 100,3) "DICTIONARY"
FROM v$rowcache

Buffer Hit Ratio
The rate the database goes to the buffer instead of the hard disk to retrieve data. A low ratio suggests more RAM should be added to the system.
Buffer hit ratio have to be more than 85%
SELECT cur.value "block",con.value "con",phy.value "phy", ROUND((1-(phy.value / (cur.value + con.value)))*100,2) "BUFFER"
FROM v$sysstat@".$dbName." cur, v$sysstat@".$dbName." con, v$sysstat@".$dbName." phy
WHERE cur.name = 'db block gets'
AND con.name = 'consistent gets'
AND phy.name = 'physical reads'

Library Hit Ratio
Monitors the percentage of entries in the library cache that were parsed more than once (reloads) over the lifetime of the instance.
Library Hit Ratio have to be more than 95%
SELECT sum(pins) "EXEC", sum(pinhits) "EXEC HITS", round((sum(pinhits) / sum(pins)) * 100,3) "HIT RATIO", sum(reloads) "MISSES",round((sum(pins) / (sum(pins) + sum(reloads))) * 100,3) "HITRATIO"
FROM v$librarycache

-Show memory usage by user connected
SELECT NVL(a.username,'(oracle)') AS username,
a.machine,
a.module,
a.program,
Trunc(b.value/1024) AS memory_kb
FROM v$session a,
v$sesstat b,
v$statname c
WHERE a.sid = b.sid
AND b.statistic# = c.statistic#
AND c.name = 'session pga memory'
AND a.program IS NOT NULL
ORDER BY MEMORY_KB desc, a.MODULE asc,username desc

-Show Oracle Sort Information
select name,to_char(value,'999,999,999,999,999') "VALUE"
from sys.v_$sysstat
where name like 'sort%'
Show hit ratio for sort
Sorting percentage on disk have less than 5 %. If the value more than 5 % then change parameter SORT_AREA_SIZE.
select to_char(100*a.value/decode((a.value+b.value),0,1,(A.value+b.value)),'999.90') "VALUE"
from sys.v_$sysstat a, sys.v_$sysstat b
where a.name = 'sorts (disk)'
and b.name = 'sorts (memory)'

-Show Oracle SGA Info
select name, to_char(value,'999,999,999,999,999') "VALUE" from sys.v_$sga

-Show Oracle Invalid Objects
This query is to show invalid objects in Oracle
select OWNER,
OBJECT_TYPE,
OBJECT_NAME,
STATUS
from dba_objects
where STATUS = 'INVALID'
order by OWNER, OBJECT_TYPE, OBJECT_NAME

Solution :
if the object invalid you can recompile it or you can delete it if it's not used.
sql : SQL>ALTER PROCEDURE PROC_NAME COMPILE ;
SQL>ALTER PACKAGE PACKAGE_NAME COMPILE ;
SQL>ALTER VIEW VIEW_NAME COMPILE ;
Or if you have many invalid objects run this script reside in : $ORACLE_HOME/rdbms/admin/utlrp.sql
Login to sqlplus
sqlplus > @utlrp

-Show Query running in Oracle
This query will show currently queries running in Oracle.
SELECT *
FROM (SELECT Substr(a.sql_text,1,50) sql_text,Trunc(a.disk_reads/Decode(a.executions,0,1,a.executions)) reads_per_execution,a.buffer_gets,a.disk_reads,a.executions,a.sorts,a.address FROM v$sqlarea a) WHERE rownum <= 10

To be continued .................................

Minggu, 29 Maret 2009

G stands for GRID (because im curious with 10G)

Grid, the word came from the GRID ELECTRICITY analogy, when we need power electricity to make our tools , server, tv , etc works, we simply connect them to electric outlet without have to know where the electricity came from (without have to know from which generator the electricity came)

When IT Resources(hardware or software) needs resources like database, CPU or other resources, this IT Resource can easily have it like our tools, tv , etc needs electricity(just plug in to electric outlet). The point is, user who is using GRID, doesnt have to know where the data reside or from which server that process the request, they only have to know the resource they request is available and can be used. Next will be RAC (Real Application Cluster) in my Data Center, user using resources have extra availabilty and performance like load balancing CMIIW)

And for administrator, they can monitor these small resources likes a single entity that can be simply manage.

So that IT resource can communicate with other IT Resource, it needs standard, protocol and interface. That why SOA(Service Oriented Architecture) on developing IT became important, because of there so many differents IT resources with different protocol,standard and interface. This make problem for huge organization with huge IT resource with differents architecture to communicate each other and to integrate them.

The idea writing this posting, because of one Oracle 10G feature, Real Application Cluster(RAC) where in one single physical database consist of from 2 or more Oracle Instances where from that instances can communicate and share data. What makes me interested that these instances can be manage like we manages single database entity.

GRID in term of computer science defined as " A computational GRID is a hradware and software infrastructure that provide dependable ,consistent,pervasive, and inexpensive access to high-end computational capabilities". That definition from Fosster and Kessellman.

Yeah next will be all cluster, after hot backup(later i will write about this topic : hot backup and cold backup for Oracle), more performance and more availability.