0

Hi guys currently i am trying to migrate current db to another and that process unfortunately involves python. I was able to do it single threaded, but it's incredibly slow took hours to finish 1M data. Is there similar method in python like Java executor and futures?

note that user_list is a chunk of 1000/1M

for data in user_list:
    q = """ insert into users(id,name,address,password) 
        Values({id},{name},{address},{password})
        """.format(id=data['id'],name=data['name'],address=data['address'],password=data['password'])
    db.command(q)

I think it would be a whole lot faster if i run for example 8 concurrent threads inserting 8 at a time instead of single thread doing single insert

3
  • Which DB are you using? It could be faster, but you would need a separate db connection for each thread. I would first see if your DB supports "bulk inserts" or "batched inserts" - those are usually a lot faster than inserting a single row at a time. Commented Jul 29, 2016 at 20:33
  • Ah, so I can't use 1 connection for doing n insert simultaneously? I am importing sql to orientdb at the moment. @ErikR Commented Jul 30, 2016 at 2:56
  • Nope - you'll need one connection per thread. Also see my answer. Commented Jul 30, 2016 at 3:32

1 Answer 1

1

Since you say in the comments that you are using orientdb, have a look at the SQL Batch capability.

Using SQL BATCH does not insert rows in parallel, but it will avoid the round-trip for each command.

You can also use SQL BATCH from Python using the pyorient library:

https://github.com/mogui/pyorient#execute-orientdb-sql-batch

To insert data in parallel you will need to create multiple connections, one for each thread.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup ended up using SQL Batch, but instead of parsing each columns, I ended up exporting separate CSVs as classes. That'd do for now. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.