본문 바로가기

Python/SQLAlchemy

(3)
(스크랩) ★★★ SQLAlchemy에서 Join 하는 방법 (feat. SQLAlchemy 1.4 ORM 공식 도큐먼트) https://docs.sqlalchemy.org/en/14/orm/queryguide.html#joins ORM Querying Guide — SQLAlchemy 1.4 Documentation The select() construct accepts ORM entities, including mapped classes as well as class-level attributes representing mapped columns, which are converted into ORM-annotated FromClause and ColumnElement elements at construction time. A Select object that con docs.sqlalchemy.org
(스크랩 ) ★★ sqlalchemy async session execute 'where', 'order by', 'limit' (feat. Top N & Bottom N 쿼리) async def get_biggest_cities(session: AsyncSession) -> list[City]: result = await session.execute(select(City).order_by(City.population.desc()).limit(20)) return result.scalars().all() where까지 붙이면, 아래처럼 하면 될듯 async def get_biggest_cities(session: AsyncSession) -> list[City]: result = await session.execute(select(City).where(City.name == 'seoul').order_by(City.population.desc()).limit(20)) return..
(스크랩) Lazy loading? Eager loading? (feat. async와 lazy loading 관계) What Lazy loading? Eager loading? 두 테이블 간에 조인을 맺고 사용을 할 때 parent를 언제 로드하는지 설정하는 것이다. 예) child1.some_parent를 호출하면 SQLAlchemy는 데이터베이스에서 parent를 언제 로드할지 결정한다. parents를 언제 로드하는지가 왜 중요한가? Joins are expensive. User idling(대기)을 피해야한다. 150ms이상의 딜레이는 noticeable하다. Joins는 경험에 부정적인 영향을 미치지 않는 UX에서 짧은 시간동안 이루어져야 한다 lazy loading이란? Lazy loading(Default): lazy loading은 필요할때만(e.g. child.parent object가 호출될때) 로딩..