Preface
SQLAlchemy is an ORM framework under the Python programming language. The framework is built on the database API and uses relational object mapping to perform database operations. In short The simple thing is: convert the object into SQL, then use the data API to execute the SQL and obtain the execution results. Recently, I encountered a pitfall when using SQLAlchemy sorting, so I wanted to summarize it and share it with more friends. Let’s take a look below.
The pitfall code
query = db_session.query(UserVideo.vid, UserVideo.uid, UserVideo.v_width, UserVideo.v_height, UserVideo.create_time, UserVideo.cover, UserVideo.source_url, UserVideo.v_type, UserVideo.category, User.username, User.sex, UserExtraInfo.avatar, UserExtraInfo.watermark) query = query.filter(UserVideo.status == 1, User.uid == UserVideo.uid, UserExtraInfo.uid == UserVideo.uid) query = query.filter(UserVideo.status == 1) query = query.order_by(-UserVideo.vid) query = query.limit(20).all()
##The pitfall code
query = db_session.query(UserVideo.vid, UserVideo.uid, UserVideo.v_width, UserVideo.v_height, UserVideo.create_time, UserVideo.cover, UserVideo.source_url, UserVideo.v_type, UserVideo.category, User.username, User.sex, UserExtraInfo.avatar, UserExtraInfo.watermark) query = query.filter(UserVideo.status == 1, User.uid == UserVideo.uid, UserExtraInfo.uid == UserVideo.uid) # .order_by(UserVideo.vid.desc()).limit(20).all() query = query.filter(UserVideo.status == 1) query = query.order_by(UserVideo.vid.desc()) query = query.limit(20).all()
desc() The function speed can be increased by 10 times
Attached below is a sqlalchemy high-performance random extraction of several pieces of data
query = db_session.query(UserVideo.vid, UserVideo.uid, UserVideo.v_width, UserVideo.v_height, UserVideo.create_time, UserVideo.cover, UserVideo.source_url, UserVideo.v_type, UserVideo.category, User.username, User.sex, UserExtraInfo.avatar, UserExtraInfo.watermark) query = query.filter(UserVideo.status == 1, User.uid == UserVideo.uid, UserExtraInfo.uid == UserVideo.uid) rvid = db_session.query(func.round(random.random() * func.max(UserVideo.vid)).label('rvid')).subquery() query = query.filter(UserVideo.category == category) query_tail = query query_tail = query_tail.join(rvid, UserVideo.vid > rvid.c.rvid).limit(20).all()