重构代码,解决商城加载太慢总结
商城加载太慢,分析原因。
1.图片大,加载慢
2.数据查询多,加载慢
处理一,通过lazyload来延迟图片加载,效果一般。
处理二,重构查询
原来的查询,分类获取商城数据,每多一个分类,就要多一次数据查询。这个很耗费时间。
foreach ($this->category as $k => $v) {
$where = array();
$content_arr = unserialize($mallCategoryModel->where(array(
'id' => $v['id']
))->getField('content'));
if (empty($content_arr)) {
$content_arr = array();
}
$content_str = implode(',', $content_arr);
if ($v['type'] == 1) { // 商铺
$where['a.status'] = 1;
$where['a.id'] = array(
'in',
$content_str
);
$storelist = M()->table('sh_store a')->join('sh_category b on a.category_id = b.id')->join('sh_store_shop c on a.id = c.store_id and c.member_id =' . $this->member_id)->where($where)->field('a.id as store_id,a.token,a.name as store_name,a.topbg,a.intro,a.logo,a.category_id,b.name as category_name,c.id as shop_id')->order('find_in_set(a.id,"' . $content_str . '")')->select();
$categoryData[$k]['storelist'] = $storelist;
} else { // 商品
$where['a.status'] = 1;
$where['b.status'] = 1;
$where['b.id'] = array(
'in',
$content_str
);
$goodslist = M()->table('sh_store a')->join('sh_goods b on b.store_id = a.id')->join('sh_store_shop c on a.id = c.store_id and c.member_id=' . $this->member_id)->where($where)->field('a.name as store_name,a.token,b.id as goods_id,b.price,b.oprice,b.name as goods_name,b.logoimg,b.salecount,b.fakemembercount,c.id as shop_id')->limit('10')->order('find_in_set(b.id,"' . $content_str . '")')->select();
$categoryData[$k]['goodslist'] = $goodslist;
}
}
慢就慢在要多次查询数据库,分类越多,查询次数越多。
重构思路,一次获取所有数据,分类进行整合。
// 重构提升加载速度
$store_content_str = $goods_content_str = '';
foreach ($this->category as $k => $v) {
$content_arr = unserialize($mallCategoryModel->where(array(
'id' => $v['id']
))->getField('content'));
if (empty($content_arr)) {
$content_arr = array();
}
$content_arr = array_slice($content_arr, 0, 6); //获取每组分类的前6条数据
$content_str = implode(',', $content_arr);
if ($v['type'] == 1) { // 店铺
if ($store_content_str == '') {
$store_content_str .= $content_str;
} else {
$store_content_str .= ',' . $content_str;
}
}
if ($v['type'] == 2) { // 店铺
if ($goods_content_str == '') {
$goods_content_str .= $content_str;
} else {
$goods_content_str .= ',' . $content_str;
}
}
}
// 获取所有自定义商品
$wheregoods = array();
$wheregoods['a.status'] = 1;
$wheregoods['b.status'] = 1;
$wheregoods['b.id'] = array(
'in',
$goods_content_str
);
$goodslist = M()->table('sh_store a')->join('sh_goods b on b.store_id = a.id')->where($wheregoods)->field('a.name as store_name,a.token,b.id as goods_id,b.price,b.oprice,b.name as goods_name,b.logoimg,b.salecount,b.fakemembercount')->select();
// 获取所有自定义商铺
$wherestore = array();
$wherestore['a.status'] = 1;
$wherestore['a.id'] = array(
'in',
$store_content_str
);
$storelist = M()->table('sh_store a')->join('sh_category b on a.category_id = b.id')->join('sh_store_shop c on a.id = c.store_id and c.member_id =' . $this->member_id)->where($wherestore)->field('a.id as store_id,a.token,a.name as store_name,a.topbg,a.intro,a.logo,a.category_id,b.name as category_name,c.id as shop_id')->select();
// 重新组装,获取相应的店铺或商品
foreach ($this->category as $k => $v) {
$content_arr = unserialize($mallCategoryModel->where(array(
'id' => $v['id']
))->getField('content'));
if (empty($content_arr)) {
$content_arr = array();
}
$content_arr = array_slice($content_arr, 0, 6); // 获取前六条数据
if ($v['type'] == 1) { // 店铺
if (count($content_arr) > 0) { // 获取相应的store数据
$storedata = array();
foreach ($content_arr as $k_store => $v_store) {
foreach ($storelist as $k_s_list => $v_s_list) {
if ($v_s_list['store_id'] == $v_store) {
$storedata[] = $v_s_list;
continue 2;
}
}
}
}
$categoryData[$k]['storelist'] = $storedata;
} else { // 商品
if (count($content_arr) > 0) { // 获取相应的goods数据
$goodsdata = array();
foreach ($content_arr as $k_goods => $v_goods) {
foreach ($goodslist as $k_g_list => $v_g_list) {
if ($v_g_list['goods_id'] == $v_goods) {
$goodsdata[] = $v_g_list;
continue 2; // 跳出两层,直接到达content_arr的foreach中,if也算一层
}
}
}
}
$categoryData[$k]['goodslist'] = $goodsdata;
}
}
减少了查询次数,增多了php对数据的处理。综合评定,重构后,速度提升了很多。