In AbstractBackendDataProvider size is called first, not optimal

My backend returns the total number of items together with the selected page of items. Therefor I have no separate getSize endpoint in my backend. As a first hack I just fetched a small page in sizeInBackEnd and extracted the total number of items from it. But that still makes unnecessary fetches and sorting in the underlying database. To get around this I fetch an arbitrary first page (page 0, size 50 as the size in query in sizeInBackEnd is Integer.MAX_VALUE) and cache it in sizeInBackEnd and when the first fetchFromBackEnd is called I return the cache and clear it.
It would be nice if sizeInBackEnd could be called after the first fetchFromBackEnd so I could store the total number of items and have sizeInBackEnd simply return it.
The response I get from my backend is something like this
GET /mydata?page=0&size=50
{
“totalItems”: 376,
“content”: [{…},{…},{…}]
}

Any suggestions how to make this more efficient and less hacky if I cannot change backend?

In Vaadin 23 and Vaadin 24 there is DataView API and with callback data providers the size count is now optional. So you can just define the fetch query and via DataView API set the count estimate.

The count query will always executed first. So you could simply cache the result you get from the count query.