CakePHP 重复相同的查询

发布于 2024-08-25 13:28:52 字数 837 浏览 2 评论 0原文

我有一个模型结构:类别有许多产品有许多库存项目属于仓库、制造商。

我使用此代码获取数据,使用 containsable 能够在关联模型中进行更深入的过滤:

$this->Category->find('all', array(
        'conditions' => array('Category.id' => $category_id),
        'contain' => array(
            'Product' => array(
                'Stockitem' => array(
                    'conditions' => array('Stockitem.warehouse_id' => $warehouse_id),
                    'Warehouse',
                    'Manufacturer',
                )
            )
        ),
        )
    );

数据结构返回得很好,但是,我得到多个重复查询,例如基于数据集,有时连续数百个此类查询。

SELECT `Warehouse`.`id`, `Warehouse`.`title` FROM `beta_warehouses` AS `Warehouse` WHERE `Warehouse`.`id` = 2

基本上,在构建数据结构时,Cake 会一遍又一遍地从 mysql 中获取每一行的数据。我们有几千行的数据集,我感觉这会影响性能。是否可以使其缓存结果而不重复相同的查询?

I have a model structure: Category hasMany Product hasMany Stockitem belongsTo Warehouse, Manufacturer.

I fetch data with this code, using containable to be able to filter deeper in the associated models:

$this->Category->find('all', array(
        'conditions' => array('Category.id' => $category_id),
        'contain' => array(
            'Product' => array(
                'Stockitem' => array(
                    'conditions' => array('Stockitem.warehouse_id' => $warehouse_id),
                    'Warehouse',
                    'Manufacturer',
                )
            )
        ),
        )
    );

Data structure is returned just fine, however, I get multiple repeating queries like, sometimes hundreds of such queries in a row, based on dataset.

SELECT `Warehouse`.`id`, `Warehouse`.`title` FROM `beta_warehouses` AS `Warehouse` WHERE `Warehouse`.`id` = 2

Basically, when building data structure Cake is fetching data from mysql over and over again, for each row. We have datasets of several thousand rows, and I have a feeling that it's going to impact performance. Is it possible to make it cache results and not repeat same queries?

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

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

发布评论

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

评论(1

夏花。依旧 2024-09-01 13:28:52

试试这个:

$this->Product->find('all', array(
    'conditions' => array('Category.id' => $category_id, 'Stockitem.warehouse_id' => $warehouse_id),
    'contain' => array(
        'Category'
        , 'Stockitem' => array(
            'Warehouse'
            , 'Manufacturer'
        )
    ),
));

如果你删除仓库和制造商,你会发现 Cakephp 只执行一个查询。

或者,您可以为复杂查询创建数据库视图。

Try this:

$this->Product->find('all', array(
    'conditions' => array('Category.id' => $category_id, 'Stockitem.warehouse_id' => $warehouse_id),
    'contain' => array(
        'Category'
        , 'Stockitem' => array(
            'Warehouse'
            , 'Manufacturer'
        )
    ),
));

If you remove Warehouse and Manufacturer you will find that Cakephp only executes one query.

Alternatively you can create database views for complex queries.

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