Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:18:06+00:00 2026-05-26T03:18:06+00:00

I’ve got the following problen: I know SQL and I don’t know how to

  • 0

I’ve got the following problen: I know SQL and I don’t know how to work with SQLAlchemy but I need to change it in 1 place in the project that I’ve inherited.
So, I’ve got this:

ModelCategories = request.sa.query(
    Model.category_id
    , Category.name
    , Category.alias).distinct().join(Category).order_by(Category.alias
    , Category.name )

And it generates a rather slow request:

SELECT DISTINCT 
  model.category_id AS model_category_id
  , category.name AS category_name
  , category.alias AS category_alias 
FROM model 
JOIN category ON category.id = model.category_id 
ORDER BY category.alias, category.name

And I need to change it with this:

SELECT 
  model.category_id AS model_category_id
  , category.name AS category_name
  , category.alias AS category_alias 
FROM ( SELECT DISTINCT model_category_id ) as model 
JOIN category ON category.id = model.category_id 
ORDER BY category.alias, category.name

But in terms of SQLAlchemy as is the first request.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:18:07+00:00Added an answer on May 26, 2026 at 3:18 am

    First of all check the SQL execution plan. If you have an index on the model.category_id column, the query should not really be slow.
    Otherwise, following options are available:

    Option-1: almost your current solution

    # python
    ModelCategories = session.query(Category).distinct().join(Model).order_by(Category.alias, Category.name)
    
    # SQL
    SELECT DISTINCT category.id AS category_id, category.name AS category_name, category.alias AS category_alias 
    FROM category 
    JOIN model ON category.id = model.category_id 
    ORDER BY category.alias, category.name
    

    This is like your current solution but somewhat cleaner in my view. I assume the performance issue might come from the fact that all table Model is used in the query, and this is also why you need to use distinct.

    Option-2: use any() on relationship

    # python (assumption: model mapping has relationship defined between Category and Model
    mapper(Category, category_table, properties={
        'models': relationship(Model, backref="category") })
    
    # python
    ModelCategories = session.query(Category).filter(Category.models.any()).order_by(Category.alias, Category.name)
    
    # SQL
    SELECT category.id AS category_id, category.name AS category_name, category.alias AS category_alias 
    FROM category 
    WHERE EXISTS (SELECT 1 
        FROM model 
        WHERE category.id = model.category_id)
    ORDER BY category.alias, category.name
    

    This should boost your performance already. I prefer this to following option-3 as it is again more clean code

    Option-3: use subquery

    # python
    q = select([Model.category_id]).distinct().alias("subq")
    ModelCategories = session.query(Category).join(q, Category.id==q.c.category_id)
    
    # SQL
    SELECT category.id AS category_id, category.name AS category_name, category.alias AS category_alias 
    FROM category
    JOIN (SELECT DISTINCT model.category_id AS category_id FROM model) AS subq 
      ON category.id = subq.category_id
    ORDER BY category.alias, category.name
    

    This should give you exactly the SQL you asked for. As mentioned, I personally prefer version-2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.