• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why would somebody do that??

 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! All!
I would like to know why some one would query a database as "select 1 from <tablename>"
and then see if the query executed properly.
Regds
Gautham Kasinath
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To see if the connection is alive?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe they want just one record
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1826
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want only 1 record then you should query
select distinct 1 from ...
unless you're certain that the table has only one row (like DUAL table in Oracle).
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the number of rows in a table without worrying abt any column name. (to execute fast)
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1826
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravish Kumar:
To get the number of rows in a table without worrying abt any column name. (to execute fast)

Wouldn't
select count(*) from ...
be a clearer way of doing the same thing? I'd suspect most databases don't actually perform a count for count(*), but rather just check some table metadata that knows the current number of rows.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would somebody do that?
It's about Being Happy....Making Friends.....
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leave it to Matola to take the question seriously...
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Matola:

select count(*) from ...

Ummm....
then why would somebody do that??
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thats exactky what I m wondering.
Finding if the connection is still alive does seem a good possibility though.. but I would rather do a isAlive() on the connection object than execute a query.. wudnt you??
Regds
Lupo
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gautham kasinath:
Well thats exactky what I m wondering.
Finding if the connection is still alive does seem a good possibility though.. but I would rather do a isAlive() on the connection object than execute a query.. wudnt you??
Regds
Lupo

I would love to use isAlive()... to bad there is no such method on a Connection object!
I have found that the only sure way to find out if a Connection is connected is to run a query.
 
swaroop shastri
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how bout !isClosed() ?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to General Computing.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swaroop shastri:
how bout !isClosed() ?

isClosed() only tells you if someone ran the close() method on the Connection object. It does not actually check to see if the Connection is still open to the database.
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at some connection pooling software the other day and part of its configuration allows you to specify a query that the connect pool software will run when it wants to check that a connection is still alive before it hands the connection out to the app.
So I would say doing a query is the main way of checking.
Adam
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still maintain it surely is of no use to be FORCED to execute a query ( without any sense ) just to check if the connection is alive..
It really doesnt make sense to me..
 
Braj Prasad
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This query is executed to ensure a row exists in the table for the select statement being executed. If such a row exists it returns '1', which can be used to evaluate for certain boolean conditions, else the result set returned is empty.
Its perceived to be an efficient way, when performing DB checks, from database load point-of-view.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that maite!!
But can you elaborate on how such a thing is an efficient way?? esp. on DB load point of view??
Regds
Gautham Kasinath
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well maite!! whatsup??
 
Michael Zalewski
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The database must scan the table. The database can stop looking and return a '1' as soon as it finds 'SOMETHING'.

The database must scan the whole table. It cannot stop after it finds the first match, because it must do more work to count all the matching rows.
If no match exists, both forms will scan the entire table. But the first query will return zero rows. (ResultSet.next() returns false). The second query will return a single row with the number '0'.
[ June 11, 2002: Message edited by: Michael Zalewski ]
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Zalewski:

The database must scan the table. The database can stop looking and return a '1' as soon as it finds 'SOMETHING'.

The database must scan the whole table. It cannot stop after it finds the first match, because it must do more work to count all the matching rows.
If no match exists, both forms will scan the entire table. But the first query will return zero rows. (ResultSet.next() returns false). The second query will return a single row with the number '0'.
[ June 11, 2002: Message edited by: Michael Zalewski ]

I think this answer is only partially true.
This will do a complete table scan returning a 1 for every column that matches the criteria( unless it is an indexed column ). It doesn't stop when it finds the first one.
The count function does a different kind of search. From what I understand it uses a binary algorithm of some sort to count the number of matches. This is exponentially more efficient than a regular query.
But I'm sure that it also depends on the database implementation of the count function, and other unique database properties.
Jamie
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do this to get new IDs to use in my table
"SELECT MAX(ID) FROM table_connectors;"
then I add 1 to the value returned
a "SELECT COUNT(*)" will get the NULLs as well but I dont think the "SELECT 1" will get the NULLs.
But since you say the only check on the data is to see if it worked correctly, then we must assume it is a check to see if
A. the table exists
on a database that likely has no other way to check if the table exists. Let us not all assume this person used the best most efficient way to achieve his objective.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic