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

  • SEARCH
  • Home
  • 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 7961105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:56:26+00:00 2026-06-04T04:56:26+00:00

Which one would be better for performance? We take a slice of products. which

  • 0

Which one would be better for performance?

We take a slice of products. which make us impossible to bulk update.

products = Product.objects.filter(featured=True).order_by("-modified_on")[3:]

for product in products:
  product.featured = False
  product.save()

or (invalid)

for product in products.iterator():
  product.update(featured=False)

I have tried QuerySet’s in statement too as following.

Product.objects.filter(pk__in=products).update(featured=False)

This line works fine on SQLite. But, it rises following exception on MySQL. So, I couldn’t use that.

DatabaseError: (1235, “This version of MySQL doesn’t yet support
‘LIMIT & IN/ALL/ANY/SOME subquery'”)

Edit: Also iterator() method causes re-evaluate the query. So, it is bad for performance.

  • 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-04T04:56:27+00:00Added an answer on June 4, 2026 at 4:56 am

    As @Chris Pratt pointed out in comments, the second example is invalid because the objects don’t have update methods. Your first example will require queries equal to results+1 since it has to update each object. That might really be costly if you have 1000 products. Ideally you do want to reduce this to a more fixed expense if possible.

    This is a similar situation to another question:
    Django: Cannot update a query once a slice has been taken

    That being said, you would have to do it in at least 2 queries, but you have to be a bit sneaky on how to construct the LIMIT…

    Using Q objects for complex queries:

    # get the IDs we want to exclude
    products = Product.objects.filter(featured=True).order_by("-modified_on")[:3]
    # flatten them into just a list of ids
    ids = products.values_list('id', flat=True)
    
    # Now use the Q object to construct a complex query
    from django.db.models import Q
    # This builds a list of "AND id NOT EQUAL TO i"
    limits = [~Q(id=i) for i in ids]
    Product.objects.filter(featured=True, *limits).update(featured=False)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am curious which one would be better fitting as a currency field ?
From the following list of frameworks, which one would you use to develop a
How do you connect these to your computer? Which one would be easier to
What is the difference between HttpContext.Current.Request.IsAuthenticated and HttpContext.Current.User.Identity.IsAuthenticated? Which one would you use in
Do all git commands have a --dry-run option, or one which would indicate what
If I would want to operate on pointers which point to one byte should
I have one query on which I would like to get your valuable feedback.
I would like to have a mapping which maps two string into one string.
I would like to know which ViewController is currently the active one. in other
I have an UITableViewController which I would like to add UIToolbar to with one

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.