Monday, May 18, 2015

Faster Access Rows inside Table Using ROWID pseudocolumn.

ROWID : ROWID is pseudo column that returns the address of the row. It is faster to access data from table. This column are unique for an particular table. Rows in different tables that are stored together in the same cluster can have the same ROWID .

This is combination of :

The data object number of the object.
The data block in the datafile.
The position of the row in the data block (first row is 0).
The datafile number of tablespace.

SQL> SET TIMING ON;
SQL> CREATE TABLE BIG_TABLE
  2  (ID NUMBER(10),
  3  NAME VARCHAR2(200)
  4  );

Table created.

Elapsed: 00:00:00.05
SQL> INSERT INTO BIG_TABLE (ID , NAME)
  2  SELECT LEVEL, NULL FROM DUAL CONNECT BY LEVEL <= 100000;

100000 rows created.

Elapsed: 00:00:00.11
SQL> COMMIT;

Commit complete.

Elapsed: 00:00:00.01
SQL> DECLARE
  2  TYPE REC_DATA IS RECORD
  3  (
  4  ID BIG_TABLE.ID%TYPE
  5  );
  6  TYPE TT_DATA IS TABLE OF REC_DATA INDEX BY PLS_INTEGER;
  7  T_DATA TT_DATA;
  8  BEGIN
  9     SELECT ID BULK COLLECT INTO T_DATA FROM BIG_TABLE ;
 10
 11     FORALL IND IN T_DATA.FIRST .. T_DATA.LAST
 12     UPDATE BIG_TABLE
 13     SET NAME ='NAME '||T_DATA(IND).ID
 14     WHERE ID=T_DATA(IND).ID;
 15  END;
 16  /

PL/SQL procedure successfully completed.

Elapsed: 00:04:59.99
SQL> commit;

Commit complete.

Elapsed: 00:00:00.01
SQL> DECLARE
  2  TYPE REC_DATA IS RECORD
  3  (
  4  ID BIG_TABLE.ID%TYPE,
  5  ROW_ID VARCHAR2(1000)
  6  );
  7  TYPE TT_DATA IS TABLE OF REC_DATA INDEX BY PLS_INTEGER;
  8  T_DATA TT_DATA;
  9  BEGIN
 10     SELECT ID,ROWID BULK COLLECT INTO T_DATA FROM BIG_TABLE ;
 11
 12     FORALL IND IN T_DATA.FIRST .. T_DATA.LAST
 13     UPDATE BIG_TABLE
 14     SET NAME ='NAME '||T_DATA(IND).ID
 15     WHERE ROWID=T_DATA(IND).ROW_ID;
 16  END;
 17  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:01.55
SQL>

No comments:

Post a Comment