Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Efficiency comparison of filter and _ set in Django ForeignKey reverse query

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "efficiency comparison between filter and _ set in Django ForeignKey reverse query". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

When you use Django to create a model, you must often use ForeignKey to create a many-to-one foreign key relationship between two tables, for example, there is a models.ForeignKey (A) in B. When we need to reverse query B associated with a specific instance in A, we may use two different methods: A.B_set.all () or B.objects.filter (A).

Experimental environment

Operating system: Manjaro Linux 17.1-rc2 Python: Python 3.6.3 Django: Django 1.11.7 Database: SQLite 3.21.0 CPU: i3-4130 @ 3.4GHz memory: DDR3 1600 8G + 4G

Experimental plan

Create the "question" model Questions and the "answer" model Answers, respectively, and the answer model has a many-to-one relationship with the question model ForeignKey to create one question and two answers. Then use two different methods to run the query data 10000 times to compare the time consumed.

Experimental implementation

Create an experimental model

# myapp/models.pyfrom django.db import modelsclass Questions (models.Model):''title = models.CharField (' title', max_length=100) content = models.TextField ('description') class Answers (models.Model):''Model of the answer' 'question = models.ForeignKey (Questions, on_delete=models.CASCADE, verbose_name=' question') content = models.TextField ('answer')

Then we went into django's shell to add data to the model and write our tests.

> from myapp.models import Questions, Answers# creates the first question Questions.objects.create (title ='is this the first question?' Content ='I think this is the first question. I don't know if it's true. ) # create the first answer Answers.objects.create (question = Questions.objects.get (pk=1), content = 'you're right, this is the first question') # create the second answer Answers.objects.create (question = Questions.objects.get (pk=1), content = 'subject, you are the first question, but am I the second answer?' )

Using timeit to test the time consumed by the two methods

From timeit import timeit# build function def time_test_1 (): question= Question.objects.get (pk=1) answers = question.answers_set.all () # build function using filter method def time_test_2 (): question= Question.objects.get (pk=1) answers = Answers.objects.filter (question=question) # use timeit to test timeit (time_test_1, number=10000) 5.346277045000534timeit (time_test_2, number=10000) 5.11136907800028 10000 times

In fact, after many tests, at least in my usage, it always takes about 0.2-0.3 seconds longer to use A.B_set.all () reverse query than B.objects.filter (A) filtering method. Therefore, in terms of time cost, it is more efficient to use filter filtering.

This is the end of the content of "efficiency comparison between filter and _ set in Django ForeignKey reverse query". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report