Main Page | Report this Page
Computers Forum Index  »  Computer - Databases - Oracle (Misc)  »  Is this the sanctioned way to ascertain a table's...
Page 2 of 2    Goto page Previous  1, 2

Is this the sanctioned way to ascertain a table's...

Author Message
Noons...
Posted: Sun Oct 18, 2009 5:15 am
Guest
Maxim Demenko wrote,on my timestamp of 17/10/2009 11:24 PM:

Quote:

I got the impression, he *is* checking metadata (more precise -
user_tables).


He is counting the rows of a table to check its existence, according to his own
words:

Quote:
Sometimes my code needs to verify whether a table exists. Years ago, I
was about to post that particular questions here. I then realized
that the following statements achieve the desired results:

EXEC SQL SELECT COUNT(1) INTO :howMany FROM user_tables WHERE

I can't possibly understand how you got that impression...
 
Mark D Powell...
Posted: Mon Oct 19, 2009 12:29 am
Guest
On Oct 18, 5:49 pm, Serge Rielau <srie... at (no spam) ca.ibm.com> wrote:
Quote:
Mark D Powell wrote:
There is an American expression that comes to mind in regards to not
using a select count(*) to verify existance just because some other
developer might copy the code at some future date: you can't fix
stupid.

I didn't want to use teh S-word.. But even in the case of know (even
modeled!) uniqueness COUNT will be slower that limiting the number of rows.
Let's first assume the DBMS is operating straight forward without any
fancy optimizations.
The attempt to read the second row costs CPU. There are various tricks
one can use to peek ahead and cut that codepath, but it's still CPU.
Also teh COUNT requires group by processing (also not free). So there is
a runtime cost to be paid for asking teh DBSM a question that has only a
loose correlation (# of tables) to the business need (existance of at
least one table).
Now let's assume that the DBMS is clever. It realizes that USER_TABLES
(in Oracle) implies a specific schemaname and tablename + schemaname are
unique. and it therefor can throw out the COUNT and replace it with
something niftier.
That at least costs extra compile time in the optimizer - aside from
making the code somewhat dependent on the version of the DBMS that is at
least that smart.
A simple tule holds: Ask the DBMS what you want to know. Don't ask for
more information than you need.

I do not think I would let what someone might do stop me from doing
what I need to do.  What I might do if Serge's concern came to mind is
code a database stored function that perrforms the table exists check
and returns 0 or 1 to indicate existance.

How about simply assuming the table exists and handle the non existence
as a PL/SQL exception.
Note that unless the SQL in question is dynamic the whole PL/SQL block
using the non existing table is likely in an invalid state to begin with.
Either way the compiler will holler if the table doesn't exist and that
check will be executed by the DBMS anyway.

Cheers
Serge

--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab

Serge, I do not understand yoru point. In this case the OP is reading
the rdbms dictionary view user_tables which has one and only one row
for every table the user owns to see if the table exists for the
user. The result will always be zero or one and it is an efficient
query.

The OP has stated why he wants to use the query. I would rather the
table always exist, but for what the OP wants to do this is a good way
to test existance.

IMHO -- Mark D Powell --
 
Serge Rielau...
Posted: Mon Oct 19, 2009 1:49 am
Guest
Mark D Powell wrote:
Quote:
There is an American expression that comes to mind in regards to not
using a select count(*) to verify existance just because some other
developer might copy the code at some future date: you can't fix
stupid.
I didn't want to use teh S-word.. But even in the case of know (even

modeled!) uniqueness COUNT will be slower that limiting the number of rows.
Let's first assume the DBMS is operating straight forward without any
fancy optimizations.
The attempt to read the second row costs CPU. There are various tricks
one can use to peek ahead and cut that codepath, but it's still CPU.
Also teh COUNT requires group by processing (also not free). So there is
a runtime cost to be paid for asking teh DBSM a question that has only a
loose correlation (# of tables) to the business need (existance of at
least one table).
Now let's assume that the DBMS is clever. It realizes that USER_TABLES
(in Oracle) implies a specific schemaname and tablename + schemaname are
unique. and it therefor can throw out the COUNT and replace it with
something niftier.
That at least costs extra compile time in the optimizer - aside from
making the code somewhat dependent on the version of the DBMS that is at
least that smart.
A simple tule holds: Ask the DBMS what you want to know. Don't ask for
more information than you need.

Quote:
I do not think I would let what someone might do stop me from doing
what I need to do. What I might do if Serge's concern came to mind is
code a database stored function that perrforms the table exists check
and returns 0 or 1 to indicate existance.
How about simply assuming the table exists and handle the non existence

as a PL/SQL exception.
Note that unless the SQL in question is dynamic the whole PL/SQL block
using the non existing table is likely in an invalid state to begin with.
Either way the compiler will holler if the table doesn't exist and that
check will be executed by the DBMS anyway.

Cheers
Serge

--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab
 
Palooka...
Posted: Mon Oct 19, 2009 4:58 am
Guest
On 19/10/09 01:29, Mark D Powell wrote:
Quote:
On Oct 18, 5:49 pm, Serge Rielau<srie... at (no spam) ca.ibm.com> wrote:
Mark D Powell wrote:
There is an American expression that comes to mind in regards to not
using a select count(*) to verify existance just because some other
developer might copy the code at some future date: you can't fix
stupid.

I didn't want to use teh S-word.. But even in the case of know (even
modeled!) uniqueness COUNT will be slower that limiting the number of rows.
Let's first assume the DBMS is operating straight forward without any
fancy optimizations.
The attempt to read the second row costs CPU. There are various tricks
one can use to peek ahead and cut that codepath, but it's still CPU.
Also teh COUNT requires group by processing (also not free). So there is
a runtime cost to be paid for asking teh DBSM a question that has only a
loose correlation (# of tables) to the business need (existance of at
least one table).
Now let's assume that the DBMS is clever. It realizes that USER_TABLES
(in Oracle) implies a specific schemaname and tablename + schemaname are
unique. and it therefor can throw out the COUNT and replace it with
something niftier.
That at least costs extra compile time in the optimizer - aside from
making the code somewhat dependent on the version of the DBMS that is at
least that smart.
A simple tule holds: Ask the DBMS what you want to know. Don't ask for
more information than you need.

I do not think I would let what someone might do stop me from doing
what I need to do. What I might do if Serge's concern came to mind is
code a database stored function that perrforms the table exists check
and returns 0 or 1 to indicate existance.

How about simply assuming the table exists and handle the non existence
as a PL/SQL exception.
Note that unless the SQL in question is dynamic the whole PL/SQL block
using the non existing table is likely in an invalid state to begin with.
Either way the compiler will holler if the table doesn't exist and that
check will be executed by the DBMS anyway.

Cheers
Serge

--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab

Serge, I do not understand yoru point. In this case the OP is reading
the rdbms dictionary view user_tables which has one and only one row
for every table the user owns to see if the table exists for the
user. The result will always be zero or one and it is an efficient
query.

The OP has stated why he wants to use the query. I would rather the
table always exist, but for what the OP wants to do this is a good way
to test existance.

Assuming that your PL/SQL has half decent error handling, Serge's way is

much better IMHO. Anyway, as he he says, if the table does not exist/is
not visible, the block will not compile.

Palooka
 
Maxim Demenko...
Posted: Mon Oct 19, 2009 8:51 am
Guest
On 18 Okt., 07:21, Noons <wizofo... at (no spam) yahoo.com.au> wrote:
Quote:
Maxim Demenko wrote,on my timestamp of 17/10/2009 11:24 PM:



I got the impression, he *is* checking metadata (more precise -
user_tables).

He is counting the rows of a table to check its existence, according to his own
words:


yes, he is counting the rows of one very specific table -
*user_tables* .
But i think, it is irrelevant as long as we agree on the subject - one
parse call should be more efficient than query the data dictionary.


Quote:
 >>> EXEC SQL SELECT COUNT(1) INTO :howMany FROM user_tables WHERE

I can't possibly understand how you got that impression...


Best regards

Maxim
 
Robert Klemme...
Posted: Mon Oct 19, 2009 9:56 am
Guest
On 10/19/2009 02:58 AM, Palooka wrote:
Quote:
On 19/10/09 01:29, Mark D Powell wrote:
On Oct 18, 5:49 pm, Serge Rielau<srie... at (no spam) ca.ibm.com> wrote:
Mark D Powell wrote:
There is an American expression that comes to mind in regards to not
using a select count(*) to verify existance just because some other
developer might copy the code at some future date: you can't fix
stupid.

I didn't want to use teh S-word.. But even in the case of know (even
modeled!) uniqueness COUNT will be slower that limiting the number of
rows.
Let's first assume the DBMS is operating straight forward without any
fancy optimizations.
The attempt to read the second row costs CPU. There are various tricks
one can use to peek ahead and cut that codepath, but it's still CPU.
Also teh COUNT requires group by processing (also not free). So there is
a runtime cost to be paid for asking teh DBSM a question that has only a
loose correlation (# of tables) to the business need (existance of at
least one table).
Now let's assume that the DBMS is clever. It realizes that USER_TABLES
(in Oracle) implies a specific schemaname and tablename + schemaname are
unique. and it therefor can throw out the COUNT and replace it with
something niftier.
That at least costs extra compile time in the optimizer - aside from
making the code somewhat dependent on the version of the DBMS that is at
least that smart.
A simple tule holds: Ask the DBMS what you want to know. Don't ask for
more information than you need.

I do not think I would let what someone might do stop me from doing
what I need to do. What I might do if Serge's concern came to mind is
code a database stored function that perrforms the table exists check
and returns 0 or 1 to indicate existance.

How about simply assuming the table exists and handle the non existence
as a PL/SQL exception.
Note that unless the SQL in question is dynamic the whole PL/SQL block
using the non existing table is likely in an invalid state to begin
with.
Either way the compiler will holler if the table doesn't exist and that
check will be executed by the DBMS anyway.

Cheers
Serge

--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab

Serge, I do not understand yoru point. In this case the OP is reading
the rdbms dictionary view user_tables which has one and only one row
for every table the user owns to see if the table exists for the
user. The result will always be zero or one and it is an efficient
query.

The OP has stated why he wants to use the query. I would rather the
table always exist, but for what the OP wants to do this is a good way
to test existance.

Assuming that your PL/SQL has half decent error handling, Serge's way is
much better IMHO. Anyway, as he he says, if the table does not exist/is
not visible, the block will not compile.

I do not agree. Here's why: assuming you have an application that
automatically deals with schema migration. If you follow the approach
"use it and handle the case that the table is not there" requires to mix
different types of code (schema treatment with core logic). IMHO that
is a bad idea. For applications like this you want to do the existence
check upfront (e.g. during application start) and not when the
application is running. I agree to Mark: there are completely valid
reasons to check for existence of database objects independent of usage
and for that querying the data dictionary is the proper way to go. (If
you are using JDBC the driver will typically do that for you anyway when
using the meta data interface).

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Serge Rielau...
Posted: Mon Oct 19, 2009 5:02 pm
Guest
Well, I have voiced my opinion. People may agree or not. That's fine.
It's what I teach at any rate.

Cheers
Serge

--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab
 
Noons...
Posted: Mon Oct 19, 2009 5:09 pm
Guest
Maxim Demenko wrote,on my timestamp of 19/10/2009 6:51 PM:

Quote:

yes, he is counting the rows of one very specific table -
*user_tables* .
But i think, it is irrelevant as long as we agree on the subject - one
parse call should be more efficient than query the data dictionary.

Yes, absolutely. My apologies, I misread the OP's question.
 
joel garry...
Posted: Mon Oct 19, 2009 9:04 pm
Guest
On Oct 19, 6:09 am, Noons <wizofo... at (no spam) yahoo.com.au> wrote:
Quote:
Maxim Demenko wrote,on my timestamp of 19/10/2009 6:51 PM:



yes, he is counting the rows of one very specific table -
*user_tables* .
But i think, it is irrelevant as long as we agree on the subject - one
parse call should be more efficient than query the data dictionary.

Yes, absolutely.  My apologies, I misread the OP's question.

Actually, you goofed up there, but OP also said:
Quote:
Internally, the command 'nrec' actually is a simple:

SELECT COUNT(1) from :someTable;

Which could potentially be disastrous.

There could perhaps be a discussion about context switching between
PL and SQL relevant to other parts of the thread
http://www.google.com/search?btnG=1&pws=0&q=oracle+pl%2Fsql+context+switching
..

jg
--
at (no spam) home.com is bogus.
http://www3.signonsandiego.com/stories/2009/oct/18/friends-believe-chp-officer-did-all-he-could-avoid/?metro&zIndex=184764
 
Noons...
Posted: Tue Oct 20, 2009 3:17 pm
Guest
Palooka wrote,on my timestamp of 19/10/2009 10:58 AM:
Quote:

Assuming that your PL/SQL has half decent error handling, Serge's way is
much better IMHO. Anyway, as he he says, if the table does not exist/is
not visible, the block will not compile.

And the "is not visible" bit is the important point there, IMHO.
Using the view USER_TABLES presumes the table(s) to be checked are created by
the user firing off the SQL. In this day and age of dbs with multiple schemas
and cross-schema access (consolidation et all) it's much better to check on
visibility: the table might be there and accessible, just not owned by the
schema of the same name as the user. That means using ALL_TABLES, not
USER_TABLES, at the very least. And what happens then if it is a view, not a table?
I still think Serge's (and Maxim's) approach of checking via parsing is the
correct one: it covers everything. Either the object (be it table or view) is
accessible or not, doesn't matter from where and in what conditions.
 
 
Page 2 of 2    Goto page Previous  1, 2
All times are GMT
The time now is Thu Nov 26, 2009 9:31 am