MySQL Forums
Forum List  »  PHP

Re: Showing recordsets from tables with one (main table) to many (secondary table) relationship
Posted by: Rick James
Date: January 13, 2011 11:37AM

If you are spilling from one Genus to the next, that gets trickier...

Genus >= '$g' AND scientificname > '$s'
OR
Genus > '$g'
ORDER BY Genus, scientificname
LIMIT 1

More optimally...
Genus >= '$g' AND ( scientificname > '$s' OR Genus > '$g' )
ORDER BY Genus, scientificname
LIMIT 1

The second formulation can use an index on Genus; the first cannot.

Probably you should have
INDEX(Kingdom, ..., Family, Genus, scientificname)
Effectively you are doing WHERE = on the first few of these, then switching to >. And that is what the optimizer can do best. My second formulation will get as far as Genus.

Because of things like this, it is terrible to split DATE and TIME instead of using the combined DATETIME.

Let's say it is 09:36, and you split the hours and minutes. To say "after now", you can't say
hr > 09 and mn > 36
But you can say
hr >= 09 and (mn > 36 OR hr > 09)

Options: ReplyQuote


Subject
Written By
Posted
Re: Showing recordsets from tables with one (main table) to many (secondary table) relationship
January 13, 2011 11:37AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.