Home >Backend Development >Python Tutorial >Pitfalls of SQLAlchemy sorting in python
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()Yes, you read that right, it’s the horizontal bar, slow down. Change to
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()For more articles related to the pitfalls of SQLAlchemy sorting in python, please pay attention to the PHP Chinese website!