我该怎么做才能优化以下功能或减少内存消耗? [英] what can I do to optimize the following function or some other thing to reduce memory consumption?

查看:109
本文介绍了我该怎么做才能优化以下功能或减少内存消耗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究业务目录之类的东西,并且需要在类别列表中显示类别的递归父类.

I am working on business directory sort of thing, and need to show recursive parents of categories at category listing.

我正在使用以下功能:

    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;
    }

我正在使用此功能,因为它在管理站点上使用,并且在管理站点上有点慢也可以,并且admin只能是一个,所以我可以给它提供更多的内存.但是我认为限制内存超过300M一个电话太多了,仍然收到了这个消息:

I am using this function and as it is on admin site and a bit slow at admin site can is fine too, and admin will be only one so I can give it more memory.But I think limit memory more than 300M for one call is too much and still getting this:

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 

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

So is there way so that I can optimize above function? or I need to do some specific sort of indexing or algo optimization, or any other possible way? Or I just stop showing all parents and super parents of category (that is client demand to see hierarchy)? Or need to increase memory as I already worked on a directory and that was also slow at admin site so I guess they were just using more Memory?

任何建议将不胜感激.

Any advice will be appreciated.

这里是表架构,它具有parent_id,因此它作为递归关系起作用.

Here is that table schema, it has parent_id so it is working as recursive relation.

   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 ;

推荐答案

问题已解决,实际上存在一条记录,其parent_id指向其自己的cat_id作为主键.因此,这是指向自身的,在那种情况下,递归并没有结束.我使用了while循环,在这种情况下变为无限.

Issue is solved, there was actually a record whose parent_id was pointing to its own cat_id that is primary key. So that was pointing to itself and in that case that recursion simply wasn't ending. I used while loop and that become infinite in this case.

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

However during debugging I found this post that was helpful, http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ It provide better ways to handle same thing. In my scenario self join is useful as mentioned in this post.

这篇关于我该怎么做才能优化以下功能或减少内存消耗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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