Hi everyone, I’ll briefly show how to enable monitoring of the modifications made to the tables of an Oracle database.
The main purpose of this kind of monitoring, in my view, is to know which tables are changed the most and how many changes those tables have had since the last statistics gathering. Why since the last statistics gathering? Because every time statistics are gathered on a table, this information is reset to zero.
You know that calculation Oracle does to find out when statistics are stale? Well, you can do the same calculation by querying the dba_tab_modifications view. By default, when a table has 10% of its rows changed, Oracle considers its statistics stale, so if you use dbms_stats.gather_*_stats with OPTIONS=>’GATHER STALE’, statistics will be gathered after that amount of changes. You can change the Stale Statistics percentage with the dbms_stats.set_*_prefs procedure and the STALE_PERCENT parameter.
My Oracle version is 12.1.0.2, but all of this works the same for Oracle Database 11g and 10g (I believe it also works on 9i, but I think only the flush_database_monitoring_info procedure doesn’t exist there, I haven’t tested it).

On to the examples and tests…
First I’ll create a test table.
SQL> create table t as select * from hr.jobs;
Table created.
Let’s check how this table is created by default.
SQL> SET PAGES 200 LIN 200
SQL> COL OWNER FOR A20
SQL> COL TABLE_NAME FOR A30
SQL> select owner, table_name, monitoring, num_rows, blocks, empty_blocks, avg_space, to_char(last_analyzed, 'dd/mm/yyyy hh24:mi:ss') last_analyzed
from dba_tables
where owner='SYS' and table_name='T';
OWNER TABLE_NAME MON NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE LAST_ANALYZED
-------------------- ------------------------------ --- ---------- ---------- ------------ ---------- -------------------
SYS T YES
Notice that the table is already created with monitoring enabled. This is because the statistics_level parameter of my database is set to TYPICAL. The same happens if the parameter is ALL; monitoring is only not enabled by default if the table is created while the parameter is set to BASIC. (See the details in section 13.3.1.8 of the Performance Tuning Guide)
It’s important to know how to disable and enable monitoring on tables.
To disable it:
SQL> alter table T nomonitoring;
Table altered.
Then we check that it was disabled:
SQL> select owner, table_name, monitoring from dba_tables where owner='SYS' and table_name='T';
OWNER TABLE_NAME MON
-------------------- ------------------------------ ---
SYS T NO
Now let’s enable it again:
SQL> alter table T monitoring;
Table altered.
Now we can confirm that the table is being monitored again:
SQL> select owner, table_name, monitoring from dba_tables where owner='SYS' and table_name='T';
OWNER TABLE_NAME MON
-------------------- ------------------------------ ---
SYS T YES
Let’s check whether our table already has any modifications:
SQL> select table_name,inserts,updates,deletes,truncated,timestamp
from dba_tab_modifications
where table_name='T';
no rows selected
Let’s insert some records, and make some updates and deletes on this table so we can see the result of the monitoring.
SQL> insert into t values ('OP_MGR', 'Operations Manager', 10000, 20000);
1 row created.
SQL> insert into t values ('BD_MEM', 'Board Member', 100000, 500000);
1 row created.
SQL> insert into t values ('IT_MGR', 'IT Manager', 10000, 30000);
1 row created.
SQL> update t set max_salary=35000 where job_id='IT_MGR';
1 row updated.
SQL> update t set max_salary=50000 where job_id='OP_MGR';
1 row updated.
SQL> update t set max_salary=900000 where job_id='BD_MEM';
1 row updated.
SQL> delete t where job_id='BD_MEM';
1 row deleted.
SQL> commit;
Commit complete.
Let’s check whether the changes were recorded by the table monitoring:
SQL> select table_name,inserts,updates,deletes,truncated,timestamp
from dba_tab_modifications
where table_name='T';
no rows selected
Huh, where are the changes made to table T?
This information doesn’t always go to the tables immediately, so we need to flush it from the Oracle instance’s memory area to the tables using the dbms_stats.flush_database_monitoring_info procedure.
SQL> exec dbms_stats.flush_database_monitoring_info;
PL/SQL procedure successfully completed.
Now we can see the changes that table T went through.
SQL> select table_name,inserts,updates,deletes,truncated,timestamp
from dba_tab_modifications
where table_name='T';
TABLE_NAME INSERTS UPDATES DELETES TRU TIMESTAMP
------------------------------ ---------- ---------- ---------- --- ---------
T 3 3 1 NO 10-SEP-16
We can see that when statistics are gathered on the table, the monitoring information is deleted.
SQL> exec dbms_stats.gather_table_stats('SYS','T');
PL/SQL procedure successfully completed.
SQL> select table_name,inserts,updates,deletes,truncated,timestamp
from dba_tab_modifications
where table_name='T';
no rows selected
The flush of information from memory to the tables happens at different intervals/moments depending on the version. For Oracle 9i I read somewhere that the information is flushed every 15 minutes, and for 10g every 3 hours, but I think that in the current versions, 11g and 12c, this interval no longer applies; from what I was able to validate, the flush happens when the buffer gets full. When I find the reference I’ll update the article, because I believe I read this in some book.
This is a very simple but interesting piece of understanding about how the Oracle database behaves.
I’ve even used this kind of procedure from the article as a sort of trace of sessions coming from an application server running Tomcat, to find out which tables were being changed by a given process of the system. In that scenario only one user was working on the system, but since it wasn’t possible to isolate the session, it was the only way I found to identify something of what the system was doing, and it helped the developer fix the code.
I hope it’s useful to you, and if you liked it, like and share it to spread knowledge in the community.
As a testing tip, I suggest you create a sample table, make 11% changes to it and then gather statistics with the Gather Stale option to validate what I said. You’ll see that it really works and you’ll know how Oracle calculates stale statistics.
References:
http://docs.oracle.com/cd/E25178\_01/server.1111/e16638/stats.htm#i41506
Regards,
Franky

Comments