This is CS50 Web Project1 stage where I have to import books.csv (containing isbn, title, author and year) with 5000 lines in it. The problem is that the import itself takes too long (about 10 lines per second) and it's not normal I suppose. How do I speed it up?
I have created a table with isbn, title, author and year rows all in varchar. I use postgesql. Next I wrote import.py , which looks like this
import csv
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
def main():
f = open("books.csv")
reader = csv.reader(f)
for ISBN, title, author, year in reader:
db.execute("INSERT INTO books (ISBN, title, author, year) VALUES (:ISBN, :title, :author, :year)",
{"ISBN":ISBN, "title":title, "author":author, "year":year})
db.commit()
if __name__ == "__main__":
main()
I expect the import to proceed in less than a minute, but now it's about 30-40 minutes.