You can do a few things to achieve that.
One way is to use an additional argument while writing to sql.
df.to_sql(method = 'multi')
According to this documentation, passing 'multi' to method argument allows you to bulk insert.
Another solution is to construct a custom insert function using multiprocessing.dummy.
here is the link to the documentation :https://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.dummy
import math
from multiprocessing.dummy import Pool as ThreadPool
...
def insert_df(df, *args, **kwargs):
nworkers = 4 # number of workers that executes insert in parallel fashion
chunk = math.floor(df.shape[0] / nworkers) # number of chunks
chunks = [(chunk * i, (chunk * i) + chunk) for i in range(nworkers)]
chunks.append((chunk * nworkers, df.shape[0]))
pool = ThreadPool(nworkers)
def worker(chunk):
i, j = chunk
df.iloc[i:j, :].to_sql(*args, **kwargs)
pool.map(worker, chunks)
pool.close()
pool.join()
....
insert_df(df, "foo_bar", engine, if_exists='append')
The second method was suggested at https://stackoverflow.com/a/42164138/5614132.