dougourang1856 2019-06-11 12:44
浏览 100
已采纳

如何使用<select>标签中嵌套的项目列表减少数据库查询的数量?

Laravel 5.8: I'm getting too much queries when I use nested list of items.

This is for menu in the View. I've done nested menu of items but it seems to me it takes too much queries(10 extra queries for my list of 30 items) Although I used "eager loads".

In the model:

public function children()
{
    return $this->hasMany(Item::class, 'parent_id', 'id');
}

Its Repository: (used eager loads [with])

public function getListOfItemTitles()
{
    $columns = ['id', 'parent_id', 'title'];

    $result = $this->startConditions()
        ->select($columns)
        ->with(['children:id,title,parent_id'])
        ->get();
    return $result;
}

And the view:

<select name="item_id" id="item_id" class="form-control" required>
    @foreach($itemList as $itemOption)
        @if($itemOption->parent_id == 1)
            <optgroup label="{{ $itemOption->title }}">
                @foreach($itemOption->children as $children)
                    <option value="{{ $children->id }}">
                        {{ $children->title }}
                    </option>
                    @foreach($children->children as $subchildren)
                        <option value="{{ $subchildren->id }}">
                            —{{ $subchildren->title }}
                        </option>
                    @endforeach
                @endforeach
            </optgroup>
        @endif
    @endforeach
</select>

Here are my queries:

select `id`, `parent_id`, `title` from `items` where `items`.`deleted_at` is null
select `id`, `title`, `parent_id` from `items` where `items`.`parent_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30) and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 4 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 8 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 12 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 16 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 20 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 26 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 27 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 28 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 29 and `items`.`parent_id` is not null and `items`.`deleted_at` is null
select * from `items` where `items`.`parent_id` = 30 and `items`.`parent_id` is not null and `items`.`deleted_at` is null

I'd be happy if you help me to decrease amount of queries. Any additional advice in this case would be very apreciated since I'm new in PhP & Laravel.

  • 写回答

1条回答 默认 最新

  • dsk88199 2019-06-11 13:10
    关注

    You must eager load the subschildren, too:

    $columns = ['id', 'parent_id', 'title'];
    
    $result = $this->startConditions()
        ->select($columns)
        ->with(['children:id,title,parent_id', 'children.children:id,title'])
        ->get();
    return $result;
    

    Consider the following example from this article: https://laravel-news.com/eloquent-eager-loading

    $posts = App\Post::with('author.profile')->get();
    $posts->map(function ($post) {
        return $post->author->profile;
    });
    

    Here you can see they use dot notation to fetch deep relationships using with()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊