PersistenceExtensions implementation performance issue

Issue [InfluxDB] Crashing influxDB 2.1 with a simpel rule: previousState · Issue #11878 · openhab/openhab-addons · GitHub revealed that the current implementation of some PersistenceExtensions operations is not efficient for InfluxDB 2 using flux query language and are problematic with databases with a lot of data.

The problem is that some pattern like sort(desc) limit 1 doesn’t play well because sort is not a push-down function (see here) and forces a sort of all data with a lot of data retrieving.

I’ve done an optimization here detecting the sort(desc) limit 1 pattern and changing for last() but the variant that wants a different value isn’t optimized because iterates with 1000 pagination.

Does anyone know why this strategy is used instead of using a limit 1 with a not equals filter criteria?

Also want to expose the problem because if new operations need to be developed it will be nice to try to avoid sort without limit 1 or a start date and possibly will be nice also to include persistence addons maintainers to discuss if the operators will play well in current query implementations.