python - Fastest way to fetch value of just one field of an instance in Django -
let's have model this:
class a(models.model): title = models.charfield(max_length=10) (more fields)
i want fetch value of title field of instance. can either use filter
:
a.objects.filter(id=1).values_list('title', flat=true)
which generates sql statement this:
select "app_table_a"."title" "app_table_a" ("app_table_a"."a_id" = 1) limit whatever
or use get
:
a.objects.get(id=1).title
which generates this:
select "app_table_a"."id", "app_table_a"."title", ... ,"app_table_a"."all fields in model" "app_table_a" ("app_table_a"."a_id" = 1)
the difference in select
part of queries, gives impression first approach better option. when @ execution time of statements, don't see meaningful difference , both statements fluctuate around, example, 25ms.
so, question whether there difference, performance-wise (specially huge databases , models many fields), or twisting long statements?
yes, agree you.there no significant difference in between them(in perfomance when 1 record) because, behind scenes django get()
method runs filter()
method, checks filter results set 1 record.
Comments
Post a Comment