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 3404044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:20:04+00:00 2026-05-18T05:20:04+00:00

The table in question contains roughly ten million rows. for event in Event.objects.all(): print

  • 0

The table in question contains roughly ten million rows.

for event in Event.objects.all():
    print event

This causes memory usage to increase steadily to 4 GB or so, at which point the rows print rapidly. The lengthy delay before the first row printed surprised me – I expected it to print almost instantly.

I also tried Event.objects.iterator() which behaved the same way.

I don’t understand what Django is loading into memory or why it is doing this. I expected Django to iterate through the results at the database level, which’d mean the results would be printed at roughly a constant rate (rather than all at once after a lengthy wait).

What have I misunderstood?

(I don’t know whether it’s relevant, but I’m using PostgreSQL.)

  • 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-05-18T05:20:05+00:00Added an answer on May 18, 2026 at 5:20 am

    Nate C was close, but not quite.

    From the docs:

    You can evaluate a QuerySet in the following ways:

    • Iteration. A QuerySet is iterable, and it executes its database query the first time you iterate over it. For example, this will print the headline of all entries in the database:

      for e in Entry.objects.all():
          print e.headline
      

    So your ten million rows are retrieved, all at once, when you first enter that loop and get the iterating form of the queryset. The wait you experience is Django loading the database rows and creating objects for each one, before returning something you can actually iterate over. Then you have everything in memory, and the results come spilling out.

    From my reading of the docs, iterator() does nothing more than bypass QuerySet’s internal caching mechanisms. I think it might make sense for it to a do a one-by-one thing, but that would conversely require ten-million individual hits on your database. Maybe not all that desirable.

    Iterating over large datasets efficiently is something we still haven’t gotten quite right, but there are some snippets out there you might find useful for your purposes:

    • Memory Efficient Django QuerySet iterator
    • batch querysets
    • QuerySet Foreach
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Replaces Question: Update multiple rows into SQL table Here's a Code Snippet to update
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
Another basic Rails question: I have a database table that needs to contain references
Assuming a table of tags like the stackoverflow question tags: TagID (bigint), QuestionID (bigint),
Simple question, how do you list the primary key of a table with T-SQL?
Question for YUI experts... I have a table and I've made each cell of
Quick question, I was curious if there is any difference between a database table
To follow on from my question yesterday.... MySQL Table Design for a Questionnaire I
I have two tables - `employee` and `department`. 1. `employee` table contains column id,employee
I have a question about tables in MySQL. I'm currently making a website where

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.