Views: 2593 (Data available since 06.02.2017)
The main idea of optimization in terms of the amount of information and cache consists in selecting and storing in cache of only required data and not all the data.
Selecting all the data may require more time and significantly increase the cache size.
- Some API methods may generate excessive information. It is recommended that queries be limited to required data only early on.
$rs = CIBlockResult::GetNext(true, false);
// setting the parameter false permits limiting the selection to the transformed values of fields only.
$res = CIBlock::GetList(array(), array('TYPE'=>'catalog', 'SITE_ID'=>SITE_ID, 'ACTIVE'=>'Y'), false);
- Sometimes the method Fetch works faster than GetNext, but it does not make the data safe.
- Caching of required data only.
In the component itself the result of its operation $arResult shall be limited to the required data only.
For example, a component must display the element identifier and its name, and then only this element identifier and its name must be brought to cache:
If you perform any operations in result_modifier.php in order to transfer their result to component_epilog.php, keys in SetResultCacheKeys will also have to be installed. In this case, the result of the operations will also be cached.
foreach ($sAr as $key => $arElement)
$this->__component->arResult["IDS"] = $arElement["ID"];
Note: If no keys are installed in SetResultCacheKeys, then, by default, all the results of $arResult will be cached.
Links on the Subject:
- Cache size optimization.
If, for example, the size of the cache file exceeds 1MB, excessive caching might be under way. In this case, cached data should be analyzed. The more the cache file size is, the less efficient it is.
- Limiting the volume of requested information.
You can also enhance the performance by displaying information in parts.
Displaying all information at once, for example, list of news for the entire year, is not recommended. In most cases, it is not needed and extremely inefficient. Displaying just the latest news of the day is enough.
The system has several methods for limiting data selection by quantity. E.g., using a page navigation for the list of news, the selection can be limited to, say, 20 elements per page, and if the user goes to the next page, the next 20 elements will be requested and displayed.
nPageSize=20 of the array arNavStartParams permits displaying 20 elements, but it makes 2 database queries:
1. "COUNT" - obtaining the number of elements
2. "SELECT" - data selection proper
while the parameter
nTopCount=4 of the same array creates query 1-n as follows:
SELECT * FROM tbl LIMIT 4;
The use nTopCount is much more efficient, because it makes no additional query in order to obtain selection entries.
- If own component is being created and queries will be needed to obtain different data, it is recommended that an option should be introduced in the component settings which will determine the data to be obtained. This way, the data that are unnecessary at this very moment will not be requested each time.