Skip to content

Commit deb5a7a

Browse files
committed
Micro-optimize paginations
1 parent 4bcd233 commit deb5a7a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

flask_sqlalchemy.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,15 @@ def paginate(self, page, per_page=20, error_out=True):
385385
items = self.limit(per_page).offset((page - 1) * per_page).all()
386386
if not items and page != 1 and error_out:
387387
abort(404)
388-
return Pagination(self, page, per_page, self.order_by(None).count(), items)
388+
389+
# No need to count if we're on the first page and there are fewer
390+
# items than we expected.
391+
if page == 1 and len(items) < per_page:
392+
total = len(items)
393+
else:
394+
total = self.order_by(None).count()
395+
396+
return Pagination(self, page, per_page, total, items)
389397

390398

391399
class _QueryProperty(object):

0 commit comments

Comments
 (0)