Performance test not yielding good result when connected to Oracle DB using SQLAlchemy #9449
-
First Check
Commit to Help
Example Codedb_connections.py
db_url = URL.create("oracle+oracledb", username="username", password="password", host="host",
port=port, database="testdb",)
engine = create_engine(db_url, label_length=5, max_overflow=10, pool_size=20)
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
base = declarative_base()
def db_session():
db = SessionLocal()
try:
yield db
finally:
db.close()
books_crud.py
def get_available_books(db: Session):
return db.query(models.BooksMaster).filter(models.BooksMaster.available == 1).all()
routers.py
@router.get("/books")
async def get_available_books(db:Session = Depends(db_session)):
available_books = books_crud.get_available_books(db)
return available_booksDescriptionI have used SQLAlchemy and connected to Oracle DB with max_pool = 20 implementing the conventional method as given in code snippet. The endpoint performs a simple retrieve operation. I did a performance test with peak_concurrency = 100users, spawn_rate = 10users/second for 2min. The average response time = 2sec and throughput = 30TPS. Also db connections got exhausted and requests failed. The expectation from this discussion would be to get an approach to connect to oracle db using SQLAlchemy ORM that provides the best performance. I have gone through multiple github discussions but were unable to find the standard or the best practice. Operating SystemLinux Operating System DetailsRHEL 8 FastAPI Version0.95.1 Python Versionpython 3.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Hello, it appears that you're using synchronous sqlalchemy API with async endpoint, it would be better if you use asynchronous sqlalchemy API or make your endpoint function synchronous (but it would lead to extra costs due to that function running in a thread executor) Also you should use new SQLAlchemy You can also simplify your dependency function: def db_session():
with SessionLocal() as session:
yield session |
Beta Was this translation helpful? Give feedback.
Hello, it appears that you're using synchronous sqlalchemy API with async endpoint, it would be better if you use asynchronous sqlalchemy API or make your endpoint function synchronous (but it would lead to extra costs due to that function running in a thread executor)
Also you should use new SQLAlchemy
2.0selectAPI instead of legacySession.queryYou can also simplify your dependency function: