【发布时间】: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