mysql_connect与mysql_pconnect [英] mysql_connect VS mysql_pconnect

查看:113
本文介绍了mysql_connect与mysql_pconnect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个疑问,我已经在网上搜索了,答案似乎是多种多样的.通过PHP连接到数据库时,在mysql_connect上使用mysql_pconnect更好吗?我读到pconnect的伸缩性要好得多,但另一方面,它是一个持久连接...同时拥有10000个连接(全部持久)对我而言似乎不可扩展.

I have this doubt, I've searched the web and the answers seem to be diversified. Is it better to use mysql_pconnect over mysql_connect when connecting to a database via PHP? I read that pconnect scales much better, but on the other hand, being a persistent connection... having 10 000 connections at the same time, all persistent, doesn't seem scalable to me.

谢谢.

推荐答案

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

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.

与其他品牌的数据库相比,与MySQL数据库建立连接的速度较快,因此与其他品牌的数据库相比,使用持久连接给MySQL带来的收益成比例地减少.

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.

持久连接也有缺点.无论是否需要连接,数据库服务器都会为每个连接分配资源.因此,如果连接处于空闲状态,则会无目的地看到大量浪费的资源.我不知道您是否会达到10,000个空闲连接,但即使是数百个也会很昂贵.

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.

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

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.

至少对于MySQL来说,持久连接的弊端可能胜过它们的好处.还有其他更好的技术来实现高可伸缩性.

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

2014年3月更新:

Update March 2014:

与其他品牌的RDBMS相比,MySQL的连接速度始终很低,但是越来越好.

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

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

在MySQL 5.6中,我们开始着手优化代码处理连接和断开连接.并且这项工作在MySQL 5.7中得到了加速.在这篇博客文章中,我将首先展示我们已经取得的成果,然后描述我们为获得这些成就所做的努力.

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.

这篇关于mysql_connect与mysql_pconnect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆