Performance with Django querysets -


i know querysets lazy not sure how django behave in case. let's want print 2 variables user_tokens , user_last_token. there performance difference between these examples:

example a

@classmethod def get_all_tokens(cls, user):     return cls.objects.filter(user=user, usage=cls.phone_validation)  @classmethod def get_last_token(cls, user):     return cls.get_all_tokens(user).latest('created_at')  @classmethod def play_with_tokens(cls, user):     user_tokens = cls.get_all_tokens(user, true)     user_last_token = cls.get_last_token(user, true)      print(user_tokens)     print(user_last_token) 

example b

@classmethod def get_all_tokens(cls, user):     return cls.objects.filter(user=user, usage=cls.phone_validation)  @classmethod def play_with_tokens(cls, user):     user_tokens = cls.get_all_tokens(user, true)     user_last_token = user_tokens.latest('created_at')      print(user_tokens)     print(user_last_token) 

is b faster a? thanks.

for db performance same. can check with

>>> django.db import connection, reset_queries >>> reset_queries() >>> run code here... >>> connection.queries 

you see 4 queries: 1st query equals 3rd one, 2nd query equals 4th one.

this:

user_last_token = user_tokens.latest('created_at') 

does not use cached queryset, issues new sql query.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -