select only one record

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

select only one record

Post by julian_lp »

As the subject says, ... what is the best way to retrieve only one record?

I mean, let's suppose that I need to get certain record given its ID, so I *know* that there will be only one.
I'm using LIMIT 1, but I wanted to know if there is some other advice that should be taken into account...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if there is only one feasable record you will get only one record.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post by julian_lp »

volka wrote:if there is only one feasable record you will get only one record.
Well yes, of course. I'm looking for optimizing all my queries...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For optimizing queries the general rules are
  • avoid unnecessary column selection
  • make joins as restrictive as possible to weed out stuff you don't want as early as possible
  • correctly use indexes
  • correctly select storage engines
  • correctly choose column types
  • use EXPLAIN to examine how the database understands and implements your queries
  • read the documentation of your database thoroughly. They often have sections on query optimization. They aren't written to suck up space; read them.
Post Reply