【问题标题】:Improving codeigniter performance提高 codeigniter 性能
【发布时间】:2014-12-12 10:35:55
【问题描述】:

我正在建立一个电子商务网站,例如 ebay。 我正在使用代码点火器。它太慢了。 我已经尝试过缓存,但仍然太慢。 我知道我的代码有问题。有人请建议我如何改善加载时间或减少查询。

家用控制器的索引功能

public function index(){
        $this->load->model('homemodel');
        $data['menu'] = $this->homemodel->GetMenu();
        $data['slider'] = $this->homemodel->GetSlider();
        $data['popular'] = $this->homemodel->GetPopular();
        $data['new'] = $this->homemodel->GetNew();
        $data['featured'] = $this->homemodel->GetFeatured();
        $data['reviewed'] = $this->homemodel->GetMostReviewed();
        $data['seller'] = $this->homemodel->GetBestSeller();
        $data['auction'] = $this->homemodel->GetAuction();
        $data['deal'] = $this->homemodel->GetDeals();
        $this->load->view('index',$data);
    }

家庭模型

    public function GetMenu(){
        $this->db->select("*");
        $this->db->order_by("parentID");
        $query = $this->db->get("category");
        $result = $query->result_array();
        $p=0;$i=0;$cat="0";
        foreach($result as $items){
        //$cat.= $items['parentID']."<br>";
                if($items['parentID']==$p){
                        $menu[$p][$i] = $items;
                        $i++;
                }else{
                        $i=0;
                        $p=$items['parentID'];
                        $menu[$p][$i] = $items;
                        $i++;
                }
        }
        return $menu;
    }



    public function GetPopular(){
        $this->db->select("*");
        $this->db->where("parentID","0");
        $query = $this->db->get("category");
        $result = $query->result_array();
        $i=1;$pop='';
        foreach($result as $items){
            $this->db->select("*");
            $this->db->where("product.master_cat",$items['categoryID']);
            $this->db->where("product.popular","1");
            $this->db->where("images.ref_type","0");
            $this->db->where("images.is_cover","1");
            $this->db->join("images","images.ref_id=product.product_id");
            $query2 = $this->db->get("product");
            $pop[$i] = $query2->result_array();
            $i++;
        }
        return $pop;
    }



    public function GetNew($cid=NULL){
        if($cid!=''){
    $query = $this->db->query("SELECT * FROM product JOIN `images` ON `images`.`ref_id`=`product`.`product_id` JOIN `product_reputation` ON `product_reputation`.`product_id`=`product`.`product_id` WHERE `product`.`category_id` IN (".$cid.") ORDER BY `product`.`product_id` DESC LIMIT 8");
    $res = $query->result_array();
    }else{
        $this->db->select("*");
        $this->db->order_by("product.product_id","DESC");
        $this->db->limit(8);
            $this->db->join("images","images.ref_id=product.product_id");
            $this->db->join("product_reputation","product_reputation.product_id=product.product_id");
            $query = $this->db->get("product");
            $res = $query->result_array();
        }
        return $res;
    }

    public function GetFeatured($cid=''){
        if($cid!=''){
    $query = $this->db->query("SELECT * FROM product JOIN `images` ON `images`.`ref_id`=`product`.`product_id` JOIN `product_reputation` ON `product_reputation`.`product_id`=`product`.`product_id` WHERE product.is_featured='1' and `product`.`category_id` IN (".$cid.") ORDER BY `product`.`product_id` DESC LIMIT 8");
    $res = $query->result_array();
    }else{
    $this->db->select("*");
    $this->db->where("product.is_featured","1");
    $this->db->order_by("product.product_id","DESC");
    $this->db->limit(8);
        $this->db->join("images","images.ref_id=product.product_id");
        $this->db->join("product_reputation","product_reputation.product_id=product.product_id");
        $query = $this->db->get("product");
        $res = $query->result_array();
        }
        return $res;
    }

    public function GetMostReviewed($cid=''){
        if($cid!=''){
    $query = $this->db->query("SELECT * FROM product JOIN `images` ON `images`.`ref_id`=`product`.`product_id` JOIN `product_reputation` ON `product_reputation`.`product_id`=`product`.`product_id` WHERE product.is_featured='1' and `product`.`category_id` IN (".$cid.") ORDER BY product_reputation.total_review DESC LIMIT 8");
    $res = $query->result_array();
    }else{
    $this->db->select("*");
    $this->db->where("product.is_featured","1");
    $this->db->order_by("product_reputation.total_review","DESC");
    $this->db->limit(8);
        $this->db->join("images","images.ref_id=product.product_id");
        $this->db->join("product_reputation","product_reputation.product_id=product.product_id");
        $query = $this->db->get("product");
        $res = $query->result_array();
        }
        return $res;
    }

    public function GetBestSeller($cid=''){
        if($cid!=''){
    $query = $this->db->query("SELECT * FROM product JOIN `images` ON `images`.`ref_id`=`product`.`product_id` JOIN `seller_reputation` ON seller_reputation.seller_id=product.product_owner WHERE product.is_featured='1' and `product`.`category_id` IN (".$cid.") ORDER BY seller_reputation.total_review DESC LIMIT 8");
    $res = $query->result_array();
    }else{
    $this->db->select("*");
    $this->db->where("product.is_featured","1");
    $this->db->order_by("seller_reputation.total_review","DESC");
    $this->db->limit(8);
        $this->db->join("images","images.ref_id=product.product_id");
        $this->db->join("seller_reputation","seller_reputation.seller_id=product.product_owner");
        $query = $this->db->get("product");
        $res = $query->result_array();
        }
        return $res;
    }

这些是示例方法。请提供一些建议。

谢谢。

【问题讨论】:

  • codeigniter 缓存完全绕过了数据库,所以如果您真的尝试过缓存并且速度太慢,那么它是您的 Web 服务器的问题,而不是您的模型方法。

标签: mysql performance codeigniter


【解决方案1】:

有很多事情可能会导致性能不佳。您的数据库表中有多少条记录,它们是否被正确索引。 SELECT * 将产生性能开销,因为数据库查询管理器必须转到元数据以找出查询应返回哪些列。如果您直接指定列,您将节省一些时间。

您的 GetPopular() 函数正在获取主记录列表,然后为主列表中的每条记录运行子查询。这可能会非常慢,具体取决于主列表中的记录数。如果您在单个查询中获得类别和热门产品,您将获得更好的性能(使用 LEFT JOIN 以避免删除没有热门产品的类别)。如果一个类别有多个受欢迎的产品,您将需要设计一种获取不同类别的方法:这可能只是一个单独的查询,或者迭代可用的类别/产品数组可能更快

您可能会在此屏幕上自动加载一大堆不需要的内容。不要将所有帮助程序和库设置为自动加载。

但最大的瓶颈可能是数据库和 Web 服务器的性能。通常,如果您采用共享托管计划,则 Web 服务器和数据库服务器是独立的(虚拟)机器,两者之间可能存在明显的网络延迟,而且您只能使用有限的 RAM 和 CPU 周期.如果托管速度是问题,那么您至少需要投资一个 VPS,并且可能将您的数据库从 MySQL 移到更繁琐的地方。对于 VPS 和 MSSQL 或 Azure 作为数据库,您每月可能需要几百美元的托管费用。

【讨论】:

  • 非常感谢。它真的很棒。
猜你喜欢
  • 2018-03-01
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 2021-12-18
  • 2011-05-07
  • 2016-04-19
相关资源
最近更新 更多