Foreach loop scalability suggestion

Foreach loop scalability suggestion

Hello people,

In foreach loop is this compulsory to set start index and Maximum iterations? Because in my application only for three foreach loops giving this scalability suggestion. (Please check the screenshot).

I am using these foreach loops after query refresh. So is this necessary to set start index?

Suraj Borade
You never have to set those values and I can't think of the last time I did set them.  Just leave them blank and you'll be fine, unless you have a particular reason to start or stop at a particualr place which is rare in my opinion.

Hope this helps,
Hi Suraj.
It is a warning, and therefore not necessary. But if you leave these properties empty, an important optimization will not take place.

First, let me clarify what this warning is all about. You are fetching a query from the database, possibly with thousands of results. Then, you are presenting it on a paginated table records (or some other paginated widget). That means that you don't really need to fetch from the database all of those thousands of results - you simply need the current page that will be presented to the user.

The OutSystems platform performs this optimization automagically. The developer doesn't even have to think about it, and the app will be optimized for pagination. So everyone is happy.

Until you put a foreach iterating the query. Now the platform is unsure if your business logic depends on iterating all of those thousands of results, or simply iterating the results that are paginated. So, like any optimization, it falls back to the pessimistic hypotesis - that you really must iterate all results. But, in doing so, it generates the warning you see.

If you follow the instructions on the warning, your foreach will be bounded to the same records that will be shown in the paginated widget. And that's a way of telling the platform that your foreach must only operate in the paginated set, so it could turn the optimizations on again.

Of course, if your table isn't going to grow very large, you can indeed ignore this warning. But for large tables, an unbounded foreach will hurt the performance of your page by orders of magnitude.