简体   繁体   English

CodeIgniter DataMapper ORM内存问题

[英]CodeIgniter DataMapper ORM memory issue

I am using CodeIgniter and DataMapper to manage relationships for an ecommerce store. 我正在使用CodeIgniter和DataMapper管理电子商务商店的关系。 The relationships are: 关系是:

A product has many skus A product has many amazon_products A sku has many amazon_products 一个产品有很多skus一个产品有很多amazon_products一个sku有很多amazon_products

I'm trying to retrieve all of the data from the database and format it to be used by Backbone on the front end. 我正在尝试从数据库中检索所有数据并将其格式化以供前端Backbone使用。 I ultimately want to send json to the page in the following format: 我最终希望以以下格式将json发送到页面:

Array of products
    Product
        Array of amazon_products 
        Array of skus
            Array of amazon_products
    Product
        Array of amazon_products 
        Array of skus
            Array of amazon_products
    Product
        Array of amazon_products 
        Array of skus
            Array of amazon_products

Currently, I'm generating this data with the following PHP code: 目前,我正在使用以下PHP代码生成此数据:

$products = new Product();
$products->order_by('ext_created_on', 'desc')->get();

$data['products'] = array();

foreach($products as $product){
        $product_array = $product->to_array();
        $product_array['amazon'] = $product->amazon_product->get()->all_to_array();
        foreach($product->sku->get() as $sku){
                $sku_array = $sku->to_array();
                $sku_array['amazon'] = $sku->amazon_product->get()->all_to_array();
                $product_array['skus'][] = $sku_array;
        }
        $data['products'][] = $product_array;

        echo memory_get_usage()."<br>"
}

$data['products'] = json_encode($data['products']);

When I monitor the memory of my Ubuntu server when this executes, it seems to be using a lot (~13% of Memory). 当我监视Ubuntu服务器执行时的内存时,似乎使用了很多内存(约占内存的13%)。 Also, since there are about 700 products in the database, DataMapper is running 1000's of queries to get all of this data. 另外,由于数据库中大约有700种产品,所以DataMapper正在运行1000个查询来获取所有这些数据。 Are the queries causing the high memory use? 查询是否导致高内存使用? Or is there something I'm overlooking in the foreach loop that could be resulting in a memory a memory leak? 还是在foreach循环中我忽略了某些事情,可能导致内存泄漏?

The output of get_memory_usage() is 33678176 after the first product loop, and 109580968 after the last. get_memory_usage()的输出在第一个乘积循环后为33678176,在最后一个乘积后为109580968。

In case anyone comes across this, I was able to cut the memory usage to about 1/3rd by using DataMapper's clear() method. 万一有人遇到这种情况,我可以使用DataMapper的clear()方法将内存使用量减少到大约1/3。 I simply added the line $product->clear() at the end of the outer foreach loop. 我只是在外部foreach循环的末尾添加了$product->clear()行。

I also added a $sku->clear() at the end of the inner loop. 我还在内部循环的末尾添加了$sku->clear()

While I've never used the DataMapper library you are using, you mentioned that there are thousands of queries being run in order to retrieve the results. 尽管我从未使用过使用的DataMapper库,但您提到要运行数千个查询才能检索结果。 Based on that, it seems like maybe there is a better method than get() for retrieving $product->sku and $sku->amazon_product. 基于此,似乎有比get()更好的方法来检索$ product-> sku和$ sku-> amazon_product。 Is your first get() on line #2 not acquiring all of the information for you? 第2行上的第一个get()是否没有为您获取所有信息? My thinking is that get() does not check for available result values, and that another function in your DataMapper may refer to the available result set before making additional calls to the database. 我的想法是get()不会检查可用的结果值,并且DataMapper中的另一个函数可能在对数据库进行其他调用之前先引用可用的结果集。

In fact, I wonder if $product->sku is already cached with the value for sku. 实际上,我想知道$ product-> sku是否已经用sku的值缓存了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM