我正在使用Code igniter框架工作,我发现页面的加载时间过长。其他页面是正常速度,当我使用探查器进行慢速页面时,我发现了以下结果:
**BENCHMARKS
Loading Time: Base Classes 0.0090
Controller Execution Time ( Home / Index ) 3.6712
Total Execution Time 3.6802
MEMORY USAGE
7,404,176 bytes
DATABASE: db202519_sdemo QUERIES: 244**
<?php
function getAllAd_bymotorcars($serach_data,$order,$limit = '',$offset = ''){
$twoMonths=strtotime("-3 months", time());
$this->db->cache_on();
$q = ' SELECT * FROM place_ad';
if(isset($serach_data['photos']) && $serach_data['photos'] !=""){
$q .= ' JOIN place_ad_gallery ON place_ad.id_place_ad = place_ad_gallery.id_place_ad';
}
$q .= ' WHERE place_ad.deleted = 0 AND place_ad.status = "active"';
$q .= ' AND place_ad.id_sub_categories = 1';
$q .= ' AND place_ad.id_categories = 1';
if(isset($serach_data['countries']) && $serach_data['countries'] !=""){
$q .= ' AND place_ad.id_countries = '.$serach_data['countries'];
}
if(isset($serach_data['cities']) && $serach_data['cities'] !=""){
$q .= ' AND place_ad.id_cities = '.$serach_data['cities'];
}
if(isset($serach_data['area']) && $serach_data['area'] !=""){
$q .= ' AND place_ad.id_area = '.$serach_data['area'];
}
if(isset($serach_data['sub_categories']) && $serach_data['sub_categories'] !=""){
$q .= ' AND place_ad.id_sub_categories = '.$serach_data['sub_categories'];
}
if(isset($serach_data['sub_categories2']) && $serach_data['sub_categories2'] !=""){
$q .= ' AND place_ad.id_sub_categories2 = '.$serach_data['sub_categories2'];
}
if(isset($serach_data['sub_categories3']) && $serach_data['sub_categories3'] !=""){
$q .= ' AND place_ad.id_sub_categories3 = '.$serach_data['sub_categories3'];
}
if((isset($serach_data['price_from']) && $serach_data['price_from'] !="") && (isset($serach_data['price_to']) && $serach_data['price_to'] !="")){
$q .= ' AND place_ad.price_aed BETWEEN '.$serach_data['price_from'].' AND '.$serach_data['price_to'];
} else{
if(isset($serach_data['price_from']) && $serach_data['price_from'] !=""){
$q .= ' AND place_ad.price_aed >= '.$serach_data['price_from'];
}
if(isset($serach_data['price_to']) && $serach_data['price_to'] !=""){
$q .= ' AND place_ad.price_aed <= '.$serach_data['price_to'];
}
}
if(isset($serach_data['adress_ad']) && $serach_data['adress_ad'] !=""){
$adress_ad=$serach_data['adress_ad'];
$q .= " AND place_ad.title LIKE '%$adress_ad%'";
}
if(isset($serach_data['age']) && $serach_data['age'] !=""){
$q .= ' AND place_ad.age = '.$serach_data['age'];
}
if(isset($serach_data['condition']) && $serach_data['condition'] !=""){
$q .= ' AND place_ad.condition = '.$serach_data['condition'];
}
if(isset($serach_data['warranty']) && $serach_data['warranty'] !=""){
$q .= ' AND place_ad.warranty = '.$serach_data['warranty'];
}
if(isset($serach_data['ads_posted']) && $serach_data['ads_posted'] !=""){
$date_to = date('Y-m-d');
$date_from = strtotime(date('Y-m-d'));
$case = $serach_data['ads_posted'];
switch($case) {
case 0:
$date_from = strtotime('0 days',$date_from);
break;
case 3:
$date_from = strtotime('-3 days',$date_from);
break;
case 7:
$date_from = strtotime('-7 days',$date_from);
break;
case 14:
$date_from = strtotime('-14 days',$date_from);
break;
case 30:
$date_from = strtotime('-30 days',$date_from);
break;
case 90:
$date_from = strtotime('-90 days',$date_from);
break;
case 190:
$date_from = strtotime('-190 days',$date_from);
break;
}
$date_from = date('Y-m-d',$date_from);
$q .= ' AND place_ad.created_date >= "'.$date_from.'" AND place_ad.created_date <= "'.$date_to.'"';
}
if(isset($serach_data['photos']) && $serach_data['photos'] !=""){
$q .= ' AND place_ad_gallery.image!="" ';
}
//$q .= ' GROUP BY place_ad.id_place_ad';
if(isset($serach_data['photos']) && $serach_data['photos'] !=""){
$q .= ' GROUP BY place_ad_gallery.id_place_ad';
}
$q .= ' ORDER BY '.$order;
if($limit != '')
$q .= ' LIMIT '.$limit.' OFFSET '.$offset;
$query = $this->db->query($q);
//echo $this->db->last_query();exit;
if($limit == '')
return $query->num_rows();
else
return $query->result_array();
}
?>
控制器中使用上述函数来获取所有这些信息。在通过函数时,页面面临加载问题。
如何提高此页面的加载速度?
答案 0 :(得分:0)
启用缓存服务。
$config['cache_query_string'] = FALSE;
$config['cache_path'] = 'set-a-path';
这是参考。 https://www.codeigniter.com/user_guide/libraries/caching.html