I am not entirely sure of your concern. Does this mean that you will return a large data set, so will large lists be processed in memory?
If so, you can do it in the SQL statement if you are not too worried about portability; that is, if you use MySQL, you can do:
Item.query.order_by(func.rand()).offset(20).limit(10).all()
Or, in PostgreSQL:
Item.query.order_by(func.random()).offset(20).limit(10).all()
Other databases have similar mechanisms, so the function you call will depend on what you are aiming for. And, of course, if you are trying to write a general-purpose application that can run on any of the SQLAlchemy components, you might have to stick with the above example.
source share