提高codeigniter性能

时间:2014-12-12 10:35:56

标签: mysql performance codeigniter

我正在建立像ebay这样的电子商务网站。 我正在使用codeigniter。它太慢了。 我试过缓存,但仍然太慢了。 我知道我的代码中存在问题。有人请建议我如何改善加载时间或减少查询。

家庭控制器的索引功能

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

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

感谢。

1 个答案:

答案 0 :(得分:0)

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

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

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

但最大的瓶颈可能是数据库和Web服务器的性能。通常,如果您使用共享主机方案,则Web服务器和数据库服务器是独立的(虚拟)计算机,并且两者之间可能存在显着的网络延迟,此外,您将只能使用有限的RAM和CPU周期。 。如果托管速度是问题,那么你将需要至少投资一个VPS,并可能将你的数据库从MySQL转移到更加笨拙的东西。对于VPS以及MSSQL或Azure作为数据库,您可能每月要花费几百美元来托管费用。