Page 1 of 1

select only one record

Posted: Wed Aug 23, 2006 3:40 pm
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...

Posted: Wed Aug 23, 2006 3:42 pm
by volka
if there is only one feasable record you will get only one record.

Posted: Wed Aug 23, 2006 4:10 pm
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...

Posted: Wed Aug 23, 2006 6:39 pm
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.