mysql持久连接和mysql_pconnect的优点?

时间:2023-02-03 03:52:41

I had never heard of persistent connections before, and I don't understand the advantages. I run a PHP/MySQL based internet 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.

我之前从未听说过持久的联系,我不明白这些优点。我运行一个基于PHP / MySQL的互联网网站,每天收到数以万计的页面浏览量。在我的每个页面上的头文件中,我刚刚使用了mysql_connect(),并且我没有在终止页脚文件中的连接时感到烦恼。

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

在我的情况下使用mysql_pconnect()有什么好处?

2 个解决方案

#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:

但是,如果您阅读mysql_pconnect的手册页,则说明:

  • 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.
  • 如果PHP和MySQL位于同一服务器或本地网络上,则连接时间可以忽略不计,在这种情况下,持久连接没有任何优势。

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

基本上,mysql_pconnect()尝试使用您指定的凭据查找已打开的持久连接。如果没有找到它,那么它会成为一个新的。执行语句后,它也不会关闭连接

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

所以在你的情况下你可能没有注意到差异,但实际上你应该使用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:

但是,如果您阅读mysql_pconnect的手册页,则说明:

  • 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.
  • 如果PHP和MySQL位于同一服务器或本地网络上,则连接时间可以忽略不计,在这种情况下,持久连接没有任何优势。

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

基本上,mysql_pconnect()尝试使用您指定的凭据查找已打开的持久连接。如果没有找到它,那么它会成为一个新的。执行语句后,它也不会关闭连接

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

所以在你的情况下你可能没有注意到差异,但实际上你应该使用mysql_pconnect()。