使用 Django 的 ORM 加速批量插入? [英] Accelerate bulk insert using Django's ORM?
本文介绍了使用 Django 的 ORM 加速批量插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我计划使用 django 的 ORM 将从约 750 个文件(每个约 250MB)中提取的 10 亿条记录上传到数据库.目前每个文件需要大约 20 分钟来处理,我想知道是否有任何方法可以加速这个过程.
I'm planning to upload a billion records taken from ~750 files (each ~250MB) to a db using django's ORM. Currently each file takes ~20min to process, and I was wondering if there's any way to accelerate this process.
我采取了以下措施:
- 使用@transaction.commit_manually 并每 5000 条记录提交一次
- 设置 DEBUG=False 以便 django 不会累积内存中的所有sql命令
- 在单个文件中运行记录的循环完全包含在单个函数中(最小化堆栈更改)
- 避免访问数据库进行查询(使用数据库中已有对象的本地哈希而不是使用 get_or_create)
- 设置 force_insert=Truesave() 希望它能为 Django 节省一些逻辑
- 显式设置id 希望它能为 Django 节省一些逻辑
- 一般代码最小化和优化
- Use @transaction.commit_manually and commit once every 5000 records
- Set DEBUG=False so that django won't accumulate all the sql commands in memory
- The loop that runs over records in a single file is completely contained in a single function (minimize stack changes)
- Refrained from hitting the db for queries (used a local hash of objects already in the db instead of using get_or_create)
- Set force_insert=True in the save() in hopes it will save django some logic
- Explicitly set the id in hopes it will save django some logic
- General code minimization and optimization
我还能做些什么来加快速度?以下是我的一些想法:
What else can I do to speed things up? Here are some of my thoughts:
- 使用某种更快的 Python 编译器或版本(Psyco?)
- 覆盖ORM,直接使用SQL
- 使用一些可能更好的第三方代码(1、2)
- 请求 django 社区创建一个 bulk_insert 函数
- Use some kind of Python compiler or version which is quicker (Psyco?)
- Override the ORM and use SQL directly
- Use some 3rd party code that might be better (1, 2)
- Beg the django community to create a bulk_insert function
欢迎提供有关这些项目的任何指示或任何其他想法:)
Any pointers regarding these items or any other idea would be welcome :)
推荐答案
Django 1.4 在 QuerySet 对象上提供了一个 bulk_create()
方法,见:
Django 1.4 provides a bulk_create()
method on the QuerySet object, see:
- https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create
- https://docs.djangoproject.com/en/dev/releases/1.4/
- https://code.djangoproject.com/ticket/7596
这篇关于使用 Django 的 ORM 加速批量插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文