1

I am adding in a DB the output of a model like this:

cursor = conn.cursor()
for i in range(len(df)):
    UserId = df.loc[i, 'UserId']
    Timestamp = df.loc[i, 'Timestamp']
    ChurnPropensity = df.loc[i, 'ChurnPropensity']

    sql = "INSERT INTO DB_Name (UserId, Timestamp, ChurnPropensity) VALUES ({},'{}',{});".format(UserId, Timestamp, ChurnPropensity)

    cursor.execute(sql)

conn.commit()

However, it takes a long time due to the for loop. How would you improve computation time?

Salut,

4
  • 1
    How big is the table? Have you tried using pd.DataFrame.to_sql? Commented Oct 24, 2018 at 14:23
  • I have actually, but did not manage to set up properly the 'create_engine' bit. The table is a few thousand rows, but I'd like it to be able to easily scale. Commented Oct 24, 2018 at 14:28
  • Managed to get pd.DataFrame.to_sql working, and time was reduced by 90% :) Commented Oct 26, 2018 at 14:40
  • Awesome. But I think there are still bottlenecks with bulk inserts even with to_sql because of the I/O. You can try building raw queries and then using sqlalchemy to execute them as well. But great it helped! Commented Oct 26, 2018 at 14:44

1 Answer 1

1

Try this method. Ideally it should speed up the execution.

query = "INSERT INTO DB_Name (UserId, Timestamp, ChurnPropensity) VALUES ({},'{}',{});"
df.apply(lambda row: cursor.execute(query.format(row['UserId'], 
                                                 row['Timestamp'], 
                                                 row['ChurnPropensity'])),
axis=1)

From my experience, execute method itself is slow, so you could speed up by trying to execute multiple queries at once.

query = "INSERT INTO DB_Name (UserId, Timestamp, ChurnPropensity) VALUES ({},'{}',{});"
queries_list = df.apply(lambda row: query.format(row['UserId'], 
                                            row['Timestamp'], 
                                            row['ChurnPropensity']),  axis=1).values.tolist()

queries = ' '.join(queries_list)
cursor.execute(queries, multi=True)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! but it takes the same amount of time.
unexpected keyword argument 'multi'

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.