A brief description of the easy-to-err error in SQLAlchemy sorting

Source: Internet
Author: User
This article mainly describes the python in the sqlalchemy sort of a pit, the text gives a detailed sample code, the need for friends can reference, interested friends to learn from the following together.

Objective

SQLAlchemy is an ORM framework in the Python programming language that builds on database APIs and uses relational object mappings for database operations, in short: converting objects to SQL and then executing SQL using the data API and getting execution results. Recently in the use of sqlalchemy sort encountered a pit, so want to summarize down, share to more friends, below to see together.

The Code of the Pit

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. All ()

No pits of 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 ()). All ()  query = query.filter (Uservideo.status = = 1)  query = query.order_by (UserVideo.vid.desc ())  query = Query.limit (). All ()

Yes, you're right, that's the horizontal bar, slow speed. Change desc() to function speed can be increased by 10 times times

Attached below is a sqlalchemy high-performance random fetching of several strips 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 = Queryquery_tail = Query_tail.join (Rvid, Uservideo.vid > Rvid.c.rvid). Limit (+). All ()

"Recommended"

1. Python Free video tutorial

2. Python Object-oriented video

3. Python Learning Manual

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.