Best practice for calling save() many times

whitebear

When calling save many times, it works but takes a few minuts.

num = 100000
for i in range(num):
    lu = sm.UserType(type_id=1)
    lu.save()

In my understanding save exec the SQL.

So if I can put save together, it takes a shorter time.

Is there any practice for this purpose?

Willem Van Onsem

You can work with .bulk_create(…) [Django-doc]:

num = 100000

sm.UserType.objects.bulk_create([
    sm.UserType(type_id=1)
    for i in range(num)
])

This will for most databases create all records with one query, except for SQLite, that will create the objects in batches of 999 items.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling an API 20 times to generate a list, best practice?

From Dev

Jquery Best Practice - Putting DOM Element in Variable regardless of how many times it was used

From Dev

Admob best practice: how many times do I request, and how long should I show a banner ad?

From Dev

SQL one to many, best practice?

From Dev

Best practice to save the uploaded images

From Dev

best-practice for ordered many to many relation?

From Java

Calling many methods of many objects many times per second

From Dev

Best practice for makefile calling other makefile

From Dev

Best practice for calling two different endpoints for a widget?

From Dev

Vue best practice for calling a method in a child component

From Dev

Best Practice to avoid using 'ifs' for calling methods

From Dev

Best practice when calling self object methods

From Dev

Best practice for calling the NgbModal open method

From Dev

Calling a external API class on Observer best practice

From Dev

Rust - Calling function with multiple types best practice

From Dev

Best practice for calling objects that are private members of a class?

From Dev

android viewpagerindicator calling getPageTitle() too many times

From Dev

Object Instantiation Calling Constructor Too Many Times

From Java

Best practice for passing many arguments to method?

From Dev

Best practice: how to pass many arguments to a function?

From Dev

Best practice for retrieving many images from Parse?

From Dev

What is a best practice for OCaml variants with many fields?

From Dev

Best practice for many try-catch blocks

From Dev

Best practice save product image in laravel

From Dev

Pytorch: best practice to save list of tensors?

From Dev

Azure IoT Hub - Save telemetry best practice

From Dev

How to use @JsonManagedReference and @JsonBackReference for Many to Many relationship with best practice?

From Dev

Best practice for storing data of many to many models (tags)

From Dev

Best practice for preloading core data with many to many relationship

Related Related

  1. 1

    Calling an API 20 times to generate a list, best practice?

  2. 2

    Jquery Best Practice - Putting DOM Element in Variable regardless of how many times it was used

  3. 3

    Admob best practice: how many times do I request, and how long should I show a banner ad?

  4. 4

    SQL one to many, best practice?

  5. 5

    Best practice to save the uploaded images

  6. 6

    best-practice for ordered many to many relation?

  7. 7

    Calling many methods of many objects many times per second

  8. 8

    Best practice for makefile calling other makefile

  9. 9

    Best practice for calling two different endpoints for a widget?

  10. 10

    Vue best practice for calling a method in a child component

  11. 11

    Best Practice to avoid using 'ifs' for calling methods

  12. 12

    Best practice when calling self object methods

  13. 13

    Best practice for calling the NgbModal open method

  14. 14

    Calling a external API class on Observer best practice

  15. 15

    Rust - Calling function with multiple types best practice

  16. 16

    Best practice for calling objects that are private members of a class?

  17. 17

    android viewpagerindicator calling getPageTitle() too many times

  18. 18

    Object Instantiation Calling Constructor Too Many Times

  19. 19

    Best practice for passing many arguments to method?

  20. 20

    Best practice: how to pass many arguments to a function?

  21. 21

    Best practice for retrieving many images from Parse?

  22. 22

    What is a best practice for OCaml variants with many fields?

  23. 23

    Best practice for many try-catch blocks

  24. 24

    Best practice save product image in laravel

  25. 25

    Pytorch: best practice to save list of tensors?

  26. 26

    Azure IoT Hub - Save telemetry best practice

  27. 27

    How to use @JsonManagedReference and @JsonBackReference for Many to Many relationship with best practice?

  28. 28

    Best practice for storing data of many to many models (tags)

  29. 29

    Best practice for preloading core data with many to many relationship

HotTag

Archive