Grouping a large number of entries I have imported data with 18000+ entries. It's a list of product models with brands associated to each. The product model is being used as the title and the brand field is plain text.

There are approximately 140 brands which I need to output as a dropdown.

I can't output this many entries and then try and group (and order them) on the template as it crashes the page.

This is code that would work if it didn't crash the page (and would take minutes to load the page even if it didn't).

{% set allEntries = craft.entries.section('solarWaterHeaterModels').order('brand asc').limit(null).find() %}

{% set allEntriesByBrand = allEntries|group('brand') %}

{% for brand, entriesInBrand in allEntriesByBrand %}

    {% for entry in entriesInBrand %}
        {% if loop.index == 1 %}
            &lt;option value="{{ entry.brand }}"&gt;{{ entry.brand }}&lt;/option&gt;
        {% endif %}
    {% endfor %}

{% endfor %}


Can anyone offer some guidance on how to approach this please?

Many thanks
First, based on your example code I don't think you actually need to loop on the entries – you could simply group them on the brand name, and then loop on the keys for the grouped array (the keys would be your brand names):

{% set brands = allEntries|group('brand')|keys %}

{% for brand in brands %}
     &lt;option value="{{ brand }}"&gt;{{ brand }}&lt;/option&gt;
{% endfor %}


So that's a slight optimization, but you'd still be pulling ~18k entries to print ~140 brand names and I'm not sure the above wouldn't still crash your server – so ideally you should probably use a different strategy altogether.

If you're comfortable w/ writing a little PHP, I'd suggest you scaffold a custom plugin – make sure that the Variables component is included, and add something like the below to the variables/YourPluginHandleVariable.php class:

public function getBrands()
{
    $query = craft()-&gt;db-&gt;createCommand();
    $query-&gt;selectDistinct('field_brand');
    $query-&gt;from('content');
    return array_filter($query-&gt;queryColumn());
}


In your template, you'd use this like so:

{% set brands = craft.yourPluginHandle.getBrands() %}

{% for brand in brands %}
     &lt;option value="{{ brand }}"&gt;{{ brand }}&lt;/option&gt;
{% endfor %}


Be sure to replace yourPluginHandle and YourPluginHandleVariable with the actual handle for your plugin.

Note that I haven't actually tested the above w/ as many as +18k rows, so even a distinct select query might not be performant enough. Of course, you can always cache the result.