Re: MySQL close connection, what's the purpose?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/3/06, tedd <tedd@xxxxxxxxxxxx> wrote:
> chris said:
>
> >Time. Opening a db connection is time consuming. There are many levels
> >involved (making the connection, authentication, etc).. Even worse if
> >the connection is over tcp/ip because that overhead comes in on top as
> >well.
>
> I replied:
>
> >Have you timed it?
> >
> >Maybe I'll do that tomorrow.
>
> Okay, I performed a test that made 100 queries, and found that it
> took about four times as long to open a dB connection each time and
> get the data as it did to open it once. In other words, if your
> script pulls data from a dB 100 times, then making a connection to
> the dB once, pulling the data, and then closing the dB is four times
> as fast as opening the dB, pulling the data, and closing the dB in
> sequence one hundred times.
>
> Now, that sounds like you should open your dB at the start of your
> script and just leave it open until the your script quits (as the
> original poster suggested), but there's a few thing to consider here.
>
> 1. The total time difference shown in the above test is a little over
> a tenth of a second.
>
> 2. By keeping the dB open, you're actually tying up the resources of
> the server more than necessary. After all, your script will always
> run longer than the query, right?
>
> 3. Most scripts (I'm guess here) don't access the dB 100 times or
> more -- so doing is less, means less of a difference.
>
> 4. And lastly, by closing the dB in your script when you're done with
> it, allows the server to use it's resources to "clean-up" quicker and
> have more time to be responsive to other queries.
>
> So, not that you implied otherwise, I still don't see any convincing
> argument as to why one should open a dB at the start of a script and
> not close it as the original poster asked. Furthermore, not that I'm
> anyone special, but if I was overlooking someone's code who did that,
> I wouldn't think favorably of the technique -- it would appear as
> sloppy programming to me.

Just out of interest, could you re-run the test using persistent connections?

change mysql_connect to mysql_pconnect..

--
Postgresql & php tutorials
http://www.designmagick.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux