在 Laravel 6 中查询列表(转换为数组),在遍历过程中需要赋值单条记录的额外字段,还需要使用单个对象,进而导致 N + 1 问题的处理
1、在 Laravel 6 中查询列表(转换为数组),为何必须转换为数组,原因是现阶段的需求是在遍历过程中,需要赋值单条记录的额外字段。此额外字段不存在于模型中。
1 2 3 4 5 6 7 8 | $wpThemes = Theme::select( 'id' , 'custom_name' , 'name' , 'is_original' , 'is_default' , 'created_at_gmt' , 'updated_at_gmt' , 'original_theme_name' ) ->orderBy( 'updated_at_gmt' , 'desc' ) ->get()->toArray(); foreach ( $wpThemes as $wpTheme ) { $wpThemeObject = $this ->get( $wpTheme [ 'id' ]); $wpTheme [ 'updatable' ] = $this ->isActionable( $saasThemeConfig , $wpThemeObject , 'updatable' ); } |
2、导致 N + 1 问题的出现,查看生成的 SQL 数量,为 79 条,额外增加了 25 条查询(由于使用了 with,实际增加了 50 条查询 SQL)。如图1
3、最终决定在 Laravel 6 中查询列表(不转换为数组),在遍历过程中,将单个对象转换为数组。
1 2 3 4 5 6 7 8 9 | $wpThemes = Theme::select( 'id' , 'custom_name' , 'name' , 'is_original' , 'is_default' , 'created_at_gmt' , 'updated_at_gmt' , 'original_theme_name' ) ->orderBy( 'updated_at_gmt' , 'desc' ) ->get(); foreach ( $wpThemes as $wpTheme ) { $wpThemeArray = $wpTheme ->toArray(); $wpThemeObject = $this ->get( $wpThemeArray [ 'id' ]); $wpThemeArray [ 'updatable' ] = $this ->isActionable( $saasThemeConfig , $wpTheme , 'updatable' ); } |
4、查看生成的 SQL 数量,为 22 条,之前实际增加了 50 条额外的查询 SQL,现在已经取消了。符合预期。如图2
近期评论