PHP - Zend Framework - 使用静态方法进行数据访问

发布于 2024-12-05 14:42:32 字数 369 浏览 0 评论 0原文

我一直在网上阅读有关使用静态方法从数据库访问数据的优缺点的信息。我有一个基于 Zend 框架构建的基于 LAMP 的网站。

有人说在性能方面 - 几乎没有任何区别,正如 Greg Beech 的分析所引用的那样

在调用一个方法 20 亿次时,通过将其设置为静态,您可以节省大约 5 毫秒,这对于每个方法调用来说几乎没有节省任何时间

但是如果我正确理解静态方法的概念 - 只有一个方法实例,这意味着只有一个数据库连接 - 因此在高流量网站上 - 当一个请求正在被服务时 - 其他请求将排队等待相同的方法,对吧?

因此,只想了解您的想法和意见,了解将访问方法声明为静态是否有意义。

非常感谢

I've been reading online about the pros and cons of using static methods for accessing data from the database. I have a LAMP based site built on Zend framework.

Some say in terms of performance - there is hardly any difference as quoted here by Greg Beech's analysis

in calling a method two billion times you could save yourself around 5ms by making it static, which is a saving of approximately nothing for each method call

But if I understand the concept of static methods correctly - there is only one instance of method which means only one DB connection - so on a high traffic website - while one request is being served - the other requests will be queued for the same method right?

So just wanted your thoughts and inputs on whether it makes sense to declare access methods as static or not.

Thanks much

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

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

发布评论

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

评论(2

梦屿孤独相伴 2024-12-12 14:42:32

我认为您可能对这里的流程有偏差的理解。

各个访客的每个单独请求均完全独立地得到满足。想象一下这 2 个独立的请求:

Request visitor 1              Request visitor 2

index.php                      index.php
creates Zend_Db instance $db   creates Zend_Db instance $db
calls $db->insert()            calls $db->insert()
calls $db->insert() again      calls $db->insert() again

                    \        /
                     \      /
                      \    /

                     Database
           Queue might be something like:

             4. insert 2 from request 2
             3. insert 2 from request 1
             2. insert 1 from request 2
             1. insert 1 from request 1

换句话说:基本上只有数据库本身会因 SQL 语句排队而受到困扰。

PHP 脚本彼此独立工作。因此,无论您创建实例方法还是静态方法,都与 SQL 查询的排队方式完全无关。

I think you may have a skewed understanding of the processes at hand here.

Every individual request of an individual visitor is served completely independent of each other. Imagine these 2 independent requests:

Request visitor 1              Request visitor 2

index.php                      index.php
creates Zend_Db instance $db   creates Zend_Db instance $db
calls $db->insert()            calls $db->insert()
calls $db->insert() again      calls $db->insert() again

                    \        /
                     \      /
                      \    /

                     Database
           Queue might be something like:

             4. insert 2 from request 2
             3. insert 2 from request 1
             2. insert 1 from request 2
             1. insert 1 from request 1

In other words: basically only the database itself is troubled with queuing the SQL statements.

The PHP scripts work independent of each other. So whether you create instance methods or static methods is completely irrelevant to how the SQL queries are being queued.

蹲墙角沉默 2024-12-12 14:42:32

静态方法并不一定意味着只有一个,它意味着您不需要类的实例来调用方法。如果您只希望一个对象的 1 个实例在其生命周期内存在,则可以使用单例模式一个请求。

每个请求页面的用户都会获得他们自己的该对象的实例,但他们只能拥有其中一个。

正如fireeyedboy所说,数据库会负责在请求进来时对其进行排队。在请求大量涌入的情况下,每个页面与数据库交互的调用可能必须等待较早的请求先被处理,从而导致一些处理延迟。

A static method doesn't necessarily mean that there is only one, it means you don't need an instance of the class to call a method. The Singleton Pattern can be used if you only want 1 instance of an object to exist for the lifetime of a request.

Each individual user requesting a page would get their own instance of that object, but they could only have one of each.

As fireeyedboy said, the database would be responsible for queuing up the requests as they came in. In the case of a large influx of requests, each page's calls to interact with the database may have to wait for earlier requests to be processed first, causing some delays to processing.

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