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.

Drop tablespace with missing datafile

Today i have my new server installed with oracle 10G and i have to migrate old oracle 8i to this newly machine. I've already succeed mograting from oracle8i to oracle 10G by using export / import utility in oracle (later i will post how to do it in this blog)

Unfortunately i'm accidentally delete one datafile (not the system datafile) and when i want to shutdown the database i get this error code

ORA-01157: cannot identify/lock data file 121 - see DBWR trace file

ORA-01110: data file 121: '/ora_data/ORADATA/USAGE_INDEX23_01.dbf'

to solve just do this simple step:

1.Shutdown abort your database to force shutdown

sqlplus > shutdown abort

2.Startup mount database

sqlplus > startup mount

3.After that issue this following command

sqlplus > ALTER DATABASE DATAFILE ‘’ OFFLINE DROP;

4.Then open the database

sqlplus > alter database open

5.Drop the tablespace by issue this following command

DROP TABLESPACE INCLUDING CONTENTS;

6.After that if you have backup your database you can recover your tablespace.

Remote desktop your Linux using Xmanager

It's really helpful to remote your Linux. For example if your Linux is in Data Center where the place is so cold and there is no 'cozy' place for you to install and configure something in your Linux that need X11(Like install Oracle using Oracle Universal Installer).


To remote your Linux into your workstation you need software to remote your Linux, for now i choose Xmanager (Quite simple but "LICENSED" huh). There is other tool like VNC or XMing(I think i choose XManager because i've use it in my Solaris Box long time ago and always got good connection using it)


So to enable your Linux for remote desktop just follow this simple instruction :


1. Open gdm.conf configuration file, usually located in /etc/X11/gdm/gdm.conf or /etc/gdm/custom.conf


2. Edit gdm.conf file, in section [gdcmp] change enable = false to enable = true


3. Change run level to 5 in file /etc/inittab (example : x:5:once:/etc/X11/prefdm -nodaemon


4. Restart gdm service by simply run command : gdm-restart


5. Find your Xmanager source, installed it but not crack it.


PS : To enable X graphic open your terminal using root and type xhost +


Now let see my Linux Box in my workstation

How to enable telnet in solaris 10

To enable telnet in Solaris open and edit file in /etc/default/login and add comment to this line CONSOLE=/dev/console


#CONSOLE=/dev/console


The step is :


-bash-3.00$vi /etc/default/login and then add commen(#) in this line CONSOLE=/dev/console


That's all and know you can remote your solaris

The points on installing Oracle in non windows environment

The points on installing Oracle in non windows environment is (from my point of view) are :



  1. Of course you must have the Installer source and check if the installer is compatible with your UNIX or LINUX version

  2. Check the packages needed by the oracle Installer to successfully installed in your UNIX (check the pkginfo) and Linux (check it use rpm command)

  3. Check your memory ( mostly oracle required 512 MB)

  4. Check your diskspace, including your tmp diskspace and another partition for the oracle.

  5. Create your Optimal Flexible Architecture (OFA), it's the oracle file folder structure

  6. Create DBA and OINSTALL group for installation

  7. Set environment variable for oracle, including oracle_home,oracle_base, etc.

  8. Create user oracle for installation.


This some useful link for Oracle installation in non windows environment :


oracle documentation


ORACLE-BASE