惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
小众软件
小众软件
量子位
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
博客园_首页
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Security Latest
Security Latest
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
Lohrmann on Cybersecurity
V
V2EX
P
Proofpoint News Feed
I
Intezer
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Help Net Security
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
L
LINUX DO - 热门话题
D
DataBreaches.Net
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
P
Proofpoint News Feed
NISL@THU
NISL@THU
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Security @ Cisco Blogs

博客园 - 斌哥tobin

Go工程选择开源分库分表中间件可用性测试 Golang解决fatal error: all goroutines are asleep - deadlock! Laravel配置Route调用artisan 研究微信红包分配算法之Golang版 解决IDEA提示Untrusted Server's certificate 证书不可用( Server's certificate is not trusted ) select * 和 select 字段的速度对比 Docker镜像拉取失败或超时的解决办法:添加国内镜像 php连接docker运行的mysql,显示(HY000/2002): Connection refused的解决办法 VirtualBox虚拟CentOS7共享文件夹 Windows7用VirtualBox虚拟Ubuntu共享文件夹的终极方式 PhpStorm添加PHP代码规范检查CodeSniffer(phpcs)和PHP代码静态分析工具Mess Detector(phpmd) php的三个常用判断函数 php计算字符串长度 mac brew 安装php扩展报错:parent directory is world writable but not sticky Mac iTerm2命令行快捷操作 System.Web.AspNetHostingPermission 类型的权限已失败 优化phpstorm运行卡顿问题! composer [ReflectionException] Class Fxp\Composer\AssetPlugin\Repository\NpmRepository does not exist - 斌哥tobin nginx1.8安装nginx_concat_module及400错误解决办法
Laravel Model查询结果的3种存储格式内存占用对比
斌哥tobin · 2020-09-21 · via 博客园 - 斌哥tobin

2020-09-21 15:15  斌哥tobin  阅读(807)  评论()    收藏  举报

PHP Laravel框架支持Model查询数据后可以有多种方式返回数据,对新手会造成一些困扰,比如数组Model对象、集合、纯数组
今天从内存占用的角度对比一下3种数据返回方式

按数组Model对象返回数据

  • 写法
TestModel::query()->where('field_name', $value)->limit(10)->get()->all();

按集合返回数据

  • 写法
TestModel::query()->where('field_name', $value)->limit(10)->get();

按纯数组返回数据

  • 写法
TestModel::query()->where('field_name', $value)->limit(10)->get()->toArray();

测试环境

PHP 7.2.28, Docker Compose, MacOS

对比数据

  • 按数量分别取10、100、1000、10000行数据来对比
返回格式 查询行数 查询结果内存占用
Model数组 10 433.2109K
array 10 457.5313K
Collection 10 433.2891K
Model数组 100 803.3984K
array 100 816.4688K
Collection 100 803.4766K
Model数组 1000 4.3761M
array 1000 4.2790M
Collection 1000 4.3762M
Model数组 10000 40.4700M
array 10000 39.2743M
Collection 10000 40.4701M

结论

  1. 数据量在100以内时,用Model数组占用内存还更少
  2. Collection总比Model数组多一点点是因为Collection集合多了一层对象包装存储Model数组,有点鸡肋,所以我很少用这种方式
  3. Model数组配置IDE辅助提示时开发效率更高,并且内存占用相比纯数组array相差不大,所以建议用Model数组方式

开发建议

从开发效率和维护角度我建议选择Model数组的开发方式
因为在PhpStorm这个IDE中,Model的查询方法中加入注释 @return static[] 就可以有IDE字段辅助
当然前提是在相应Model的注释中加入属性注释 @property,下方给出几个示例

Model @property 示例

/**
 * @property int $id 主键ID
 * @property string $order_sn 订单号
 */
class OrderInfoModel extends Model
{
    protected $table = 'order_info';
    public $timestamps = false;
      /**
      单例入口
      */
    public static function singleton()
    {
        return app(static::class);
    }
      /**
     * 查找订单
     * @param string $sn
     * @return static
     */
    public function findBySn($sn)
    {
        return static::query()->where('order_sn', $sn)->first();
    }
      /**
     * 查找订单列表
     * @return static[]
     */
    public function findListByPage($page, $pageSize)
    {
        return static::query()->forPage($page, $pageSize)->get()->all();
    }
}
//注意两个方法的 @return 差别

使用方法

  • 以一个分页列表接口为例
class OrderController extends Controller
{
      public function actionList(Request $request)
    {
            $page = $request->get('page', 1);
            $pageSize = $request->get('size', 10);
            $results = [];
            $list = OrderInfoModel::singleton()->findListByPage($page, $pageSize);
            if ($list) foreach ($list as $item) {
                  $results[] = ['order_sn' => $item->order_sn]; //在IDE中, 这个$item->order_sn是有辅助提示的,就是Model中的@property起的作用
            }
            //其他代码省略
      }
}