数据库有300万条记录,以下测试都是在没有查询缓存的情况下进行的,直接上对比代码吧。
先看优化前:
$list = $this->model ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select();$list = collection($list)->toArray();
优化前语句:SELECT * FROM `fa_weblog_log` ORDER BY `fa_weblog_log`.`id` DESC LIMIT 2862790,10
优化前耗时:21.826222s
再看优化后:
$min_id = $this->model->where(['id' => ['gt', 0]])->order('id', 'asc')->value('id');$last_max_id = $min_id + $offset - 1;$list = $this->model ->where($where) ->where(['id' => ['gt', $last_max_id]]) ->order('id', 'asc') ->limit($limit) ->select();$list = collection($list)->toArray();
优化后语句:SELECT * FROM `fa_weblog_log` WHERE `id` > 2862780 ORDER BY `id` ASC LIMIT 10
优化后耗时:0.963421s
从优化前后对比可以看出查询速度上有了质的提升,有几个注意的地方:
1.必须用id asc排序,所以这里必须写死,不能再使用其他字段进行排序
2.也不能再使用关联查询了
继续优化让其支持id排序:
$min_id = $this->model->order('id', $order)->value('id');if ($order == 'desc') { $last_max_id = $min_id - $offset;} else { $last_max_id = $min_id + $offset;}$operator = ['asc' => 'egt', 'desc' => 'elt'];$list = $this->model ->where($where) ->where(['id' => [$operator[$order], $last_max_id]]) ->order('id', $order) ->limit($limit) ->select();$list = collection($list)->toArray();
优化后语句:SELECT * FROM `fa_weblog_log` WHERE `id` <= 2862772 ORDER BY `fa_weblog_log`.`id` DESC LIMIT 10
优化后耗时:0.963427s
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。