We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
# BAD ## 每次都执行commit,整体耗时较长(大约25s左右) for num in range(10000): Record.objects.create(num=num) # GOOD ## 统一提交数据库,耗时很短(1s以内) inserted_list = [] for num in range(10000): inserted_list.append(Demo(num=num)) Record.objects.bulk_create(inserted_list)
注意: bulk_create 方法只执行一次数据库交互,这样相当于创建时间一样,并且自定字段不会在返回数据中
The text was updated successfully, but these errors were encountered:
之前遇到过一个坑:bulk_create 提交的东西过多,会导致 MySQL 异常,可能根据具体场景,适当对 inserted_list 做切片提交会更稳妥。
bulk_create
inserted_list
Sorry, something went wrong.
可以设置batch_size
之前遇到过一个坑:bulk_create 提交的东西过多,会导致 MySQL 异常,可能根据具体场景,适当对 inserted_list 做切片提交会更稳妥。 可以设置batch_size
我之前居然还手写了切片 😵
当数量不可控时,一定要添加batch_size
Successfully merging a pull request may close this issue.
注意: bulk_create 方法只执行一次数据库交互,这样相当于创建时间一样,并且自定字段不会在返回数据中
The text was updated successfully, but these errors were encountered: