简体   繁体   中英

increase insertion speed in python

I have a script in django which i am running on terminal to update the field values in database , there is about 3000 records to be updated , it updated but it takes so much time.

Here is the code:

getAge  = myplayer.objects.all()
for i in getAge:
    i.age  = i.age + 0.0192         # it is equilant to  1/52
    i.save()    
    print  "new age of id - " ,i.id, "is ", i.age 

I am using a MYSQL DB please suggest how can i speed up the insertion time

thanks

If you don't need to print new ages, you can do as follow using F :

from django.db.models import F
myplayer.objects.update(age=F('age')+0.0192)
from django.db.models import F    
myplayer.objects.all().update(age=F('age') + 0.192)

Read https://docs.djangoproject.com/en/dev/topics/db/queries/#updating-multiple-objects-at-once

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM