CakePHP 重复相同的查询
我有一个模型结构:类别有许多产品有许多库存项目属于仓库、制造商。
我使用此代码获取数据,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
如果你删除仓库和制造商,你会发现 Cakephp 只执行一个查询。
或者,您可以为复杂查询创建数据库视图。
Try this:
If you remove Warehouse and Manufacturer you will find that Cakephp only executes one query.
Alternatively you can create database views for complex queries.