9 Ocak 2015 Cuma

CONSISTENT GET MECHANISM

As you know there are 2 types of IO operations on Oracle Database


A.physical reads: these are done by reading from disk (expensive reads). However, physical reads can also occur on IO devices (such as RAW,ASM) or disk cache.

B.logical reads: these are done by reading the data blocks on buffer cache. (performance reads). There are 2 types of logical reads:
  
          
B.1. Consistent Get: this is a read on buffer cache to access the data in the block which is consistent with a given point time or SCN. Consistent gets will be increased if it needs to use undo segments to apply the uncommited changes.
                               
B.2. DB Block Get(current get): this is a read on buffer cache to access the most up-to-date current copy of the data in the blcok. There can be only one current copy of a block. 

**db block gets are generally used while DML operations
                               

This is a very brief explanation of reads, now I will only try to explain consistent get mechanism.
                                 

What is consistent gets?

Actually, a consistent get is the number of accessing blocks in the buffer cache.

Example;


--create a tablespace with uniform extent size;

create tablespace test_tbs datafile '+DATA' size 100m extent management local uniform size 64k;

--create a table;

create table demo_objects tablespace test_tbs as select * from dba_objects where rownum between 1 and 300;

--gather the statistics

begin
  dbms_stats.gather_table_stats('HR', 'DEMO_OBJECTS', granularity => 'ALL');
end;

--check the number of rows and total number of blocks

select num_rows, blocks from dba_tables where table_name='DEMO_OBJECTS';

       NUM_ROWS     BLOCKS
          300             8

--check the data block and number of rows for each block

select dbms_rowid.ROWID_BLOCK_NUMBER(rowid) blkno, count(*) row_cnt
  from demo_objects
 group by dbms_rowid.ROWID_BLOCK_NUMBER(rowid)
 order by 1

              BLKNO      ROW_CNT
                131         76
                132         73
                133         72
                134         70
                135          9


--there are total 8 blocks but 5 of them are data blocks. So, what is the rest of 3 blocks?

select extent_id, block_id from dba_extents where segment_name='DEMO_OBJECTS'

       EXTENT_ID      BLOCK_ID
               0           128
               
***our first block is at 128th block in the 0th extent


--header block

select header_block from dba_segments s where s.segment_name='DEMO_OBJECTS'

       HEADER_BLOCK
               130


Question:
if our segment header block is 130, what are the 128th and 129th blocks?

Answer:

They are segment space management blocks
 




AUTOMATIC SEGMENT SPACE MANAGEMENT BLOCKS (BitMapBlocks)


Before the data blocks, every segment has "some segment management blcoks" to hold some information. When we start to read a table, these blocks also cause some consistent gets.


L1 Leaf   BMB---> always starts from first block at first extent, indicates the free space of blocks, can be more than one

L2 Branch BMB---> contains search hint for first L1 BMB

L3 Root   BMB---> segment header and L3 Root reside in same block




For our “demo_objects” table:

Block# 128------------> L1 BMB

Block# 129------------> L2 BMB

Block# 130------------> L3 BMB and segment header




CHECK THE STATISTICS AND CONSISTENT GETS

session#1
---------
select * from demo_objects;

Statistics
----------------------------------------------------------
         1  recursive calls
         0  db block gets
        26  consistent gets
         0  physical reads
         0  redo size
      35259  bytes sent via SQL*Net to client
       760  bytes received via SQL*Net from client
        21  SQL*Net roundtrips to/from client
         0  sorts (memory)
         0  sorts (disk)
       300  rows processed
 
We have 8 blocks for “demo_objects” table but consistent gets value is 26. So, it means we accessed the blocks multiple times.
 
Let's make a DML operation from another session (without commit) and check the consistent gets again; 

session#2
---------
SQL> update demo_objects set object_name='xxx' where rownum < 50;

49 rows updated.


session#1
---------
SQL> select * from demo_objects;

Statistics
----------------------------------------------------------
         0  recursive calls
         0  db block gets
        76  consistent gets
         0  physical reads
       116  redo size
35259  bytes sent via SQL*Net to client
       760  bytes received via SQL*Net from client
        21  SQL*Net roundtrips to/from client
         0  sorts (memory)
         0  sorts (disk)
       300  rows processed



As you see, consistent gets are increased from 26 to 76. Also there is no physical reads and db_block gets.
So where are these extra consistent gets are coming from? Answer is "undo segments"

After we rollback the session, you will see that consistent gets will be 26 again.



Now, how the consistent gets are happening?

To explain the mechanism, we should understand the array size first;

array size:  When you send a query to the database, the rows are fetched from the block according to the array size. The default value of  array size for SQL*Plus is 15. You can change this value if you want.

SQL> show arraysize
arraysize 15


Now, check how many rows each data block has for ‘demo_objects’ table;

select dbms_rowid.ROWID_BLOCK_NUMBER(rowid) blkno, count(*) row_cnt
  from demo_objects
 group by dbms_rowid.ROWID_BLOCK_NUMBER(rowid)
 order by 1

       BLKNO         ROW_CNT
           131             76
           132             73
           133             72
           134             70
           135              9


we have 5 data blocks between 131 and 135

76+73+72+70+9 = 300 rows

When we send the query, database will start to read 15 rows for each fetch.
 
For e.g.:
 
*****
Block# 132 Rows=72




**For the last fetch, there is only 12 rows but as we know fetch wants to read 15 rows.
So, what will be to the excess 3 rows?   These  rows will be read from next block.


**Another point is  Oracle starts the execution of query by finding the 1st row of the table. We can prove this from 10046 trace file:


r=  number of rows
cr= consistent reads

PARSE #139848017540176:c=1000,e=955,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=1714943783,tim=793948881469
EXEC  #139848017540176:c=0,e=21,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,plh=1714943783,tim=793948881532
FETCH #139848017540176:c=1000,e=242,p=0,cr=3,cu=0,mis=0,r=1,dep=0,og=1,plh=1714943783,tim=793948881828
FETCH #139848017540176:c=0,e=34,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948882270
FETCH #139848017540176:c=0,e=33,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948882561
FETCH #139848017540176:c=0,e=78,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948883180
FETCH #139848017540176:c=0,e=32,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948883455
FETCH #139848017540176:c=0,e=32,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948883730
FETCH #139848017540176:c=0,e=34,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948884373
FETCH #139848017540176:c=0,e=33,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948884658
FETCH #139848017540176:c=0,e=33,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948884979
FETCH #139848017540176:c=0,e=40,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948885401
FETCH #139848017540176:c=0,e=40,p=0,cr=2,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948885722
FETCH #139848017540176:c=0,e=85,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948886235
FETCH #139848017540176:c=0,e=36,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948886681
FETCH #139848017540176:c=0,e=31,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948886842
FETCH #139848017540176:c=0,e=32,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948887558
FETCH #139848017540176:c=0,e=35,p=0,cr=2,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948887873
FETCH #139848017540176:c=0,e=33,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948888158
FETCH #139848017540176:c=0,e=30,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948889540
FETCH #139848017540176:c=0,e=28,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948889818
FETCH #139848017540176:c=0,e=23,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948890134
FETCH #139848017540176:c=0,e=37,p=0,cr=2,cu=0,mis=0,r=14,dep=0,og=1,plh=1714943783,tim=793948890409
STAT #139848017540176 id=1 cnt=300 pid=0 pos=1 obj=94899 op='TABLE ACCESS FULL DEMO_OBJECTS (cr=26 pr=0 pw=0 time=1284 us cost=4 size=27900 card=300)'


As you see, the first fetch requests only 1 row and it makes 3 consistent reads.
We can say that, the first fetch used the "segment space management blocks" to find the 1st row and it makes 3 consistent gets for this operation.



Demonstration:

select * from demo_objects;


3 (first row) + 5 + 5 + 6 +6 + 1 = 26 consistent gets
We read 9+5=14 rows for the last fetch and we can check this from 10046 trace file

.
.
.
FETCH #139848017540176:c=0,e=28,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948889818
FETCH #139848017540176:c=0,e=23,p=0,cr=1,cu=0,mis=0,r=15,dep=0,og=1,plh=1714943783,tim=793948890134
FETCH #139848017540176:c=0,e=37,p=0,cr=2,cu=0,mis=0,r=14,dep=0,og=1,plh=1714943783,tim=793948890409



I wrote a little complicated query to calculate the consistent gets. I assumed that it will cost 3 cr to find the 1st row. This can change according to extent size and number of extents.

CALCULATION QUERY

select 'Finding 1st row' block_no,
       1 rows_1st_count,
       1 rows_2nd_count,
       0 last_fetch_rows,
       3 "block_touch(gets)"
  from dual
union all
select to_char(d.blok_no),
       d.row_count ROWs_1st_COUNT,
       case
         when rownum = 1 then
          d.final_cnt - 1
         else
          d.final_cnt
       end ROWS_2nd_COUNT,
       d.rows_left last_fetch_rows,
       case
         when rownum = 1 and d.rows_left = 0 then
          d.block_acc
         when rownum != 1 and d.rows_left = 0 then
          d.block_acc + 1
         when lead(d.rows_left)
          over(order by d.blok_no) is null and d.rows_left = 0 then
          d.block_acc + 1
         when lag(d.rows_left) over(order by d.blok_no) = 0 then
          d.block_acc + 1
         else
          d.block_acc + 2
       end "block_touch(gets)"
  from (select c.*,
               case
                 when rownum = 1 then
                  (c.final_cnt - c.rows_left - 1) / 15
                 else
                  (c.final_cnt - c.rows_left) / 15
               end block_acc
          from (select b.blok_no,
                       b.row_count,
                       case
                         when rownum = 1 or lag(b.rows_left)
                          over(order by b.blok_no) = 0 then
                          b.row_count
                         else
                          (b.row_count -
                          (15 - lag(b.rows_left) over(order by b.blok_no)))
                       end final_cnt,
                       b.rows_left
                  from (select a.*, mod(rr - 1, 15) rows_left
                          from (select dbms_rowid.rowid_block_number(rowid) blok_no,
                                    count(*) row_count,
                                    sum(count(*)) over(order by dbms_rowid.rowid_block_number(rowid)) rr
                                  from demo_objects t
                                 group by dbms_rowid.rowid_block_number(rowid)
                                 order by 1) a) b) c) d


Query Result:


                                                                             

22 Mayıs 2014 Perşembe



SETUP ASM&ASMLib on Multipath Disks


1.       Install Correct Packages

We must be careful while downloading correct rpms. Before download the rpms, check your kernel version and processor(32 bit or 64 bit):

oracleasm-(kernel)  ---  must be same version with the kernel
oracleasmlib          --- can be the latest version of correct 32 or 64 bit version
oracleasm-support --- can be the latest version of correct 32 or 64 bit version

[root@racnode1 ~]# uname -a
Linux racnode1.msb.bak 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

[root@racnode1 ~]# rpm -qa | grep asm
oracleasm-support-2.1.8-1.el5
oracleasmlib-2.0.4-1.el5
oracleasm-2.6.18-194.el5-2.0.5-1.el5
[root@racnode1 ~]#

if you downloaded and installed wrong “oracleasm-(kernel)” rpm, you can get the following errors:

[root@racnode1 bin]# /etc/init.d/oracleasm start
Initializing the Oracle ASMLib driver: [FAILED]

[root@racnode1 bin]# more /var/log/oracleasm
"/var/log/oracleasm"
"/var/log/oracleasm" 145L, 5999CCreating /dev/oracleasm mount point: /dev/oracleasm
Loading module "oracleasm": failed
Unable to load module "oracleasm"
Creating /dev/oracleasm mount point: /dev/oracleasm
Loading module "oracleasm": failed
Unable to load module "oracleasm"
Reloading disk partitions: done
Cleaning any stale ASM disks...
Scanning system for ASM disks...
oracleasm-read-label: Unable to open device "/dev/sdb1": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-0": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-0": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-0": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-0": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-1": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-1": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-1": No such file or directory
oracleasm-read-label: Unable to open device "/dev/dm-1": No such file or directory


2.       Check your disks
[oracle@racnode1 ~]$ ls -lrt /dev/mapper/
total 0
brw-rw---- 1 root disk 253,  8 May 15 12:47 oradata2p1
brw-rw---- 1 root disk 253,  9 May 15 12:47 oradata3p1
brw-rw---- 1 root disk 253, 10 May 15 12:47 oradata1p1

3.       Create ASM disks
[root@racnode1 ~]# /etc/init.d/oracleasm createdisk ASM1 /dev/mapper/oradata1p1
[root@racnode1 ~]# /etc/init.d/oracleasm createdisk ASM2 /dev/mapper/oradata2p1

4.       For RAC systems, run the command for every node
[root@racnode1 ~]# /etc/init.d/oracleasm scandisks
Scanning the system for Oracle ASMLib disks:               [  OK  ]
[root@racnode1 ~]# /etc/init.d/oracleasm listdisks
ASM1

5.       Configure ASMLib for multipath (do the following operations for every node)

Modify /etc/sysconfig/oracleasm
ORACLEASM_SCANORDER=”dm”
ORACLEASM_SCANEXCLUDE=”sd”
Restart asmlib
/etc/init.d/oracleasm stop
/etc/init.d/oracleasm start

6.       Check disk configuration
ASMLib checks the /proc/partitions to detect the disks:

[root@racnode1 ~]# cat /proc/partitions | grep dm-
 253     0   90963968 dm-0
 253     1   51478528 dm-1
 253     2  536870912 dm-2
 253     3  536870912 dm-3
 253     4  536870912 dm-4
 253     5  536870912 dm-5
 253     6  104857600 dm-6
 253     7  536868171 dm-7
 253     8  536868171 dm-8
 253     9  536868171 dm-9
 253    10  536868171 dm-10
 253    11  104856223 dm-11

7.       ASM disks are mounted under the /dev/oracleasm/disks
root@racnode1 ~]# ls -la /dev/oracleasm/disks
brw-rw---- 1 oracle oinstall 253, 10 May 15 12:48 ASM1
brw-rw---- 1 oracle oinstall 253,  8 May 15 12:48 ASM2

8.       Major and Minor numbers of the disks have to be same in both /proc/partitions/ and /dev/oracleasm/disks/

/proc/partitions         253    10  536868171 dm-10
brw-rw---- 1 oracle oinstall 253, 10 May 15 12:48 ASM1
[root@racnode1 ~]# /etc/init.d/oracleasm querydisk -d ASM1
Disk "ASM1" is a valid ASM disk on device /dev/dm-10[253,10]


25 Nisan 2014 Cuma

How to change tablespace of sys.aud$ and how to shrink it?

To shrink the aud$ table, you have to enable "row movement". The default tablespace of aud$ is SYSAUX and because of that you need to change its tablespace first.

With 11g, changing the tablespace of aud$ table is supported.

begin

dbms_audit_mgmt.set_audit_trail_location(
audit_trail_type => dbms_audit_mgmt.audit_trail_aud_std,          audit_trail_location_value => 'AUDTBS');

end;

alter table sys.aud$ enable row movement;

alter table sys.aud$ shrink space cascade;




How to reset an expired database user without changing the password?

The simple method to reset a user is changing the password. If you dont have chance to change the password and if you can not remember the old one, you can use the following method:

select dbms_metadata.get_ddl('USER', username), '''[^'']+''') pswd, username from dba_users;



You can find the hashed passwords for all users in the database with this query. 

After that you can set the old password and reset the account.


alter user HR identified by values 'S:ASSDSKDEIRUIERUTEUYTEIEORIEORIEPORI;ERIUERIUTY';