MySQL persistent connections and advantages of mysql_pconnect?

时间秒杀一切 提交于 2019-12-01 01:14:22

问题


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()?


回答1:


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.




回答2:


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().



来源:https://stackoverflow.com/questions/1128329/mysql-persistent-connections-and-advantages-of-mysql-pconnect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!