Answer the question
In order to leave comments, you need to log in
How to speed up import from csv to database?
We have django, postgresql and 15 million products in a csv file, all imported in 1 stream.
I do it like this:
with open('new_products.csv', 'r', encoding='utf-8') as csvfile:
...
for row in rows:
product = Product()
product.price = row[0]
product.shipping = row[1]
product.save()
Answer the question
In order to leave comments, you need to log in
1. Iterate rows of several hundred pieces, and create objects corresponding to them with a single call to bulk_create:
import itertools
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
...
limit = 250
for index, items in enumerate(grouper(rows, limit)):
items = (i for i in items if i) # remove empty rows added by grouper
products = []
for item in items:
product = Product(
price=item[0],
shipping=item[1],
)
products.append(product)
Product.objects.bulk_create(products)
from django.db import transaction
...
limit = 250
for index, items in enumerate(grouper(rows, limit)):
with transaction.atomic():
for item in (i for i in items if i):
product = Product.objects.create(
price=item[0],
shipping=item[1],
)
# product can be used there to create another objects, e.g.:
for color in item[2].split(','):
ProductColor.objects.create(
product=product,
color=color,
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question