Jump to content

[SOLVED] Multiple Queries


Fehrant

Recommended Posts

Hello all. I am sort of an amateur to PHP, so some of my questions might come off as rather naive; please bear with me. I am currently working on coding a forum from scratch. I have never done any massive access website, but I am aware one should always try to optimize the code so as to not have the db do much work (among other optimizations). Yet, I have some across some cases where, either because of ignorance or because of not addressing the issue properly, I have to do two different sql queries in one page. Now, is this common? Is it bad?

 

Let me give you something concrete to work with. In a forum, when you make a new thread, not only do you add (INSERT INTO...) a thread entry to the thread table, but you also need to add a post entry to the post table (well, of course this depends on how your db is sorted out). Thus, I would have to make two sql insert statements once the form with all the information is submitted.

 

And on the subject of multiple queries, what is more memory effective? Two sql statements, or one with a JOIN clause?

 

Link to comment
Share on other sites

And on the subject of multiple queries, what is more memory effective? Two sql statements, or one with a JOIN clause?

By combining the query, yes you would lessen the network load by only having a single handshake. Yet the resulting data amount would probably be the same. There's times when you just gotta do multiple access's, be aware. If you need recursive functions then you generally can do an optimised query and then resort with php, but it's always a trade off. At the end of the day use 'microtime' to simply study access rates, there's also some admin (logging) functions in sql for further study.

Link to comment
Share on other sites

And on the subject of multiple queries, what is more memory effective? Two sql statements, or one with a JOIN clause?

 

Well, I agree with rarebit that sometime multiple queries are needed, as in your example above about two insert queries. But when it comes to doing multiple queries vs a single query I would always suggest using the single query.

 

I see a lot of posts on this forum where people do a query for all users in the users tab (for example), and then loop through each record and do another querey for user data that is in a related table. These looping queries are absolute resource hogs and should be avoided. I believe that people that use that approach do so because they do not have a firm grasp of how to work with relational databases.

 

Knowing how to craft a complex query with multiple joins is just as much an art as it is a science. It is something I strugle with many times, but the research I go through to get the "right" query makes me a better programmer.

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.