This blog post is about the defaultCountMode of the ODataModel(v2). We will cover all different modes for retrieving the count of collections. Here we look at the performance as well as the necessary implementations in the backend. Furthermore, we show how the defaultCountMode can be defined or changed at runtime.
defaultCountMode
There are different modes for retrieving the count information of an GET_ENTITYSET request:
- None
- Request
- Inline/InlineRepeat
None
![](https://janschulz.info/wp-content/uploads/2023/10/defaultCountMode_None_Timing-1.png)
The best performance of a GET_ENTITYSET request is achieved with defaultCountMode None
as the count is not requested from the server…😁 Use this defaultCountMode if the count information does not provide any added value in the application.
Request
![](https://janschulz.info/wp-content/uploads/2023/10/defaultCountMode_Request_Timing.png)
When using the defaultCountMode Request
the count is retrieved by sending a seperate $count request before requesting data. In simple words the GET_ENTITYSET implementation is executed twice, which means worse performance compared to the other options. The performance can be optimized by creating a seperate implementation for determining the count.
METHOD XXX_GET_ENTITYSET. IF io_tech_request_context->has_count( ). SELECT COUNT(*) FROM sflight INTO es_response_context-count. ELSE. SELECT * FROM sflight INTO et_entityset. ENDIF. ENDMETHOD.
Inline/InlineRepeat
![](https://janschulz.info/wp-content/uploads/2023/10/defaultCountMode_Inline_Timing.png)
With the defaultCountMode Inline
or InlineRepeat
the count is retrieved by adding $inlinecount=allpages
to data requests. Via Inline
the count will be determined once. Via InlineRepeat
the count will be determined every data request, regardless of whether the information is already available. In contrast to the countMode Request
the count will be not determined until you create a seperate implementation for determining the inlinecount in the corresponding GET_ENTITYSET method.
METHOD XXX_GET_ENTITYSET. IF io_tech_request_context->has_inlinecount( ). es_response_context-inlinecount = lines( lt_entityset ). ELSE. CLEAR es_response_context-count. ENDIF. ENDMETHOD.
Ways to set/switch the defaultCountMode
Now, that the difference between the different countModes has been clarified, the question arises as to how you can switch between them.
Model Segregation
The defaultCountMode for a model is defined in the manifest.json.
"models": { "ZHR_XXX_SRV": { "dataSource": "ZHR_XXX_SRV", "settings": { "defaultCountMode": "Inline" } } }
If there is an entityset that is used for a table the count is essential. At another entityset the count may does not add any value, but leads to massive performance decrease. To get the best out of both we can define another model with the same dataSource but different defaultCountMode. This approach is perfect for Overview Pages.
"models": { "ZHR_XXX_SRV_INLINE": { "dataSource": "ZHR_XXX_SRV", "settings": { "defaultCountMode": "Inline" } }, "ZHR_XXX_SRV_NOCOUNT": { "dataSource": "ZHR_ESS_OVP_SRV", "settings": { "defaultCountMode": "None" } } }
XML Binding
When binding a aggregation in a xml view you can define a countMode. For all possible parameters check out the sap.ui.model.odata.v2.ODataListBinding
.
items="{ path: '/MyEntitySet', parameters: { countMode: 'None' } }"
setDefaultCountMode
Apart from the other options the method setDefaultCountMode
can be used. Keep in mind that this method does not modify the count mode for existing list bindings. Only bindings created afterwards will use the new mode.