Views: 8294
Last Modified: 12.09.2014

Query Optimization Example

Always keep queries to a minimum. For example, if the cycle makes a query to the IB element, it may be the time to think about minimization. Yes, it will take time, but your clients will appreciate it.

Incorrect:

foreach($arResult["ORDERS"] as $key => $val)
{
   foreach($val["BASKET_ITEMS"] as $vvval)
   {
      $rsEls = CIBlockElement::GetGetByID();
   }
}

Correct:

$arIDs = array();
foreach($arResult["ORDERS"] as $key => $val)
  {
    foreach($val["BASKET_ITEMS"] as $vvval)
      {
        $arIDs[] = $vvval["PRODUCT_ID"];
      }
  }
if(!empty($arIDs))
{
    $rsEls = CIBlockElement::GetList(array(), array("ID" => $arIDs));
    ...
}

foreach($arResult["ORDERS"] as $key => $val)
{
   foreach($val["BASKET_ITEMS"] as $vvval)
   {
      $arIDs[] = $vvval["PRODUCT_ID"];
   }
}

$rsEls = CIBlockElement::GetList(array(), array("ID" => $arIDs));
....

foreach($arResult["ORDERS"] as $key => $val)
{
   foreach($val["BASKET_ITEMS"] as $vvval)
   {
      //your code
   }
}

You actually make one query instead of tens or even hundreds.

Special Methods

If there is a special method for any change in the database, this method should be used instead of a more general method of database change.

A good example is the module of the e-store and work with an order: the payment order toggle can be changed using CSaleOrder::Update or CSaleOrder::PayOrder. PayOrder is preferable because it will invoke the appropriate handlers.

Even if you have to change multiple fields (of the same order) and payment toggle, first make a change using PayOrder and then update the remaining fields.



Courses developed by Bitrix24