每次查询后关闭MySQL连接 [英] Closing MySQL Connection After Every Query

查看:241
本文介绍了每次查询后关闭MySQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究由一位前同事的才能组成的一些旧代码时,我仰望着我,直到每次查询后,我都注意到他们关闭了MySQL服务器的连接.

While digging through some old code put together by a former co-worker who's talents I looked up to I noticed after every query they closed the connection the MySQL server.

这似乎有点奇怪,因为我一直只是在页面末尾关闭它,看到大多数人都这样做.

This seems a bit odd since I've always just closed it at the end of the page and seen most people do the same.

所以问题是,这是PHP/MySQL中最好的主意吗?它甚至是一种方式或另一种方式都重要吗?该站点在一天中的任何指定时间都只有大约100个活动的用户在运行.因此,我看不到它会产生一种或其他方式的任何实际影响,但也许对于更大的网站有意义吗?

So the question is, is this the best idea in PHP/MySQL? Does it even matter one way or the other? The site this is running on has only about 100 users active on it at any given point throughout the day. So I can't see any real impact it would have one way or the other, but maybe it would make sense for a larger site?

推荐答案

在处理需要扩展到多个并发用户的应用程序时,这将很有帮助.这样,您只能在尽可能短的时间内打开连接(即,仅在使用时).

This can be helpful when working on an application that needs to scale to many concurrent users. By doing this, you only have the connection open for as short of a time as possible (ie, only when you are using it).

如果您有500个并发请求,它们将能够更好地相吻合,这样一个请求就可以在PHP端进行计算,而另一个请求则可以阻止对数据库的访问.

If you had 500 concurrent requests, they would be able to dovetail better so that one request could be doing computation on the PHP side while another was blocking on the database access.

这篇关于每次查询后关闭MySQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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