不要未使用MySQL连接减缓脚本? [英] Do unused mysql connections slow down scripts?

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

问题描述

我在写一个API为我的网站,以pretty大类来处理所有的API请求沿的过程。

大多数页面,如果不是所有的所有网站上的页面将至少发送一个请求负载的API。本网站的最重要的优先事项是效率和resultantly,很快服务器处理。

因此​​,我正寻求建议的一点点,当涉及到类和某些PHP函数。

首先,它看起来像我写这封信最终可能会被周围的3000 code线的类。如果此被初始化每一页上,忽略,只有一个或两个类中的功能将每页所使用的事实,将在此使API慢得多?我应该与扩展看着单独的文件的类每个类的方法?

其次,我现在有我所有的在单独的文件目录中的各种数据库的连接。内的每个连接是函数mysql_pconnect()。目前,在需要的时候,我只需要这些文件。因此,如果该方法需要在x数据库的连接,那么我只要把需要(连接...)到方法。这样做不好一个类中包含文件?

我请求,因为唯一的其他的解决方案是要求所有在页面顶部的连接,使得类可以而不需要他们为每个方法访问它们。这就是为什么我想知道是否具有几个一次连接,即使不使用它们,征税是在服务器上?

于是三个问题真的:


  1. 是否在每个页面减慢脚本运行时,开始时启动的一大类即使只使用一个类方法?这是为什么人们使用'类扩展类的?

  2. 是不是不好要求()一类中的文件?

  3. 请MySQL数据库未使用的连接放慢脚本的运行?


解决方案

没有,未使用的MySQL连接不会消耗过多的(如果有的话)的CPU时间,但它会占用一点内存来处理的各个位国家,这必须保持对每个连接。

但是,请注意MySQL的连接协议其实是相当轻量。维护持久连接池听起来很诱人,但建立新连接的成本已经很低了反正。

永久连接是一个速战速决的开销解决连接,但他们的问题带来的。最差的一个被遗弃的连接可以让处于不确定状态(正在进行的事务,改变了服务器变量/配置等)的连接,除非你非常小心,你可以很轻松地创建意外死锁。

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.

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.

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

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?

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?

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. Is it bad to 'require()' files within a class?
  3. Do unused connections to a mysql database slow down the runtime of a script?

解决方案

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.

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天全站免登陆