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

发布于 2024-12-27 15:42:49 字数 699 浏览 0 评论 0原文

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

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

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

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

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

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

因此,确实存在三个问题:

  1. 即使只使用一个类方法,在每个页面的开头启动一个大类是否会减慢脚本运行时间?这就是人们使用“类扩展类”的原因吗?
  2. 在类中“require()”文件是否不好?
  3. 未使用的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2025-01-03 15:42:49

不,未使用的 MySQL 连接不会消耗太多(如果有的话)CPU 时间,尽管它会占用一点内存来处理必须在每个连接的基础上维护的各种“状态”位。

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

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

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.

丶视觉 2025-01-03 15:42:49

如果您正在编写一个 API 来将数据库中的数据提供给多个第三方,那么您最好不要让他们查询数据库,甚至不要通过 Web 服务(除非您有这些第三方立即需要的第二到第二个数据库突变)

更好的方法是将 xml 文件写入受保护的位置,并使用 Web 服务对第三方进行身份验证并提供 xml 文件。
然后定期更新xml。

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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文