未使用的 mysql 连接会减慢脚本速度吗? [英] Do unused mysql connections slow down scripts?

查看:25
本文介绍了未使用的 mysql 连接会减慢脚本速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的网站编写 API,以及一个相当大的类来处理所有 API 请求.

I am in the process of writing an API for my website, along with a pretty large class to process all of the API requests.

大多数页面,如果不是网站上的所有页面,都会在加载时向 api 发送至少一个请求.该网站最重要的优先事项是效率和因此非常快速的服务器处理.

Most pages, if not all all the pages on the website will send at least one request to the api on load. The most important priority for this website is efficiency and resultantly, very quick server processing.

因此,当涉及到类和某些 PHP 函数时,我正在寻求一些建议.

I am therefore seeking a little bit of advice when it comes to classes and certain PHP functions.

首先,看起来我正在编写的类最终可能会有大约 3000 行代码.如果这在每个页面上初始化,忽略每个页面只使用类中的一两个函数这一事实,这会使 API 变慢吗?我应该为每个类方法查看带有类扩展名的单独文件吗?

Firstly, it looks like the class I am writing will probably end up being around 3000 lines of code. If this is initialised on each page, ignoring the fact that only one or two of the functions within the class will be used per page, will this make the API much slower? Should I be looking at separate files with extensions to the class for each class method?

其次,我目前在一个目录中的单独文件中拥有与各种数据库的所有连接.每个连接内都有函数 mysql_pconnect().目前,我只在需要时才需要这些文件.因此,如果该方法需要连接到 x 数据库,那么我只需将 require(connection...) 放入该方法中.在类中包含文件是不是很糟糕?

Secondly, I currently have all of my connections to the various databases in separate files within a directory. Within each connection is the function mysql_pconnect(). At the moment, I only require these files when needed. So if the method needs a connection to the x database, then I simply place require(connection...) into the method. Is it bad to include files within a class?

我在问,因为唯一的其他解决方案是要求页面顶部的所有连接,以便类可以访问它们,而不需要每个方法都需要它们.这就是为什么我想知道一次有多个连接,即使它们不使用,是否对服务器造成负担?

I am asking, because the only other solution is to require all of the connections at the top of the page so that the class can access them without requiring them for each method. This is why I would like to know whether having several connections at once, even if they are not used, is taxing on the server?

真的是三个问题:

  1. 在每个页面的开头启动一个大类是否会减慢脚本运行时的速度,即使只使用一个类方法?这就是人们使用类扩展类"的原因吗?
  2. 在类中使用require()"文件不好吗?
  3. 未使用的 mysql 数据库连接是否会减慢脚本的运行速度?

推荐答案

不,一个未使用的 MySQL 连接不会消耗太多(如果有)cpu 时间,尽管它会占用一点内存来处理 '状态",必须在每个连接的基础上维护.

No, an unused MySQL connection won't consume much (if any) cpu time, though it will occupy a bit of memory to handle the various bits of 'state' which have to be maintained on a per-connection basis.

但是,请注意 MySQL 的连接协议实际上相当轻量级".维护一个持久连接池听起来很有吸引力,但无论如何建立新连接的成本已经很低了.

However, note that MySQL's connection protocol is actually fairly "light-weight". Maintaining a pool of persistent connections sounds attractive, but the cost of establishing a new connection is already very low anyways.

持久连接是解决连接开销的快速解决方案,但它们确实会带来问题.最糟糕的是被放弃的连接会使连接处于不确定状态(正在进行的事务、更改的服务器变量/配置等...),除非您非常小心,否则很容易造成无意的死锁.

Persistent connections are a quick fix to solving connection overhead, but they do bring in issues. The worst one being abandoned connections can leave the connections in an indeterminate state (in-progress transactions, changed server variables/configurations, etc...) and you can quite easily create inadvertent deadlocks unless you're very careful.

这篇关于未使用的 mysql 连接会减慢脚本速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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