python sqlalchemy批量插入大量数据,性能最佳!
def create_user_items(db: Session, mouse_events, user, events_dict):
mouse_events = json.loads(mouse_events)
db.execute(
models.Sessions.__table__.insert(),
[{"user_id": user, "x_coordinate": event["x_cor"], "y_coordinate": event["y_cor"],
"event_id": events_dict.get(event['event']), "event_time": event["event_time"],
"x_resolution": event["x_res"],
"y_resolution": event["y_res"]} for event in mouse_events]
)
db.commit()