如何在 Zend_Db_Table 之上以一种高性能的方式使用 RDBMS?(如果有的话……) [英] How does one use the RDBMS in a performant way on top of Zend_Db_Table? (if at all...)

查看:36
本文介绍了如何在 Zend_Db_Table 之上以一种高性能的方式使用 RDBMS?(如果有的话……)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在一个 Zend Framework 项目中工作时,我的脑海里一直在持续不断的战争——好的应用程序设计要求数据库查询的数量最小化.每个查询在延迟和 RDBMS 计算时间方面都很昂贵.Zend_Db_Table 鼓励您进行大量查询——例如,没有内置的方式与其他 Zend_Db_Tables 连接.并且没有办法在给定 Zend_Db_Table 实例的情况下提取底层表的名称,这使得根据 Zend_Db_Table 编写 Zend_Db_Select 查询变得困难.从某种意义上说,Zend_Db_Table 鼓励您实现 RDBMS 已经在 PHP 中提供的功能,这显然是次优的.

There's this constant war going on in my thoughts when I work on a Zend Framework project -- good application design mandates that the number of database queries be minimized. Each query is expensive in terms of both latency and RDBMS calculation time. And Zend_Db_Table encourages you to make lots of queries -- there's no built in way to JOIN with other Zend_Db_Tables, for example. And there's no way to extract the name of the underlying table given an instance of Zend_Db_Table, which makes writing Zend_Db_Select queries in terms of Zend_Db_Table difficult. In a sense, Zend_Db_Table encourages you to implement features the RDBMS already provides in PHP, which is obviously suboptimal.

另一方面,Zend_Db_Table 通过抽象出 SQL 查询本身,使表的行为更像原生 PHP 对象.用户不必担心经常引用所有内容,SQL 操作作为简单的 PHP 方法公开.

On the other hand, Zend_Db_Table makes tables behave a bit more like native PHP objects by abstracting away the SQL queries themselves. The user need not worry about quoting all that often, and SQL operations are exposed as simple PHP methods.

最好的是 Zend_Db_Table 之类的东西,但它会使用惰性操作(就像 Microsoft 对 LINQ-to-SQL/实体框架/等所做的那样)以便将似乎是多个 PHP 语句的内容压缩为单个(或至少更少)RDBMS 查询(y|ies).

What would be nice would be something like Zend_Db_Table, but which would use lazy operations (as Microsoft does with LINQ-to-SQL/Entity Framework/etc.) in order to condense what seems to be multiple PHP statements into a single (or at least fewer) RDBMS quer(y|ies).

这种东西存在吗?它是否包含在 Zend 中?

Does such a thing exist? Is it included with Zend or not?

推荐答案

我同意你的看法,Zend_Db_Table 有时就像一把寻找钉子的锤子.它并不总是编写带有连接的定制的高性能查询的最自然的工具.在我看来,Zend_Db_Table 主要适用于没有连接的单个表.但是仍然可以使用 Zend_Db 组件的各个方面进行优化的数据库查询.

I agree with you that Zend_Db_Table sometimes acts as a hammer in search of nail. It's not always the most natural tool for writing customized, performant queries with joins. It seems to me that Zend_Db_Table is optimal primarily for a single table without joins. But it is still possible to do optimal db querying using aspects of the Zend_Db component.

正如@singles 所指出的,所有这些 db 访问都应该隐藏在模型中(对于 ActiveRecord 类型的方法)或映射器(对于 DataMapper 方法)或实体管理器(如 Doctrine2)中.

As noted by @singles, all this db access should probably be buried inside a model (for an ActiveRecord type of approach) or in a mapper (for a DataMapper approach) or in an entity manager (like Doctrine2) does.

例如,您的自定义BlogPost 模型可以提供一个自定义BlogPostDataAccess 的实例,该实例将提供一个Zend_Db_Adapter 的实例.您的所有查询(包括优化的连接)都将位于 BlogPost_DataAccess 类中.DataAccess 类中的 SELECT 查询可以使用类似(伪代码)之类的东西来编写:

For exmaple, your custom BlogPost model could be fed an instance of a custom BlogPostDataAccess which would be fed an instance of Zend_Db_Adapter. All your queries - including your optimized joins - would reside within the BlogPost_DataAccess class. The SELECT queries in the DataAccess class could be written using soemthing like (pseudocode):

$select = $adapter->select()
        ->join()
        ->join()
        // etc.
        ->join()
        ->where()
        ->limit()
        ->order();

明白我的意思吗?

更新

$tableMap 成员变量怎么样?类似的东西:

How about a $tableMap member variable? Something like:

public static $tableMap = array(
    'posts'        => 'table_posts',
    'users'        => 'table_users',
    'categories'   => 'table_categories',
    // etc
);

然后在您的查询中,您可以使用以下内容:

Then in your queries, you could use something like:

$select = $adapter->select();
$select->from(self::$tableMap['users'], array('name', 'email'));

也许可以添加名为 setTableMap($tableMap)getTableMap()getTable($k)setTable($k),将初始映射数据存储在配置文件中,然后在引导过程中填充静态成员.

Maybe add methods called setTableMap($tableMap), getTableMap(), getTable($k), setTable($k), store the initial mapping data in a config file and then populate the static member during bootstrap.

这篇关于如何在 Zend_Db_Table 之上以一种高性能的方式使用 RDBMS?(如果有的话……)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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