未使用的mysql连接会减慢脚本吗?

时间:2022-03-16 20:43:27

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,以及一个相当大的类来处理所有的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.

大多数页面,如果不是所有的页面,网站将至少发送一个请求到api的负载。这个网站最重要的优先级是效率和结果,非常快的服务器处理。

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

因此,当涉及到类和某些PHP函数时,我希望得到一些建议。

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?

首先,看起来我写的这个类大概会有3000行代码。如果在每个页面上初始化它,忽略了类中每个页面只使用一个或两个函数的事实,这是否会使API慢得多?我应该为每个类方法查找具有类扩展名的独立文件吗?

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?

其次,我目前在一个目录中拥有到不同数据库的所有连接。在每个连接中都有函数mysql_pconnect()。目前,我只在需要的时候需要这些文件。因此,如果该方法需要连接到x数据库,那么我只需将需要(连接…)放入方法中。在类中包含文件不好吗?

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?

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

So three questions really:

所以三个问题:

  1. Does initiating a large class at the beginning of each page slow down the script runtime, even if only one class method is being used? Is this why people use 'class extends class'?
  2. 在每个页面的开头初始化一个大类是否会减慢脚本运行时,即使只使用了一个类方法?这就是为什么人们使用“类扩展类”吗?
  3. Is it bad to 'require()' files within a class?
  4. 在类中“require()”文件不好吗?
  5. Do unused connections to a mysql database slow down the runtime of a script?
  6. 未使用的mysql数据库连接是否会减慢脚本的运行速度?

2 个解决方案

#1


3  

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连接不会占用太多(如果有的话)cpu时间,但它会占用一点内存来处理必须在每个连接上维护的各种“状态”位。

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.

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

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.

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

#2


1  

If you are writing an api to serve data from your db to multiple 3rd parties you are better off not letting them query the db, not even through a webservice (unless you have second-to-second db mutations that these 3rd parties need immediately)

如果您正在编写一个api来向多个第三方提供来自db的数据,那么最好不要让他们查询db,即使是通过web服务(除非您有这些第三方需要的每秒的db突变)

Better way would be to write xml file(s) to a protected location and use a webservice to auth a 3rd party and serve the xml file(s). Then update the xml periodically.

更好的方法是将xml文件写入受保护的位置,并使用web服务来获取第三方并服务于xml文件。然后定期更新xml。

#1


3  

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连接不会占用太多(如果有的话)cpu时间,但它会占用一点内存来处理必须在每个连接上维护的各种“状态”位。

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.

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

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.

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

#2


1  

If you are writing an api to serve data from your db to multiple 3rd parties you are better off not letting them query the db, not even through a webservice (unless you have second-to-second db mutations that these 3rd parties need immediately)

如果您正在编写一个api来向多个第三方提供来自db的数据,那么最好不要让他们查询db,即使是通过web服务(除非您有这些第三方需要的每秒的db突变)

Better way would be to write xml file(s) to a protected location and use a webservice to auth a 3rd party and serve the xml file(s). Then update the xml periodically.

更好的方法是将xml文件写入受保护的位置,并使用web服务来获取第三方并服务于xml文件。然后定期更新xml。