SQLAlchemy with MySQL on AWS Lambda is taking long time to truncate table

211 views
Skip to first unread message

Chandra Prakash

unread,
Dec 13, 2022, 8:13:14 AM12/13/22
to sqlalchemy
On creating all tables using alembic for migrations and then truncate any empty table gets completed quickly, BUT once lambda function is triggered to insert some data in a table through SQLAlchemy ORM Session query (as given below) and then truncate the table takes very much time. Where is the problem?

```
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

engine = create_engine("mysql+pymysql://....")
Session = sessionmaker(bind=engine)

def add_user():
    session = Session()
    session.add(User(**{'user_id': 1, 'name': 'user name'}))
    session.close()
    session.bind.dispose()

```

Yaakov Bressler

unread,
Dec 13, 2022, 6:49:27 PM12/13/22
to sqlalchemy
Is it possible your sessions hasn't committed the change / closed the transaction?

Also, I don't think dispose is helping you here. Consider removing it?

How about modifying:

def add_user():
    session = Session()
    session.add(User(**{'user_id': 1, 'name': 'user name'}))
    session.commit()
    session.close()
    # consider removing
    # session.bind.dispose()

Chandra Prakash

unread,
Dec 14, 2022, 12:19:53 AM12/14/22
to sqlalchemy
I tried this, kept commit and close statements and removed dispose statement, but yet the problem is not solved, it's taking time to truncate the table.

Michael Bayer

unread,
Jan 4, 2023, 7:19:55 PM1/4/23
to sqlalchemy
is truncate happening in some other process?   does it speed up once you kill the initial python process?   does this truncate take time with other kinds of INSERT statements?   is the table very large?   overall you need to perform more observations here as to what conditions cause this "truncate" to take a lot of time.  there's nothing happening in SQLAlchemy that affects this much, particularly for MySQL which is not very aggressive about locking tables.
Reply all
Reply to author
Forward
0 new messages