Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Relative query efficiencies

 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the COUNT will be faster but not by any really measurable amount.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way you'll know for sure is to try both cases and see.
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic