Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:32:36+00:00 2026-06-17T10:32:36+00:00

I am evaluating one QuerySet, and then another, but the second is a subset

  • 0

I am evaluating one QuerySet, and then another, but the second is a subset of the first. I’m trying to do this in an efficient way, with as few database calls as possible. (This has been asked before but to be honest I didn’t totally understand the answer and I’m not sure they completely apply to what I’m thinking about.)

Using the Django doc’s example Weblog models, in a view, this is the code before trying to optimise:

myblog = Blog.objects.get(pk=1)

d={} #going to pass this to my template

# not using count()
d['num_of_entries'] = len(myblog.entry_set.all()) 

# not using exists()
d['is_jolly'] = bool(Entry.objects.filter(blog=myblog, headline__startswith='Jolly'))

# ... other code but no further use of database in this view

The second QuerySet is a subset of the first. Should I try to use pure Python to get the subset (and so only evaluate one QuerySet -one less database call)?

Or, perhaps simply do the following?

# other code as above

d['num_of_entries'] = myblog.entry_set.count()

d['is_jolly'] = Entry.objects.filter(blog=myblog, headline__startswith='Jolly').exists()

# ... other code but no further use of database in this view
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:32:38+00:00Added an answer on June 17, 2026 at 10:32 am

    “As few database queries as possible” isn’t the right criterion. You want to also think about the amount of work done by those database queries, and the amount of data that’s transferred from the database to your Django server.

    Let’s consider the two ways to implement what you’re after. Approach 1:

    entries = myblog.entry_set.all()
    num_of_entries = len(entries)
    is_jolly = any(e.headline.startswith('Jolly') for e in entries)
    

    Approach 2:

    num_of_entries = myblog.entry_set.count()
    is_jolly = Entry.objects.filter(blog=myblog, headline__startswith='Jolly').exists()
    

    In approach 1 there’s a single database query, but that query going to be something like SELECT * FROM ENTRY WHERE .... It potentially fetches a large number of entries, and transmits all their content across the network to your Django server, which then throws nearly all that content away (the only field it actually looks at is the headline field).

    In approach 2 there are two database queries, but the first one is a SELECT COUNT(*) FROM ENTRY WHERE ... which fetches just one integer, and the second is a SELECT EXISTS(...) which fetches just one Boolean. Since these queries don’t have to fetch all the matching entries, there are a lot more possibilities for the database to optimize the queries.

    So approach 2 looks much better in this case, even though it issues more queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are currently evaluating upgrading from XP to Windows 7, but have one last
I am in process of evaluating ORM first time. Please suggest which one i
We're currently a Linq to SQL shop but evaluating Entity Framework. One thing that
I am recrusively evaluating properties via reflection using object.GetType().GetProperty(string propertyName). This is working fine
I'm evaluating the use of CRM 2011 to replace one of our existing LOB
I'm evaluating a piece of software (uPortal), and it lists one of its features
We are evaluating a few technologies to build a repository of WSDLs, and XSDs
I am evaluating two object databases, db4o ( http://www.db4o.com ) and Eloquera Database (
I'm quite lost on this one and I hope someone here could help. My
I have one query. Maybe it is a silly question but still I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.