在PHP/MySQL中,我应该打开多个数据库连接还是共享1? [英] In PHP/MySQL should I open multiple database connections or share 1?

查看:66
本文介绍了在PHP/MySQL中,我应该打开多个数据库连接还是共享1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想听听别人对此的看法?目前,我在标头类型文件内建立一个mysql数据库连接,该文件随后包含在我网站的每个页面的顶部.然后,我可以在该1个打开的连接上运行任意数量的查询.如果页面是从包含的6个文件构建的,并且有15个不同的mysql查询,则它们都将在此1个连接上运行.

I am wanting to hear what others think about this? Currently, I make a mysql database connection inside of a header type file that is then included in the top of every page of my site. I then can run as many queries as I want on that 1 open connection. IF the page is built from 6 files included and there is 15 different mysql queries, then they all would run on this 1 connection.

现在有时我会看到建立多个连接的类,例如每个查询1个.

Now sometimes I see classes that make multiple connections, like 1 for each query.

使用一种方法相对于另一种方法有什么好处?我认为1个连接要好于多个连接,但是我可能错了?

Is there any benefit of using one method over the other? I think 1 connection is better then multiple but I could be wrong?

推荐答案

创建连接可能会很昂贵(我目前尚无此语句的引用 Aha!在这里),因此似乎共识是使用更少的连接.与多个连接相比,对单个页面上的所有查询使用单个连接似乎是更好的选择.

Creating connections can be expensive (I don't have a reference for this statement as yet Aha! Here it is) so it seems as if the consensus is to use fewer connections. Using a single connection for all queries on a single page seems to be a better choice than multiple connections.

这篇关于在PHP/MySQL中,我应该打开多个数据库连接还是共享1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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