mysql_connect VS mysql_pconnect

发布于 2024-07-08 18:47:31 字数 1490 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

記憶穿過時間隧道 2024-07-15 18:47:31

MySQL 应该不需要持久连接。 在其他数据库(例如 Oracle)中,建立连接既昂贵又耗时,因此如果您可以重复使用连接,那就是一个巨大的胜利。 但是那些品牌的数据库提供了连接池,这更好地解决了这个问题。

与其他品牌相比,连接到 MySQL 数据库的速度很快,因此使用持久连接为 MySQL 带来的好处比其他品牌的数据库要少。

持久连接也有缺点。 数据库服务器为每个连接分配资源,无论这些连接是否需要。 因此,如果连接空闲,您会看到大量资源无缘无故地被浪费。 我不知道你是否会达到 10,000 个空闲连接,但即使是几百个也是昂贵的。

连接具有状态,PHP 请求从先前由另一个 PHP 请求使用的会话中“继承”信息是不合适的。 例如,临时表和用户变量通常会在连接关闭时被清除,但如果您使用持久连接则不会。 同样基于会话的设置,如字符集和排序规则。 另外,LAST_INSERT_ID() 会报告会话期间最后生成的 ID——即使是在之前的 PHP 请求期间。

至少对于 MySQL 来说,持久连接的缺点可能大于其优点。 还有其他更好的技术来实现高可扩展性。


2014 年 3 月更新:

与其他品牌的 RDBMS 相比,MySQL 连接速度始终较低,但它正在变得更好。

请参阅http://mysqlserverteam.com/improving-connectdisconnect-performance/

在 MySQL 5.6 中,我们开始致力于优化处理连接和断开连接的代码。 而这项工作在 MySQL 5.7 中得到了加速。 在这篇博文中,我将首先展示我们所取得的成果,然后描述我们为实现这些成果所做的工作。

请阅读博客了解更多详细信息和速度比较。

Persistent connections should be unnecessary for MySQL. In other databases (such as Oracle), making a connection is expensive and time-consuming, so if you can re-use a connection it's a big win. But those brands of database offer connection pooling, which solves the problem in a better way.

Making a connection to a MySQL database is quick compared to those other brands, so using persistent connections gives proportionally less benefit for MySQL than it would for another brand of database.

Persistent connections have a downside too. The database server allocates resources to each connection, whether the connections are needed or not. So you see a lot of wasted resources for no purpose if the connections are idle. I don't know if you'll reach 10,000 idle connections, but even a couple of hundred is costly.

Connections have state, and it would be inappropriate for a PHP request to "inherit" information from a session previously used by another PHP request. For example, temporary tables and user variables are normally cleaned up as a connection closes, but not if you use persistent connections. Likewise session-based settings like character set and collation. Also, LAST_INSERT_ID() would report the id last generated during the session -- even if that was during a prior PHP request.

For MySQL at least, the downside of persistent connections probably outweighs their benefits. And there are other, better techniques to achieve high scalability.


Update March 2014:

MySQL connection speed was always low compared to other brands of RDBMS, but it's getting even better.

See http://mysqlserverteam.com/improving-connectdisconnect-performance/

In MySQL 5.6 we started working on optimizing the code handling connects and disconnects. And this work has accelerated in MySQL 5.7. In this blog post I will first show the results we have achieved and then describe what we have done to get them.

Read the blog for more details and speed comparisons.

行雁书 2024-07-15 18:47:31

基本上,您必须平衡创建连接与保持连接的成本。 尽管 MySQL 建立新连接的速度非常快,但它仍然需要付出代价——线程设置时间以及 Web 服务器的 TCP/IP 设置时间。 这在流量足够高的网站上很明显。 不幸的是,PHP 对连接的持久性没有任何控制。 因此,答案是大幅降低 MySQL 中的空闲超时(例如降低到 20 秒),并增加线程缓存大小。 总之,这通常效果非常好。

另一方面,您的应用程序需要尊重连接的状态。 最好不要假设会话所处的状态。如果您使用临时表,那么使用 CREATE IF NOT EXISTS 和 TRUNCATE TABLE 会有很大帮助,对它们进行唯一命名(例如包含为 userid)也有很大帮助。 交易问题比较多; 但您的代码始终可以在顶部执行 ROLLBACK,以防万一。

Basically you have to balance the cost of creating connections versus keeping connections. Even though MySQL is very fast at setting up a new connection, it still costs -- in thread setup time, and in TCP/IP setup time from your web server. This is noticeable on a high-enough traffic site. Unfortunately, PHP does not have any controls on the persistence of connections. So the answer is to lower the idle timeout in MySQL a long way (like down to 20 seconds), and to up the thread cache size. Together, this generally works remarkably well.

On the flip side, your application needs to respect the state of the connection. It is best if it makes no assumptions about what state the session is in. If you use temporary tables, then using CREATE IF NOT EXISTS and TRUNCATE TABLE helps a lot, as does naming them uniquely (such as including as userid). Transactions are bit more problematic; but your code can always do ROLLBACK at the top, just in case.

决绝 2024-07-15 18:47:31

mysql_connect()mysql_pconnect() 都适用于数据库连接,但差别不大。 在mysql_pconnect()中,p代表持久连接。

当我们使用mysql_connect()函数时,每次它都会根据请求打开和关闭数据库连接。

但在 mysql_pconnect() 函数的情况下:

  • 首先,在连接时,该函数将尝试查找已使用相同主机、用户名和密码打开的(持久)连接。 如果找到,将返回它的标识符,而不是打开新连接。

  • 其次,当脚本执行结束时,与SQL服务器的连接不会关闭。 相反,连接将保持打开状态以供将来使用(mysql_close() 不会关闭 mysql_pconnect() 建立的连接)。

当您的网站流量很大时,mysql_pconncet() 非常有用。 此时,对于每个请求,它都不会打开连接,而是从池中获取连接。 这将提高您网站的效率。 但对于一般用途,mysql_connect() 是最好的。

mysql_connect() and mysql_pconnect() both are working for database connection but with little difference. In mysql_pconnect(), p stands for persistance connection.

When we are using mysql_connect() function, every time it is opening and closing the database connection, depending on the request.

But in case of mysql_pconnect() function:

  • First, when connecting, the function would try to find a (persistent) connection that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

  • Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the connection will remain open for future use (mysql_close() will not close connection established by mysql_pconnect()).

mysql_pconncet() is useful when you have a lot of traffice on your site. At that time for every request it will not open a connection but will take it from the pool. This will increase the efficiency of your site. But for general use mysql_connect() is best.

落日海湾 2024-07-15 18:47:31

您不太可能达到 10000 个连接。 无论如何,请访问官方来源。 (强调我的)。

如果持久连接没有
任何附加功能,是什么
它们有什么用处?

答案非常简单——
效率。 持久连接是
如果创建链接的开销很好
对你的SQL服务器来说是高的。 是否
这个开销不是很高
取决于很多因素。 像什么
它是什么类型的数据库,无论是
不是它位于同一台计算机上
您的网络服务器位于哪个位置,如何
加载 SQL 服务器所在的机器
等等。 底线
如果连接开销是
高、持久的连接可以帮助您
相当大
。 他们导致孩子
仅连接一次的过程
在其整个生命周期内,而不是
每次它处理一个页面时
需要连接到 SQL 服务器。
这意味着对于每个孩子来说
打开持久连接将
有自己的开放持久
连接到服务器。 例如,
如果你有 20 个不同的孩子
运行脚本的进程
与 SQL 的持久连接
服务器,你有 20 个不同的
与 SQL 服务器的连接,一
来自每个孩子。

但是请注意,这可能会产生一些影响
如果您使用数据库,则有缺点
连接限制为
被顽固的孩子超越
连接。 如果您的数据库有
16 个同时连接的限制,
在繁忙的服务器过程中
会话中,17 个子线程尝试
连接,一将无法连接。 如果
您的脚本中存在错误
不允许关闭连接
向下(例如无限循环),
只有 16 个连接的数据库可能
迅速被淹没。 检查你的
数据库信息文档
关于处理废弃或闲置的
连接。

It's very unlikely that you'll reach 10000 connections. Anyhow, go to the official source. (Emphasis mine).

If persistent connections don't have
any added functionality, what are
they good for?

The answer here is extremely simple --
efficiency. Persistent connections are
good if the overhead to create a link
to your SQL server is high. Whether or
not this overhead is really high
depends on many factors. Like, what
kind of database it is, whether or
not it sits on the same computer on
which your web server sits, how
loaded the machine the SQL server sits
on is and so forth. The bottom line
is that if that connection overhead is
high, persistent connections help you
considerably
. They cause the child
process to simply connect only once
for its entire lifespan, instead of
every time it processes a page that
requires connecting to the SQL server.
This means that for every child that
opened a persistent connection will
have its own open persistent
connection to the server. For example,
if you had 20 different child
processes that ran a script that made
a persistent connection to your SQL
server, you'd have 20 different
connections to the SQL server, one
from each child.

Note, however, that this can have some
drawbacks if you are using a database
with connection limits that are
exceeded by persistent child
connections. If your database has a
limit of 16 simultaneous connections,
and in the course of a busy server
session, 17 child threads attempt to
connect, one will not be able to. If
there are bugs in your scripts which
do not allow the connections to shut
down (such as infinite loops), the
database with only 16 connections may
be rapidly swamped. Check your
database documentation for information
on handling abandoned or idle
connections.

无敌元气妹 2024-07-15 18:47:31

MYSQL_CONNECT()

1.mysql_connect可用于关闭连接。每次打开和关闭数据库连接时,根据请求而定。

2.这里在MYSQL connect中每次加载页面时都会打开数据库

3.页面加载时每次都会加载数据库

4.用于关闭连接

示例:

<?php $conn = mysql_connect(‘host’, ‘mysql_user’, ‘mysql_password’); if(!$conn){ die(‘Could not connect: ‘ . mysql_error()); } echo ‘Connected successfully’; mysql_close($conn); ?>

说明:

host:指定主机名或IP地址如本地主机。

mysql_user:指定MySQL用户名

mysql_password:指定MySQL密码

MYSQL_PCONNECT()

1.我们使用mysql_pconncet(),它最初尝试查找打开的持久连接。

2.mysql_pconncet()打开持久连接

3.mysql_pconnect()不支持关闭连接

4.mysql_pconnect()无法关闭连接。 这里打开与数据库的持久连接

5.这里数据库不需要每次都连接。

6.mysql_pconncet()中不需要每次都连接数据库。

更多详细信息:http://prittytimes.com/difference- Between-mysql_connect-and-mysql_pconnect /

MYSQL_CONNECT()

1.mysql_connect can be used to close the connection.Every time it is opening and closing the database connection, depending on the request .

2.Here database is opened everytime when the page is loaded in MYSQL connect

3.When the page is loaded, the database is loaded everytime

4.It is used to close the connection

Example:

<?php $conn = mysql_connect(‘host’, ‘mysql_user’, ‘mysql_password’); if(!$conn){ die(‘Could not connect: ‘ . mysql_error()); } echo ‘Connected successfully’; mysql_close($conn); ?>

Description:

host: Specifies a host name or an IP address like localhost.

mysql_user: Specifies the MySQL username

mysql_password: Specifies the MySQL password

MYSQL_PCONNECT()

1.We use the mysql_pconncet(), it initially tries to find an open persistent connection.

2.The mysql_pconncet() opens persistant connection

3.The mysql_pconnect() does not support the close connection

4.mysql_pconnect() cannot close the connection. Here open a persistant connection to the database

5.Here database need not be connected everytime.

6.The database need not be connected every time in mysql_pconncet().

more details:http://prittytimes.com/difference-between-mysql_connect-and-mysql_pconnect/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文