Escolar Documentos
Profissional Documentos
Cultura Documentos
This could intensively help some users/DBA's to monitor the usage of his custom tables(Usage here means the
Updates and Inserts against these tables) and to get a primary estimation on the most and least accessed table on
his application and consequently can enhance his maintenance plan.
Solution
Here is a PLSQL code shows how to get a list of all the tables in a certain schema sorted descendingly by the last
change timestamp.
You can replace SCOTT and HR from the code by your schema or list of schemas you want to get the last
changes timestamps from these schema/schemas tables.
Note: The following code has not been created to be run against the default database dictionary user SYS.
Running the code against SYS user throw the following error:
The following inserts and updates on HR and Scott aims to make some changes against some tables on these
schemas because if we ran the code directly we might get all these schema tables with the same timestamp as all
of them has been created with the same timestamp when the database has first been created.
conn scott/tiger
update dept set loc='Cairo' where deptno=40;
commit;
update emp set deptno=30 where empno=7934;
commit;
conn hr/hr
update employees set email='tgates@hotmail' where employee_id=190;
commit;
update jobs set max_salary=10000 where job_id='ST_MAN';
commit;
Here is the actual code to be run against your schema or list of schemas:
conn / as sysdba
set linesize 10000
set serveroutput on
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE table_xxx';
END;
/
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE table_xxx(table_name VARCHAR2(30), owner VARCHAR2(30), date
col TIMESTAMP)';
END;
/
DECLARE
BEGIN
OPEN cur1;
LOOP
Fetch cur1 INTO v_table_name, v_owner;
v_exec1:='SELECT NVL(MAX(scn_to_timestamp(ora_rowscn)),SYSDATE-365) FROM ' ||
v_owner||'.'||v_table_name;
EXECUTE IMMEDIATE v_exec1 INTO v_datecol ;
v_exec := 'INSERT INTO table_xxx VALUES( :b2,:b3,:b4)';
EXECUTE IMMEDIATE v_exec USING v_table_name ,v_owner ,v_datecol;
EXIT WHEN cur1%NOTFOUND;
END loop;
CLOSE cur1;
COMMIT;
DBMS_OUTPUT.PUT_LINE('==================================================================');
DBMS_OUTPUT.PUT_LINE('Table Name Owner TimeStamp');
DBMS_OUTPUT.PUT_LINE('==================================================================');
LOOP
v_spa:=' ';
v_spa1:=' ';
Fetch cur2 INTO v_table_name, v_owner, v_datecol;
SELECT 30-TO_NUMBER(LTRIM(RTRIM(LENGTH(v_table_name))))
INTO v_len
FROM DUAL;
FOR i in 1..v_len LOOP
v_spa:=v_spa||' ';
END LOOP;
SELECT 15-TO_NUMBER(LTRIM(RTRIM(LENGTH(v_owner))))
INTO v_len
FROM DUAL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('==================================================================');
CLOSE cur2;
END;
/
.
.
The output should be like that:
==================================================================
Table Name Owner TimeStamp
==================================================================
JOBS HR 24/10/2008 19:20:10
EMPLOYEES HR 24/10/2008 19:18:16
EMP SCOTT 24/10/2008 19:06:43
DEPT SCOTT 24/10/2008 19:03:49
DEPARTMENTS HR 24/10/2008 18:50:01
JOB_HISTORY HR 24/10/2008 18:50:01
LOCATIONS HR 24/10/2008 18:50:01
REGIONS HR 24/10/2008 18:50:01
BONUS SCOTT 27/10/2007 18:54:18
SALGRADE SCOTT 30/08/2005 15:05:26
==================================================================
SQL>