<p>A discussion about Singletons in <strong>PHP</strong> has me thinking about this issue more and more. Most people instruct that you shouldn't make a bunch of DB connections in one request, and I'm just curious as to what your reasoning is. My first thought is the expense to your script of making that many requests to the DB, but then I counter myself with the question: wouldn't multiple connections make concurrent querying more efficient?</p>

<p>How about some answers (with evidence, folks) from some people in the know?</p>

[{'answer_id': 28597, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 2, 'selected': False, 'text': "<p>It is the cost of setting up the connection, transferring the data and then tearing it down.  It will eat up your performance.</p>\n\n<p>Evidence is harder to come by but consider the following...</p>\n\n<p>Let's say it takes x microseconds to make a connection. </p>\n\n<p>Now you want to make several requests and get data back and forth.  Let's say that the difference in transport time is negligable between one connection and many (just ofr the sake of argument).</p>\n\n<p>Now let's say it takes y microseconds to close the connection.</p>\n\n<p>Opening one connection will take x+y microseconds of overhead.  Opening many will take n * (x+y).  That will delay your execution.</p>\n"}, {'answer_id': 28602, 'author': 'mmattax', 'author_id': 1638, 'author_profile': 'https://Stackoverflow.com/users/1638', 'pm_score': 0, 'selected': False, 'text': "<p>I would assume that it is because your requests are not being sent asynchronously, since your requests are done iteratively on the server, blocking each time, you have to pay for the overhead of creating a connection each time, when you only have to do it once...</p>\n\n<p>In Flex, all web service calls are automatically called asynchronously, so you it is common to see multiple connections, or queued up requests on the same connection.</p>\n\n<p>Asynchronous requests mitigate the connection cost through faster request / response time...because you cannot <em>easily</em> achieve this in PHP without out some threading, then the performance hit is greater then simply reusing the same connection. </p>\n\n<p>that's my 2 cents...</p>\n"}, {'answer_id': 28608, 'author': 'Vincent', 'author_id': 1508, 'author_profile': 'https://Stackoverflow.com/users/1508', 'pm_score': 2, 'selected': False, 'text': "<p>Setting up a DB connection is usually quite heavy. A lot of things are going on backstage <strong>(DNS resolution/TCP connection/Handshake/Authentication/Actual Query)</strong>.</p>\n\n<p>I've had an issue once with some weird DNS configuration that made every TCP connection took a few seconds before going up. My login procedure (because of a complex architecture) took 3 different DB connections to complete. With that issue, it was taking forever to log-in. We then refactored the code to make it go through one connection only.</p>\n"}, {'answer_id': 28612, 'author': 'hometoast', 'author_id': 2009, 'author_profile': 'https://Stackoverflow.com/users/2009', 'pm_score': 1, 'selected': False, 'text': "<p>We access Informix from .NET and use multiple connections.  Unless we're starting a transaction on each connection, it often is handled in the connection pool.  I know that's very brand-specific, but most(?) database systems' cilent access will pool connections to the best of its ability.</p>\n\n<p>As an aside, we did have a problem with connection count because of cross-database connections.  Informix supports synonyms, so we synonymed the common offenders and the multiple connections were handled server-side, saving a lot in transfer time, connection creation overhead, and (the real crux of our situtation) license fees.</p>\n"}, {'answer_id': 28614, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 4, 'selected': True, 'text': "<p>Database connections are a limited resource.  Some DBs have a very low connection limit, and wasting connections is a major problem.  By consuming many connections, you may be blocking others for using the database.</p>\n\n<p>Additionally, throwing a ton of extra connections at the DB doesn't help anything unless there are resources on the DB server sitting idle.  If you've got 8 cores and only one is being used to satisfy a query, then sure, making another connection might help.  More likely, though, you are already using all the available cores.  You're also likely hitting the same harddrive for every DB request, and adding additional lock contention.</p>\n\n<p>If your DB has anything resembling high utilization, adding extra connections won't help.  That'd be like spawning extra threads in an application with the blind hope that the extra concurrency will make processing faster.  It <em>might</em> in some certain circumstances, but in other cases it'll just slow you down as you thrash the hard drive, waste time task-switching, and introduce synchronization overhead.</p>\n"}]
['https://Stackoverflow.com/questions/28590', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1344/']