网站优化,敢各位大佬这个网站给多少钱能优化?
今天看到一个cms的网站很卡,老板叫我优化有以下,下面是具体的方法
/**
* 文章列表获取
*/
public function arcList()
{
// 定义全局查询条件
$map = []; // 将所有的查询条件封装到这个数组中
$site_id = 0;
if(Request::param('site_id')){
$site_id=Request::param('site_id');
}
$map[] = ['site_id', '=', $site_id];
// 搜索功能
$keywords = Request::param('keywords');
if ( !empty($keywords) ) {
$map[] = ['title', 'like', '%'.$keywords.'%'];
}
$cid = Request::param('cid');
$integral = Request::param('integral');
$attr = Request::param('attr');
if(Request::param('datebt')){
$datebt = Request::param('datebt');
$datearr=explode(" - ",$datebt);
$datebegin=strtotime($datearr['0']);
$dateend=strtotime($datearr['1']);
$map[] = ['updatetime','between',[$datebegin,$dateend]];
}
$cateorg=ArcCate::order('sort')->select()->toArray();
if(isset($cid)){
$cids=getChildsId($cateorg,$cid);
array_push($cids,$cid);//所有子类cID+自身cID
if($cids){
$map[] = ['cid','in',$cids];
}
}
if(isset($integral)) {
if ($integral != 'all') {
$map[] = ['integral', '=', $integral];
}
}
if(isset($attr)) {
if ($attr != 'all') {
$map[] = [$attr, '=', 1];
}
}
$map[]=['del','=',0];
// 定义分页参数
$limit = isset($_GET['limit']) ? $_GET['limit'] : 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
/**
$arcList = ArcModel::where($map)
-> alias('a')
-> join(['wy_cate' => 'c'], 'a.cid = c.id')
-> order('a.id', 'desc')
//-> field('a.id,a.cid,a.title,a.createtime,a.updatetime,a.click,c.name')
-> field(['a.id','a.cid','a.title','a.createtime','a.updatetime','a.click','c.name'])
-> page($page,$limit)
-> select();
* **/
$arcList=ArcModel::where($map)
->withoutField('content')
->withSum(['scoreTotal' => 'total'], 'score_in')
->withSum(['scoreForward' => 'forward'], 'score_in')
->withCount(['timesTotal' => 'times'])
->with(['arcCate'])
->order('id','desc')
->page($page,$limit)
->select();
//dd($arcList->toArray());
//$total = count($arcList);
$total=count(ArcModel::where($map)->select());
$result = array("code" => 0, "msg" => "查询成功", "count" => $total, "data" => $arcList);
return json($result);
// 设置模板变量
View::assign('arcList', $arcList);
// 渲染模板
View::fetch();
}
很难理解这是10多年开发的人写出来的代码,统计居然将所有的文章查询出来进行计数
$total=count(ArcModel::where($map)->select());
而且这分页跟没分页有啥区别?
// 定义分页参数
$limit = isset($_GET['limit']) ? $_GET['limit'] : 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
如果是我page=1,limit=1 那还好
那如果我 page=9999999999999999999999999999999999999999&limit=9999999999999999999999999999999999999999 敢问阁下又将如何应对?
不知道大家遇到这种情况应该报价多少?
下面说说我的看法:
1、请求的参数尽量用类型限制,防止用户恶意传参
$page = (int)request("page");
2、对于返回数据的条数,应该设置一个最大值,你永远不知道用户会给你传多大
public function getLimit(int $defaultLimit = 10,int $maxLimit=100)
{
$limit = (integer)request("limit", $defaultLimit);
if ($limit >= $maxLimit) {
$limit = $maxLimit;
}
return $limit;
}
当我们使用框架查询的时候,一般 paginate
已经携带查询的总条数了
$data = ModelName::latest("id")->paginate($this->getLimit());
return $this->resData($data->items(), $data->total())
3、关于关联查询或者统计,可通过模型关联的方式进行查询,这样看上去简单直了
$data = ModelName::with("user")->withCount("log")->paginate($this->getLimit());
看到100+的列表查询,我知道今晚又该加班了。。。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: