【问题标题】:what can I do to optimize the following function or some other thing to reduce memory consumption?我可以做些什么来优化以下功能或其他一些事情以减少内存消耗?
【发布时间】:2013-02-01 07:52:07
【问题描述】:

我正在处理业务目录之类的事情,并且需要在类别列表中显示类别的递归父级。

我为此使用了以下功能:

    public function get_recursive_parents($category_id){
        $categories = array();
        $res = $this->db->from('categories')->where('cat_id',$category_id)->get()->row_array();
        $cat_id = $res['parent_id'];
        $categories[] = $res;
        while($cat_id){
            $res = $this->db->from('categories')->where('cat_id',$cat_id)->get()->row_array();
            $categories[] = $res;
            $cat_id = $res['parent_id'];
        }
        return $categories;
    }

我正在使用此功能,因为它在管理站点上,并且在管理站点上有点慢也可以,管理员将只有一个,所以我可以给它更多内存。但我认为限制内存超过 300M一个电话太多了,仍然得到这个:

Fatal error: Allowed memory size of 367001600 bytes exhausted (tried to allocate 72 bytes) in /var/www/usmanproject/salesfinder/system/database/DB_active_rec.php on line 2007 

那么有什么办法可以优化上述功能吗?或者我需要做某种特定类型的索引或算法优化,或任何其他可能的方式?或者我只是停止显示类别的所有父母和超级父母(即客户要求查看层次结构)?或者需要增加内存,因为我已经在一个目录上工作了,而且在管理站点上也很慢,所以我猜他们只是在使用更多的内存?

任何建议将不胜感激。


这是那个表模式,它有 parent_id,所以它作为递归关系工作。

   CREATE TABLE IF NOT EXISTS `categories` (
  `cat_id` int(11) NOT NULL AUTO_INCREMENT,
  `cat_name` varchar(255) DEFAULT NULL,
  `cat_title` varchar(255) DEFAULT NULL,
  `cat_desc` varchar(255) DEFAULT NULL,
  `cat_text` text,
  `parent_id` int(11) NOT NULL,
  `cat_img` varchar(255) DEFAULT NULL,
  `sort_id` int(11) NOT NULL DEFAULT '1',
  `last_level` tinyint(4) NOT NULL,
  PRIMARY KEY (`cat_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=221 ;

【问题讨论】:

  • 你使用的是哪个框架?
  • @ripa 我正在使用 CodeIgniter
  • 好的。您不会在函数中随时调用 get_recursive_parents() 。这不是递归调用。
  • @ripa 我正在通过 while 循环进行递归工作,我可以将其作为递归函数来执行,但这不是问题,问题在于查询,此循环最多运行 4 次,整个函数将不会运行超过 10 次。所以这个查询最多可以在这个页面上运行 40 次,同时一次最多有 1 条记录。所以最大。它可以返回 40 条记录,但有 40 个查询。
  • 某种存储过程可以在这里更好地工作并解决问题吗?

标签: php mysql memory-management recursion query-optimization


【解决方案1】:

尝试使用下面的代码

public function get_recursive_parents($category_id,$categories=array())
{
    if($category_id!="")
    {
        $new_ar1=array();
        $fe = $this->db->from('categories')->where('cat_id',$category_id)->get()->row_array();
        array_push($new_ar1,$fe["parent_id"]);
        return $new_ar1;
    }
    else
    {
        $res = $this->db->from('categories')->get()->row_array();
        array_push($categories,$res['parent_id']);
        $categories[$res['parent_id']]=array();

        array_push($categories[$res['cat_id']],get_recursive_parents($res['parent_id'],$categories));
    }

    return $new_ar;
}

调用函数

get_recursive_parents($category_id);

希望对你有帮助

【讨论】:

    【解决方案2】:

    问题解决了,实际上有一条记录,其parent_id 指向它自己的cat_id,即主键。所以这是指向它自己,在那种情况下,递归根本就没有结束。我使用了while循环,在这种情况下它变成了无限的。

    但是在调试过程中我发现这篇文章很有帮助, http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ 它提供了更好的方法来处理同样的事情。如本文所述,在我的场景中,自我加入很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 2011-05-13
      • 1970-01-01
      相关资源
      最近更新 更多