Jump to content

Which is faster for three tables, mysql or a custom php function


brown2005

Recommended Posts

Generally you'll find that doing it with a single query with JOINs is faster because it's less round-trips to the database. Each mySQL query has an overhead cost (a fixed time latency between the PHP and mySQL server, say 0.01 second), so doing 3 queries takes 3*0.01 + mySQL processing time (say, 0.0001 second). = 0.0301 seconds.

 

Doing 1 query will take longer to do the processing, but saves you massive amounts on the overhead costs:

1*0.01 + longer mySQL processing time (say, 0.0005 seconds) = 0.0105 seconds.

 

I've made up the numbers here to illustrate my point, but unless you're running a very complicated query, the processing time will be less than the overhead of making a query. The numbers here also ignore the extra processing you'd have to do in PHP in the first case.

 

There are probably times that doing stuff in PHP could be faster, but they're so uncommon it's not worth worrying about. Always try to do the minimum amount of queries.

Link to comment
Share on other sites

Depends if you're utilizing some sort of result set cache in the PHP functions. If not, as others have mentioned, it'd be faster using a join, especially if there's multiple users accessing the site. There'll be 3x less queries.

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.