您现在的位置是:首页 > cms教程 > Ecshop商城教程Ecshop商城教程

ecshop百万级数据性能优化提升访问速度

灵雁2025-02-06Ecshop商城教程已有人查阅

导读如果ecshop的商品数达到几万,十几万的时候,如果首页没有缓存,第一次访问的时候,你会发现其慢无比,原因就是清空了Cache后或者没有Cache的情况下

如果ecshop的商品数达到几万,十几万的时候,如果首页没有缓存,第一次访问的时候,你会发现其慢无比,原因就是清空了Cache后或者没有Cache的情况下,ECshop会Bulid一些Cache数据,导致访问很慢,但我们有时候后台编辑类目或者其他的,经常会触发清空Cache,所以首页首次访问也成了问题。
在大数据量的情况下,影响首页速度较大的就是推荐的Best、Hot、New Item的数据Bulid,它会把所有的复合条件的商品都会读一遍,然后存到/temp/static_caches /recommend_goods.php这个文件下,有时候会达到10M或者数十M,其实我们并不需要所有的商品都Bulid进去,因为这个缓存只用在 首页和Category页的调用,有点浪费。(P.S 因为Category访问本来就比较慢,所以我把热卖商品在Category的展示屏蔽了,所以只剩首页调用)
首页展示的时候,三种类型Best、Hot、New只展示10个商品(我没有选择展示多个类目),所以这个上面有很大的优化空间。
打开include目录下的lib_goods.php文件,找到function get_recommend_goods() 函数,原始的大概是:
function get_recommend_goods($type = '', $cats = '')
{
if (!in_array($type, array('best', 'new', 'hot')))
{
return array();
}
//取不同推荐对应的商品
static $type_goods = array();
if (empty($type_goods[$type]))
{
//初始化数据
$type_goods['best'] = array();
$type_goods['new'] = array();
$type_goods['hot'] = array();
$data = read_static_cache('recommend_goods');
if ($data === false)
{
$sql = 'SELECT g.goods_id, g.is_best, g.is_new, g.is_hot, g.is_promote, b.brand_name,g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND (g.is_best = 1 OR g.is_new =1 OR g.is_hot = 1)'.
' ORDER BY g.sort_order, g.last_update DESC';
$goods_res = $GLOBALS['db']->getAll($sql);
//定义推荐,较新,热门,促销商品
$goods_data['best'] = array();
$goods_data['new'] = array();
$goods_data['hot'] = array();
$goods_data['brand'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
if ($data['is_best'] == 1)
{
$goods_data['best'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
if ($data['is_new'] == 1)
{
$goods_data['new'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
if ($data['is_hot'] == 1)
{
$goods_data['hot'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
if ($data['brand_name'] != '')
{
$goods_data['brand'][$data['goods_id']] = $data['brand_name'];
}
}
}
write_static_cache('recommend_goods', $goods_data);
}
else
{
$goods_data = $data;
}
$time = gmtime();
$order_type = $GLOBALS['_CFG']['recommend_order'];
//按推荐数量及排序取每一项推荐显示的商品 order_type可以根据后台设定进行各种条件显示
static $type_array = array();
$type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot');
if (empty($type_array))
{
foreach($type2lib as $key => $data)
{
if (!empty($goods_data[$key]))
{
$num = get_library_number($data);
$data_count = count($goods_data[$key]);
$num = $data_count > $num ? $num : $data_count;
if ($order_type == 0)
{
//usort($goods_data[$key], 'goods_sort');
$rand_key = array_slice($goods_data[$key], 0, $num);
foreach($rand_key as $key_data)
{
$type_array[$key][] = $key_data['goods_id'];
}
}
else
{
$rand_key = array_rand($goods_data[$key], $num);
if ($num == 1)
{
$type_array[$key][] = $goods_data[$key][$rand_key]['goods_id'];
}
else
{
foreach($rand_key as $key_data)
{
$type_array[$key][] = $goods_data[$key][$key_data]['goods_id'];
}
}
}
}
else
{
$type_array[$key] = array();
}
}
}
//取出所有符合条件的商品数据,并将结果存入对应的推荐类型数组中
$sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
"promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
"LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
"ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ";
$type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
$type_merge = array_unique($type_merge);
$sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
$sql .= ' ORDER BY g.sort_order, g.last_update DESC';
$result = $GLOBALS['db']->getAll($sql);
foreach ($result AS $idx => $row)
{
if ($row['promote_price'] > 0)
{
$promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
}
else
{
$goods[$idx]['promote_price'] = '';
}
$goods[$idx]['id'] = $row['goods_id'];
$goods[$idx]['name'] = $row['goods_name'];
$goods[$idx]['brief'] = $row['goods_brief'];
$goods[$idx]['brand_name'] = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
$goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
$goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
$goods[$idx]['market_price'] = price_format($row['market_price']);
$goods[$idx]['shop_price'] = price_format($row['shop_price']);
$goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
if (in_array($row['goods_id'], $type_array['best']))
{
$type_goods['best'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['new']))
{
$type_goods['new'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['hot']))
{
$type_goods['hot'][] = $goods[$idx];
}
}
}
return $type_goods[$type];
}
我的思路就是不读取所有的商品信息,而是Best、Hot、New够用展示就行了,所以把代码调整了一下,限制了Bulid的商品数,去掉了一些我不需要的Join表,当然大家不一定要照着我做,我讲的都是思路,看自己的实际情况,要有点程序基本功。
更改后的代码如下:
function get_recommend_goods($type = '', $cats = '')
{
if (!in_array($type, array('best', 'new', 'hot')))
{
return array();
}
//取不同推荐对应的商品
static $type_goods = array();
if (empty($type_goods[$type]))
{
//初始化数据
$type_goods['best'] = array();
$type_goods['new'] = array();
$type_goods['hot'] = array();
$data = read_static_cache('recommend_goods');
if ($data === false)
{
$sql = 'SELECT g.goods_id, g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' WHERE (g.is_best = 1)'.
' ORDER BY g.goods_number DESC limit 50';
$goods_res = $GLOBALS['db']->getAll($sql);
$goods_data['best'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
$goods_data['best'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
}
$sql = 'SELECT g.goods_id, g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' WHERE (g.is_new = 1 and g.is_best = 0 and g.is_hot = 0)'.
' ORDER BY g.goods_id DESC limit 50';
$goods_res = $GLOBALS['db']->getAll($sql);
$goods_data['new'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
$goods_data['new'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
}
$sql = 'SELECT g.goods_id, g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' WHERE (g.is_hot = 1 and g.is_best = 0)'.
' ORDER BY g.goods_number DESC limit 50';
$goods_res = $GLOBALS['db']->getAll($sql);
$goods_data['hot'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
$goods_data['hot'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
}
write_static_cache('recommend_goods', $goods_data);
}
else
{
$goods_data = $data;
}
$time = gmtime();
$order_type = $GLOBALS['_CFG']['recommend_order'];
//按推荐数量及排序取每一项推荐显示的商品 order_type可以根据后台设定进行各种条件显示
static $type_array = array();
$type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot');
if (empty($type_array))
{
foreach($type2lib as $key => $data)
{
if (!empty($goods_data[$key]))
{
$num = get_library_number($data);
$data_count = count($goods_data[$key]);
$num = $data_count > $num ? $num : $data_count;
if ($order_type == 0)
{
//usort($goods_data[$key], 'goods_sort');
$rand_key = array_slice($goods_data[$key], 0, $num);
foreach($rand_key as $key_data)
{
$type_array[$key][] = $key_data['goods_id'];
}
}
else
{
$rand_key = array_rand($goods_data[$key], $num);
if ($num == 1)
{
$type_array[$key][] = $goods_data[$key][$rand_key]['goods_id'];
}
else
{
foreach($rand_key as $key_data)
{
$type_array[$key][] = $goods_data[$key][$key_data]['goods_id'];
}
}
}
}
else
{
$type_array[$key] = array();
}
}
}
//取出所有符合条件的商品数据,并将结果存入对应的推荐类型数组中
$sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, g.shop_price, ' .
"promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
" ";
$type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
$type_merge = array_unique($type_merge);
$sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
$sql .= ' ORDER BY g.goods_number DESC';
$result = $GLOBALS['db']->getAll($sql);
foreach ($result AS $idx => $row)
{
if ($row['promote_price'] > 0)
{
$promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
}
else
{
$goods[$idx]['promote_price'] = '';
}
$goods[$idx]['id'] = $row['goods_id'];
$goods[$idx]['name'] = $row['goods_name'];
$goods[$idx]['brief'] = $row['goods_brief'];
$goods[$idx]['brand_name'] = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
$goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
$goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
$goods[$idx]['market_price'] = price_format($row['market_price']);
$goods[$idx]['shop_price'] = price_format($row['shop_price']);
$goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
if (in_array($row['goods_id'], $type_array['best']))
{
$type_goods['best'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['new']))
{
$type_goods['new'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['hot']))
{
$type_goods['hot'][] = $goods[$idx];
}
}
}
return $type_goods[$type];
}
这样改了以后,Bulid的静态文件应该只有13K左右,速度大大加快。

本文标签:

很赞哦! ()

相关源码

  • (自适应)工业机械设备产品介绍免费pbootcms源码下载本网站模板基于PbootCMS内核精心开发,为机械设备与工业产品制造企业量身打造。设计充分考量行业特性,突出产品展示与技术实力呈现查看源码
  • 帝国cms7.5奇闻异事末解之谜模板免费下载带数据本模板基于帝国CMS7.5系统开发,为神秘现象、未解之谜类主题网站设计。包含完整的PC端、移动端及百度MIP站同步生成功能,内置火车头采集规则模块,可快速采集目标站内容资源。整体设计风格神秘大气,符合主题定位。查看源码
  • (PC+WAP)蓝色不锈钢簧线金属制品营销型pbootcms网站模板本模板基于PbootCMS内核开发,为不锈钢及金属制品企业量身打造。采用响应式设计,适配PC与移动设备,提供统一后台管理体验,数据实时同步更新。查看源码
  • (自适应响应式)装修装潢设计公司网站源码下载本模板为装修设计企业打造,采用PbootCMS内核开发,整体设计突出空间美学与功能性结合。首页采用大图轮播展示工程案例,服务项目模块支持三维效果展示,呈现装修设计企业的专业形象与技术实力。查看源码
  • (自适应)英文绿色精密模具零件加工五金零件pbootcms外贸网站模板本模板基于PbootCMS开发,为五金零件、精密模具加工等英文外贸企业设计。采用响应式布局,适配各类移动设备,是五金零件企业开展国际贸易的专业展示平台。查看源码
  • (自适应响应式)刷卡机POS机无线支付设备pbootcms网站源码下载本模板基于PbootCMS系统开发,为支付终端设备企业设计,特别适合POS机、移动支付终端、刷卡设备等金融科技产品展示。采用响应式布局技术,确保各类支付终端产品在不同设备查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐