1

I want to fetch table names from dba_tables using join with dba_tab_statistics.

select table_name 
from dba_tables a,
     dba_tab_statistics b 
where a.owner like 'Owner' and a.table_name not like '%TMP'
  and a.LAST_ANALYZED IS NOT NULL and a.table_name = b.table_name
  and b.stattype_locked IS NULL;  

This query doesn't give distinct table names. What changes are required ?

2
  • 1
    Simply do SELECT DISTINCT. Commented Nov 25, 2016 at 8:52
  • 4
    Tip of today: Switch to modern, explicit JOIN syntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed. Commented Nov 25, 2016 at 8:53

2 Answers 2

1

Use either of them:

SELECT a.table_name
  FROM dba_tables a, 
        dba_tab_statistics b
 WHERE     a.owner LIKE 'Owner'
       AND a.table_name NOT LIKE '%TMP'
       AND a.LAST_ANALYZED IS NOT NULL
       AND a.table_name = b.table_name
       AND b.stattype_locked IS NULL
 group by a.table_name ;

or

SELECT DISTINCT a.table_name
  FROM dba_tables a, 
        dba_tab_statistics b
 WHERE     a.owner LIKE 'Owner'
       AND a.table_name NOT LIKE '%TMP'
       AND a.LAST_ANALYZED IS NOT NULL
       AND a.table_name = b.table_name
       AND b.stattype_locked IS NULL ;
Sign up to request clarification or add additional context in comments.

Comments

0

Another solution :

SELECT table_name
  FROM dba_tables a
 WHERE owner LIKE 'Owner'
   AND table_name NOT LIKE '%TMP'
   AND LAST_ANALYZED IS NOT NULL
   AND exists (SELECT 0 
              FROM dba_tab_statistics b
              WHERE a.table_name = b.table_name
              AND b.stattype_locked IS NULL);

2 Comments

Thanks it worked. Only thing is taking more time i.e. 15-20 minutes
select a.table_name from dba_tab_statistics a, dba_tables b where a.owner like 'SCHEMA1' and a.table_name not like '%TMP' and a.table_name not in ('TABLE1', 'TABLE2', 'TABLE3') and a.stattype_locked IS NULL and b.temporary = 'N' group by a.table_name order by max(a.NUM_ROWS) desc;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.