Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!flask/bin/python
import sys
from time import time
from app import database
from app.modules.user_manager import models as user
# Query once, filter each time
t = time()
q = database.session.query(user.User)
for i in range(10000):
# r = database.session.query(user.User).filter(
# user.User.username == 'brandonm').all()
r = q.filter(user.User.username == 'brandonm').all()
for result in r:
assert result.display == 'Brandon Morris'
print('New query each time: {:.2f}'.format(time() - t))
# Query every time
t = time()
q = database.session.query(user.User)
for i in range(10000):
# r = database.session.query(user.User).filter(
# user.User.username == 'brandonm').all()
r = q.filter(user.User.username == 'brandonm').all()
for result in r:
assert result.display == 'Brandon Morris'
print('New filter each time: {:.2f}'.format(time() - t))
# Query once, dict lookup
t = time()
all = database.session.query(user.User).all()
name_to_display = dict()
for user in all:
name_to_display[user.username] = user.display
for i in range(10000):
assert name_to_display['brandonm'] == 'Brandon Morris'
print('Cache lookup: {:.4f}'.format(time() - t))
@BrandonLMorris
Copy link
Author

BrandonLMorris commented Sep 16, 2016

Running this on my machine, I got the following results:

New query each time: 7.56
New filter each time: 7.57
Cache lookup: 0.0027

So it seems SQLAlchemy doesn't do any kind of caching for its queries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment