Jump to content

[SOLVED] when is 1 long query slower than 2 short ones?


pnj

Recommended Posts

My question is about efficiency.  I have a table with dates, and a foreign key.  Given a specific date, I want to pull the id and date for the records before and after that date with the matching foreign key.

 

Approach 1 is to pull all records with that foreign key, and then find the relevant date in php.  There will be between 5 and 80 rows returned.

 

select pkey, v_date from v where foreign_key=72 order by v_date;

 

Approach 2 is to run two queries, one to get the entry before, one to get the entry after:

select pkey, v_date from v where foreign_key=72 and v_date<"2006-04-27" order by v_date limit 1;
select pkey, v_date from v where foreign_key=72 and v_date>"2006-04-27" order by v_date desc limit 1;

 

Am I right in thinking that the second approach is far more efficient, or is the overhead of the call to mysql so high that its better to get dozens of results, but avoid calling the extra query?

 

Thanks

 

-pnj

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.