Поиск по этому блогу

четверг, 11 октября 2012 г.

Who is locking my tables


SELECT oracle_username USERNAME, owner OBJECT_OWNER,
object_name, object_type, s.osuser,s.sid,s.serial#,
DECODE(l.block,
  0, 'Not Blocking',
  1, 'Blocking',
  2, 'Global') STATUS,
  DECODE(v.locked_mode,
    0, 'None',
    1, 'Null',
    2, 'Row-S (SS)',
    3, 'Row-X (SX)',
    4, 'Share',
    5, 'S/Row-X (SSX)',
    6, 'Exclusive', TO_CHAR(lmode)
  ) MODE_HELD
FROM gv$locked_object v, dba_objects d,
gv$lock l, gv$session s
WHERE v.object_id = d.object_id
AND (v.object_id = l.id1)
and v.session_id = s.sid
ORDER BY oracle_username, session_id;

понедельник, 8 октября 2012 г.


Bulk Update Methods Compared


For comparing update methods, lets create 2 very simple tables using 

CREATE TABLE AS SELECT 

DROP TABLE t_test;

CREATE  TABLE  t_test AS 
SELECT
LEVEL id,
trunc(dbms_random.value*10) val_prev,
0 target_upd1,
0 target_upd2
 FROM dual 
 CONNECT BY LEVEL <1000000;

DROP TABLE t_test2;    
                
CREATE  TABLE t_test2 AS 
SELECT 
LEVEL id,
trunc(dbms_random.value) val_prev,
trunc(dbms_random.value*100) source_upd1,
trunc(dbms_random.value*1000) source_upd2
 FROM dual 
 CONNECT BY LEVEL <1000000;

Both tables will contain milion rows. First table will have id columns as a quiz of numbers from 1 to 1 000 000, some value column which would not be updated, and two columns with 0 values, as target columns for updting. Second table also have id column as quiz and 2 columns vith random numbers as source updating columns.

For each table we will create B-Tree index on id column

CREATE INDEX t_test_idx  ON t_test(id) ;
   
CREATE INDEX t_test2_idx ON t_test2(id);  


Example first. Update with nested SET subquery : 


UPDATE t_test
SET    (target_upd1,target_upd2) = (
           SELECT source_upd1,source_upd2
           FROM   t_test2
           WHERE  id = t_test.id
)
WHERE  id IN (
           SELECT id
           FROM   t_test2
);

Lets go throw this statment line by line

UPDATE target_table_name
SET    (target columns list divided by , [This columns would be       updated]) = (
          SELECT [list of columns from source table needed for update]
          FROM target_table_name
          WHERE [join condition]
)
WHERE [list of id's from target table for updating] IN (
       SELECT [list of id's from source table for updating]
     FROM [source_table_name]
);

Explain plan without indexes.

UPDATE STATEMENT, GOAL = ALL_ROWS 22322484 743940 38684880
 UPDATE DWH T_TEST
  HASH JOIN RIGHT SEMI                  4284         743940 38684880
   INDEX FAST FULL SCAN DWH T_TEST2_IDX          626         1003453 13044889
   TABLE ACCESS FULL DWH T_TEST                  658         743940  29013660
  TABLE ACCESS BY INDEX ROWID DWH T_TEST2          14         10035 391365
   INDEX RANGE SCAN DWH T_TEST2_IDX           3         4014

The Cost is rather big so  we wil gathered statistics

begin
  dbms_stats.GATHER_TABLE_STATS('dwh','t_test');
   dbms_stats.GATHER_TABLE_STATS('dwh','t_test2');
  end;     
  
  
  begin
  dbms_stats.GATHER_INDEX_STATS('dwh','t_test_idx');
   dbms_stats.GATHER_INDEX_STATS('dwh','t_test_idx');
  end;         

Explain plan with indexes and staistics.



UPDATE STATEMENT, GOAL = ALL_ROWS 10003084 999999 13999986
 UPDATE DWH T_TEST
  HASH JOIN RIGHT SEMI                 3094         999999 13999986
   INDEX FAST FULL SCAN DWH T_TEST2_IDX         613         999999 4999995
   TABLE ACCESS FULL DWH T_TEST                 661         999999 8999991
  TABLE ACCESS BY INDEX ROWID DWH T_TEST2    4              1      12
   INDEX RANGE SCAN DWH T_TEST2_IDX         3              1

Thes cost in rather lower,but we will test second type of bulk updating.

Example second.Updateble Join View.

This type of Join needs an uniqueindex on target table id. (For example Primary key)

update(
       select new.id as id
       ,new.source_upd1 as source_upd1
       ,new.source_upd2 as source_upd2
       ,old.*
       from t_test2 new
       JOIN t_test old ON ( new.id = old.id) 
)
SET target_upd1 = source_upd1
    ,target_upd2 = source_upd2


Example third.Merge.


MERGE INTO t_test old
USING t_test2 new ON(old.id = new.id)
WHEN MATCHED THEN UPDATE SET 
     target_upd1 = source_upd1
    ,target_upd2 = source_upd2


After implementing indexes and gathering statistics Plan would be:

MERGE STATEMENT, GOAL = ALL_ROWS 3784 999999 29999970
 MERGE DWH T_TEST
  VIEW DWH
   HASH JOIN                         3784 999999 25999974
    TABLE ACCESS FULL DWH T_TEST                  661 999999 11999988
    TABLE ACCESS FULL DWH T_TEST2                  736 999999 13999986


  

понедельник, 9 июля 2012 г.

exists




Using EXISTS for join tables 


SELECT cr.name FROM currency cr WHERE EXISTS (SELECT 1 FROM event ev WHERE ev.currencyid = cr.currencyid)


PLAN_TABLE_OUTPUT
Plan hash value: 2107538316
----------------------------------------------------------------------------------------
| Id  | Operation             | Name           | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |                |    36 |   756 |    58   (4)| 00:00:01 |
|*  1 |  HASH JOIN SEMI       |                |    36 |   756 |    58   (4)| 00:00:01 |
|   2 |   TABLE ACCESS FULL   | CURRENCY       |   182 |  3094 |     3   (0)| 00:00:01 |
|   3 |   INDEX FAST FULL SCAN| EVENT_CURR_IDX | 89526 |   349K|    54   (2)| 00:00:01 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("EV"."CURRENCYID"="CR"."CURRENCYID")


Without EXISTS


SELECT DISTINCT  cr.name FROM currency cr,event ev WHERE ev.currencyid = cr.currencyid;


PLAN_TABLE_OUTPUT
Plan hash value: 1431156865
-----------------------------------------------------------------------------------------
| Id  | Operation              | Name           | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |                |   182 |  3822 |    63  (12)| 00:00:01 |
|   1 |  HASH UNIQUE           |                |   182 |  3822 |    63  (12)| 00:00:01 |
|*  2 |   HASH JOIN            |                | 89526 |  1835K|    58   (4)| 00:00:01 |
|   3 |    TABLE ACCESS FULL   | CURRENCY       |   182 |  3094 |     3   (0)| 00:00:01 |
|   4 |    INDEX FAST FULL SCAN| EVENT_CURR_IDX | 89526 |   349K|    54   (2)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("EV"."CURRENCYID"="CR"."CURRENCYID")

The shared pool

The shared pool is the portion of SGA contains the following main parts:

  1. The Library Cache includes sharable parts of SQL statments, PL/SQL procedures and packages
  2. The Data Dictionarry is a collection of database tables containing reference information about the database.
  3. The Result Cache is used for storing results of SQL statments and PL/SQL procedures.
  4. Lock Structures

Data buffer cache

The database buffer cache is part of SGA that holds a copies of data blocks that are read from data files.This cache is managed by LRU (Last recently used algoritm).  The first time an Ora DB Server process needs for some portion of data it tries to find them in the Data buffer cashe. If data are there Ora Process reads blocks from memory.

Difference between Oracle Database and Oracle Database Instance

An Oracle Database Server contains of Oracle Database and one or more Oracle Database instances.An instance contains with of memory structure and baskground process.Every tyme when instance is  started a shared memory is called SGA is allocated starting a background process.When users connect to an Oracle Database server, they are connected to an Oracle Database instance.

вторник, 21 февраля 2012 г.

Oracle BI PUblisher

Для группировки и фетча вне таблицы необходимо

<?for-each-group:ROW;./DEPARTMENT_NAME?><?sort:DEPARTMENT_NAME;'ascending';data-type='text'?>
<?end for-each-group?>

Для фетча внутри таблицы
<?for-each:current-group()?><?sort:SM_SAL;'ascending';data-type='text'?>
<?end for-each?>
Пример 
 <?for-each-group:ROW;./DEPARTMENT_NAME?><?sort:DEPARTMENT_NAME;'ascending';data-type='text'?>
<?DEPARTMENT_NAME?>
<?for-each:current-group()?><?sort:SM_SAL;'ascending';data-type='text'?>
<?FIRST_NAME?>
<?LAST_NAME?>
<?SALARY?>
<?SM_SAL?>
<?end for-each?>
<?end for-each-group?>