Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 8 of 10 · Django Performance & Optimization
Interview question

How do you use bulk_create, bulk_update, and iterator() to handle large datasets efficiently? बड़े डेटासेट को कुशलता से हैंडल करने के लिए bulk_create, bulk_update और iterator() का उपयोग कैसे करें?

Answer

Calling .save() in a loop to create or update many rows issues one database round-trip per object, which is slow at scale. bulk_create() inserts a list of unsaved model instances in a single (or few, batched) SQL statement, and bulk_update() similarly updates many existing instances in one query, both dramatically reducing round-trip overhead — though bulk operations skip save(), so custom save() logic and signals like pre_save/post_save don't fire.

For reading large querysets, plain iteration loads the entire result set into memory at once (via internal caching), which can exhaust memory for very large tables. iterator(chunk_size=2000) streams results from the database in batches instead of loading everything upfront, trading the queryset's result caching for bounded memory usage — the right choice for one-off exports or migrations over millions of rows.

Product.objects.bulk_create([Product(name=f"P{i}") for i in range(10000)])

products = Product.objects.filter(category='clearance')
for p in products:
    p.price *= 0.8
Product.objects.bulk_update(products, ['price'])

for product in Product.objects.all().iterator(chunk_size=2000):
    process(product)

कई पंक्तियों को बनाने या अपडेट करने के लिए लूप में .save() कॉल करना प्रति ऑब्जेक्ट एक डेटाबेस राउंड-ट्रिप जारी करता है, जो बड़े पैमाने पर धीमा है। bulk_create() असेव्ड मॉडल इंस्टेंसेस की सूची को एक ही SQL स्टेटमेंट में इंसर्ट करता है।

बड़े क्वेरीसेट्स पढ़ने के लिए, सादा इटरेशन पूरे परिणाम सेट को एक साथ मेमोरी में लोड करता है। iterator(chunk_size=2000) डेटाबेस से बैचों में परिणाम स्ट्रीम करता है।

Product.objects.bulk_create([Product(name=f"P{i}") for i in range(10000)])

for product in Product.objects.all().iterator(chunk_size=2000):
    process(product)

Was this answer clear?