aspose file tools
The moose likes JDBC and the fly likes Relative query efficiencies Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Relative query efficiencies" Watch "Relative query efficiencies" New topic
Author

Relative query efficiencies

Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Due to Access' lack of case sensitivity on the primary key, I need to check if a record exists before I insert a new record. Is it faster to return a count, or to return actual data from a table? I.E. which of the following queries will be faster?

1) SELECT COUNT(*) FROM table WHERE id = 'Test';

or

2) SELECT id FROM table WHERE id = 'Test';

I don't really need the info returned - I'll just check to see if ResultSet.next is true.

Thanks,


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Probably the COUNT will be faster but not by any really measurable amount.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The only way you'll know for sure is to try both cases and see.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Originally posted by Paul Sturrock:
The only way you'll know for sure is to try both cases and see.



This is why I said "not by any really measurable amount". But COUNT should be faster and I believe is better practice in general.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
How common is it that your id value is in use? If it's rare, I would
blindly insert, then deal with the possible collision.


There is no emoticon for what I am feeling!
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Jeff,

Unfortunately, I can't rely on a collision using my key. The key is a data stamp generated from a machine on our factory floor that uses both upper and lowercase letters. Each date code is unique, but Access thinks "dqKez" is the same as "dqKeZ" as far as unique primary keys are concerned (it's not case sensitive for key comparison).

Access IS case sensitive on queries, so the work-around is check if it exists first, and add only if it does not. If the search returns the key, then I handle the exception case.

Cheers,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Relative query efficiencies
 
Similar Threads
Accessing data in ResultSet object
Select an entity object in a NamedQuery
how to get num of rows in ResultSet
Doubt in JDBC
Getting one Row from JOIN query