Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL persistent connections and advantages of mysql_pconnect?

I had never heard of persistent connections before, and I don't understand the advantages.

I run a PHP/MySQL based website, it receives tens of thousands of page views per day. In my header file on each of these pages I have just used mysql_connect() and I haven't bothered with terminating the connection in the footer file.

In my case are there any advantages of using mysql_pconnect()?

like image 896
Juddling Avatar asked Jul 14 '09 22:07

Juddling


2 Answers

Using a persistent connection leaves the connection open after the script has finished executing. Opening and closing connections over and over causes overhead, while small, that will eventually mount up as the number of requests go up.

However, if you read the manual page for mysql_pconnect it states:

  • If PHP and MySQL are on the same server or local network, the connection time may be negligible, in which case there is no advantage to persistent connections.

If this is the case it may not be worth the trouble changing your code.

You can find more detailed information on persistent connections at the same site as above.

like image 155
Etzeitet Avatar answered Oct 18 '22 10:10

Etzeitet


Check out this URL:

http://us3.php.net/manual/en/function.mysql-pconnect.php

Basically mysql_pconnect() tries to find a persistent connection already open with the credentials that you've specified. If it doesn't find one it makes a new one. It also doesn't close the connection after a statement is executed

So really in your case you may not notice a difference but in reality you should probably be using mysql_pconnect().

like image 36
Wade Avatar answered Oct 18 '22 09:10

Wade