性能优化
一、MySQL查询优化
1. 合理设置索引;
order by 之后的排序字段最好有索引。
2.分页的时候要考虑limit
limit offset, num,要合理设置offset的值,注意limit 0, 10和limit 2000000, 10, 后者需要查询2000010条数据,并把前2000000扔掉,效率很慢。
通过查询id来优化,或
select * from wl_tagindex where byname=’f’ order by id limit 300000,10 // 优化 select * from ( select id from wl_tagindex where byname='f' order by id limit 300000,10 ) a left join wl_tagindex b on a.id=b.id
参考https://www.cnblogs.com/radphp/p/3380016.html
3. 关联查询null时,使用if 或 case when来优化
4. 数据库设计时尽量指定id AUTO_INCREMENT, PRIMARY KEY, 尽量使用NOT NULL DEFAULT 'value'
5. 单条查询使用limit(1), 避免全表扫描
6. 不要使用select * 来查询
7. 尽量不要使用or来查询
具体参考
https://www.cnblogs.com/ideacore/p/6423745.html
https://blog.csdn.net/sinat_23080035/article/details/52802569
二、PHP代码优化
1. 使用static静态数组来优化,减少不必要的数据库查询
public function getStoreName($store) { static $storeList = []; if (isset($storeList[$store])) { return $storeList[$store] } $storeName = $this->dao->select('name')->from(TABLE_STORE)->where('id')->eq($store)->fetch(); $storeList[$store] = $storeName; return $storeName; }
2. 尽量别在foreach中查询数据库
在foreach外一次性获取到所需要的数据,在foreach中赋值即可。
具体参考
https://segmentfault.com/a/1190000009442044
https://www.awaimai.com/1050.html
——

浙公网安备 33010602011771号