1 year ago

Best way to bisect two large collections

Posted 1 year ago by nexxai

I have a very large collection (several thousand items), and given a much smaller collection of similar items (let's say 5-20 of them), I need to find and select them for further processing.

Currently I do it like this:

        // This is the list of 5-20
        $new_items = Item::where('prepared', 0)

        // Get existing items..
        $all_items = Item::all();

        // and figure out which items haven't been shown above
        // to create a bisected/diffed collection of just them
        $remaining_items = $all_items->diffAssoc($new_items);

In the view I've got:

@foreach ($stuffs as $stuff)
<div class="row">
    <div class="form-group col-md-8">
        <label for="name[{{$stuff->id}}]">{{$stuff->name}}</label>

        <select class="dropdown-autocomplete-menu form-control" id="name[{{$stuff->id}}]" name="name[{{$stuff->id}}]">
            <option disabled selected>
                Select the item

            <optgroup label="Detected items">
                @foreach ($remaining_items as $item)
                <option value="{{ $item->id }}">
                    {{ $item->full_name }}

            <optgroup label="Other items">
                @foreach ($all_items as $item)
                <option value="{{ $item->id }}">
                    {{ $item->full_name }}

            <option value="-999">Not listed</option>

But if I understand it correctly (and I'm still very much a novice), this is the N+1 problem (or possibly much worse). Is there a way to more efficiently generate the view so that it doesn't take so long to render the page?

Please sign in or create an account to participate in this conversation.