• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Relative query efficiencies

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably the COUNT will be faster but not by any really measurable amount.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic