I have a table with about 200k records. I'm trying to insert abount 20 records in batch with Flask-SqlAlchemy. It takes about 20ms normally, but when I load all the records from the table before insertion, it takes about 1000ms(50x slower). Why?
from models import LocationMap, db
import random
import time
# the last line 'db.session.commit()' will be extremely slow when I uncomment this line.
#known_locations = LocationMap.query.all()
for i in range(10):
loc = LocationMap()
loc.longitude = 0 #location_batch[i + j][0]
loc.latitude = random.random() #location_batch[i + j][1]
loc.country = 'test'
loc.province = 'test2'
loc.city = 'test3'
loc.district = 'test4'
loc.township = 'test5'
db.session.add(loc)
st = time.time()
db.session.commit()
print(time.time() - st)