SQL optimisation for formtools pagination

This SQL tweak in certain database scenarios and reduce query execution time from 50+ seconds to under 1 second. The SQL is logically equivalent and I have confirmed for a complex case that the results are identical.

This branch was created from milestone-6-2-8 and can be merged automatically into p620, p630, and trunk.