python - Django - Cache vs QuerySet Filter for Server Efficiency? -
which 1 of these 2 more efficient on server resources?
lets have 1 "main" queryset use , have 3 additional filters display filtered results.
option 1:
q = entry.objects.filter(filter='filter') = q.filter(filter=a) b = q.filter(filter=b) c = q.filter(filter=c)
option 2.
lets assume can cache filtered results memcached can either run option 1 or can following:
q = entry.objects.filter(filter='filter') = cache.get('key_a') b = cache.get('key_b') c = cache.get('key_c')
is correct assumption option 1 1 database query , the filters applied django? or wrong , running option 1 database hit 4 times?
now if option 1 = 1 database hit, more efficient process a, b, c,
filters django (or rather web server) versus grabbing results cache?
i cache q
results technically have these 2 options:
option 1a:
get q
cache , apply queryset filters
q = cache.get('key_q') = q.filter(filter=a) b = q.filter(filter=b) c = q.filter(filter=c)
option 2a:
get cache
q = cache.get('key_q') = cache.get('key_a') b = cache.get('key_b') c = cache.get('key_c')
what correct, better approach django 1.8 running standard linux/apache/mysql stack on aws?
is correct assumption option 1 1 database query , the filters applied django?
no. filter(lookup=value)
calls used build sql where
clause.
or wrong , running option 1 database hit 4 times?
it's bit more complex... none of 4 statements in option 1 result in database hit, until start iterating on querysets. yes, have (at least - cf below) 1 query per queryset, of course.
so mostly, caching "unevaluated" queryset pretty useless since @ point nothing has been loaded database.
nb: "at least one" because can have n+1 queries if start following foreign keys on model instances (unless built queryset select_related
clause).
Comments
Post a Comment