| A better way to check every table for data? [message #569588] |
Tue, 30 October 2012 01:55  |
 |
ocdeveloper
Messages: 9 Registered: November 2010
|
Junior Member |
|
|
I'm working on a feature that needs to check every table for data. At the moment I am roundtripping every time and running the query like this:
SELECT 1 FROM dual WHERE EXISTS(SELECT 'x' FROM table_name)
Is there a better way that allows me to get the result for all tables in one trip? I've tried a correlated subquery like this:
SELECT table_name, (SELECT 1 FROM dual WHERE EXISTS(SELECT 'x' FROM table_name) FROM all_tables
but I think that doesn't work because Oracle looks for a table called table_name rather than the value from the outer query.
Thanks,
Neil
|
|
|
|
|
|
|
|
|
|
|
|
| Re: A better way to check every table for data? [message #569598 is a reply to message #569597] |
Tue, 30 October 2012 03:40   |
 |
Michel Cadot
Messages: 54236 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
IOT_OVERFLOW is not a real table but an overflow one and so you can't query it (Oracle uses it internally when you access columns of an IOT that are not in the main segment, see index_org_table_clause on CREATE TABLE).
The other test excludes some "virtual" tables like external tables, nested tables or temporary tables.
Actually the tests are not complete, the following one is closer (maybe some cases are missing, feel free to complete it):
where ( tablespace_name is not null or partitioned='YES' or nvl(iot_type,'NOT_IOT')='IOT' )
and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING')
Regards
Michel
[Updated on: Tue, 30 October 2012 03:41] Report message to a moderator
|
|
|
|
|
|
|
|
| Re: A better way to check every table for data? [message #569623 is a reply to message #569619] |
Tue, 30 October 2012 07:15   |
 |
Michel Cadot
Messages: 54236 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
a) recycle bin objects are excluded from user_tables by its definition ("bitand(o.flags, 128) = 0")
b) I won't but will add it below
c) good point, forgot them
So:
where -- a real table
( tablespace_name is not null or partitioned='YES' or nvl(iot_type,'NOT_IOT')='IOT' )
-- not an iot overflow
and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING')
-- not a mview container
and (user, table_name) not in (select owner, container_name from user_mviews)
-- not a mview log
and (user, table_name) not in (select log_owner, log_table from user_mview_logs)
There are also RUPD$_% tables created with mview log when there is a primary key on the table but they are not referenced in any user accessible dictionary view, so maybe one might want to exclude them based on the name ("table_name not like 'RUPD$\_%' escape '\'").
Regards
Michel
|
|
|
|
|
|
|
|
| Re: A better way to check every table for data? [message #569707 is a reply to message #569700] |
Wed, 31 October 2012 08:41   |
Solomon Yakobson
Messages: 1404 Registered: January 2010
|
Senior Member |
|
|
cookiemonster wrote on Wed, 31 October 2012 08:10It does, but that's once per table in this case, not an issue, and there's no better way to do it.
Well, there is no need for subquery:
select table_name,
extractvalue(
dbms_xmlgen.getXMLtype(
'SELECT ''Y'' val FROM "'|| table_name || '" WHERE ROWNUM = 1'
),
'/ROWSET/ROW/VAL'
) table_has_data
from user_tables
where ( tablespace_name is not null or partitioned='YES' )
and nvl(iot_type,'NOT_IOT') != 'IOT_OVERFLOW'
order by 1
/
SY.
|
|
|
|
|
|
| Re: A better way to check every table for data? [message #569731 is a reply to message #569719] |
Wed, 31 October 2012 13:20  |
 |
Michel Cadot
Messages: 54236 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
I just use OP's query to show him how to do it, no discussion on this part was intended (note that I just coy and paste his query, I (almost always) use only lower case in my queries).
Anyway, you use one query or the other one the cost is the same, it is just a matter of how you read the query. I had this discussion with T. Kyte on AskTom. I prefer the syntax that shows that I search if something exists (and don't like the rownum here), he prefers Solomon's syntax as it directly shows where you search and don't see any reason to query dual to get information on something else. Just a matter of thinking.
Regards
Michel
[Updated on: Wed, 31 October 2012 13:52] Report message to a moderator
|
|
|
|