V
V
Vitaly2022-04-15 16:43:41
1C-Bitrix
Vitaly, 2022-04-15 16:43:41

How to “optimally” calculate the number of elements by property in Bitrix?

Good afternoon.
There is "Infoblock 2.0", with property "source" type list (list in $arSource), about 100k+ elements.
It is necessary to count the number of elements for each property from the list.

$arSource = ["google","yandex","mail","avito", "еще 10 элементов"]; 
    foreach ($arSource as $s)
        $uCnt[$s] = \Bitrix\Iblock\Elements\ElementApivatsmtsTable::getList(['filter' => ['source.VALUE'=>$s], 'count_total' => 1])->getCount();

An example of what I sketched - it works, but the sampling takes much longer than we would like (about 2 seconds).
Taking into account the fact that the number of elements will increase, I would like it to be faster. Can you suggest a more optimal sample for my case?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Gritsuk, 2022-04-21
@vcherepko

Make a query with grouping and counting. Example:

\Bitrix\Iblock\Elements\ElementCatalogTable::getList([
    "select" => [
        "BRAND_VALUE" => "BRAND.VALUE",
        "COUNT",
    ],
    "filter" => [
        "!BRAND.VALUE" => false
    ],
    "group" => [
        "BRAND.VALUE"
    ],
    "runtime" => [
        new \Bitrix\Main\ORM\Fields\ExpressionField('COUNT',  'COUNT(*)' )
    ]
])

B
Beeshop87, 2022-04-16
@Beeshop87

I would first do a getList and work with the result.
I heard that database queries in a cycle are not very good =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question