jeudi 9 janvier 2014

How to find number of lines stored in a table's blocks

To find the number of lines per block for a specific table :
SQL> SELECT DBMS_ROWID.ROWID_BLOCK_NUMBER(rowid),COUNT(*) NUMBER_ROWS_PER_BLOCK
        FROM <my_table_name>
        GROUP BY DBMS_ROWID.ROWID_BLOCK_NUMBER(rowid)
        Order by COUNT(*) desc;

DBMS_ROWID.ROWID_BLOCK_NUMBER(ROWID) NUMBER_ROWS_PER_BLOCK
------------------------------------ ---------------------
                                 355                   127
                                2142                   127
                                 363                   127
                                5550                   127
                                4934                   127
                                5614                   127
                                5552                   127
                                 785                   127
                                 789                   127
And to find the file id :
SQL> select
     dbms_rowid.rowid_relative_fno(rowid) FILE#,
     dbms_rowid.rowid_block_number(rowid) BLOCK#,
     count(1)                             NUMBER_ROWS_PER_BLOCK
  from <my_table_name>
  group by
     dbms_rowid.rowid_relative_fno(rowid),
     dbms_rowid.rowid_block_number(rowid)
  order by 1,2
SQL> /

     FILE#     BLOCK#  NUMBER_ROWS_PER_BLOCK
---------- ---------- ----------------------
         5       2024        100
         5       2025        115
         5       2026        115

To dump the content of a block to the trace file, use the following command :
SQL> alter system dump datafile 5 block min 2024 block max 2024;
Now, to find the trace file you need to find the process id of your current session :
SQL> select p.spid from v$session s, v$process p
        where s.sid = (select distinct sid from v$mystat)
              and p.addr = s.paddr;