<p>I'm building a PHP page with data sent from MySQL.</p>

<p>Is it better to have</p>

<ul>
<li>1 <code>SELECT</code> query with 4 table joins, or</li>
<li>4 small <code>SELECT</code> queries with no table join; I do select from an ID</li>
</ul>

<p>Which is faster and what is the pro/con of each method? I only need one row from each tables.</p>

[{'answer_id': 55471, 'author': 'Robby Slaughter', 'author_id': 1854, 'author_profile': 'https://Stackoverflow.com/users/1854', 'pm_score': 3, 'selected': False, 'text': "<p>Generally, it's better to have one SELECT statement. One of the main reasons to have databases is that they are fast at processing information, particularly if it is in the format of query.</p>\n\n<p>If there is any drawback to this approach, it's that there are some kinds of analysis that you can't do with one big SELECT statement. RDBMS purists will insist that this is a database design problem, in which case you are back to my original suggestion.</p>\n"}, {'answer_id': 55472, 'author': 'Eric Hogue', 'author_id': 4137, 'author_profile': 'https://Stackoverflow.com/users/4137', 'pm_score': 2, 'selected': False, 'text': '<p>I would say 1 query with the join. This way you need to hit the server only once. And if your tables are joined with indexes, it should be fast.</p>\n'}, {'answer_id': 55475, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 5, 'selected': True, 'text': "<p>You should run a profiling tool if you're truly worried cause it depends on many things and it can vary but as a rule its better to have fewer queries being compiled and fewer round trips to the database.</p>\n\n<p>Make sure you filter things as well as you can using your where and join on clauses.</p>\n\n<p>But honestly, it usually doesn't matter since you're probably not going to be hit all that hard compared to what the database can do, so unless optimization is your spec you should not do it prematurely and do whats simplest.</p>\n"}, {'answer_id': 55543, 'author': 'Josh Hinman', 'author_id': 2527, 'author_profile': 'https://Stackoverflow.com/users/2527', 'pm_score': 3, 'selected': False, 'text': "<p>When you use JOINs instead of multiple queries, you allow the database to apply its optimizations. You also are potentially retrieving rows that you don't need (if you were to replace an INNER join with multiple selects), which increases the network traffic between your app server and database server. Even if they're on the same box, this matters.</p>\n"}, {'answer_id': 55549, 'author': 'Eugene', 'author_id': 1155, 'author_profile': 'https://Stackoverflow.com/users/1155', 'pm_score': 2, 'selected': False, 'text': '<p>It might depend on what you do with the data after you fetch it from the DB. If you use each of the four results independently, then it would be more logical and clear to have four separate SELECT statements. On the other hand, if you use all the data together, like to create a unified row in a table or something, then I would go with the single SELECT and JOINs.</p>\n\n<p>I\'ve done a bit of PHP/MySQL work, and I find that even for queries on huge tables with tons of JOINs, the database is pretty good at optimizing - if you have smart indexes. So if you are serious about performance, start reading up on <a href="http://www.informit.com/articles/article.aspx?p=377652" rel="nofollow noreferrer">query optimization and indexing</a>.</p>\n'}, {'answer_id': 55558, 'author': 'blinderzoff', 'author_id': 5683, 'author_profile': 'https://Stackoverflow.com/users/5683', 'pm_score': 2, 'selected': False, 'text': "<p>Well under Oracle you'd want to take advantage of the query caching, and if you have a lot of small queries you are doing in your sequential processing, it would suck if the last query pushed the first one out of the cache...just in time for you to loop around and run that first query again (with different parameter values obviously) on the next pass.</p>\n\n<p>We were building an XML output file using Java stored procedures and definitely found the round trip times for each individual query were eating us alive. We found it was much faster to get all the data in as few queries as possible, then plug those values into the XML DOM as needed.</p>\n\n<p>The only downside is that the Java code was a bit less elegant, as the data fetch was now remote from its usage. But we had to generate a large complex XML file in as close to zero time as possible, so we had to optimize for speed.</p>\n"}, {'answer_id': 60214, 'author': 'Robert Swisher', 'author_id': 1852, 'author_profile': 'https://Stackoverflow.com/users/1852', 'pm_score': 1, 'selected': False, 'text': '<p>Be careful when dealing with a merge table however. It has been my experience that although a single join can be good in most situations, when merge tables are involved you can run into strange situations.</p>\n'}]
['https://Stackoverflow.com/questions/55463', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5196/']