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 7954305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:23:23+00:00 2026-06-04T03:23:23+00:00

Good day, my question is about optimizing sql query. The following query is slow:

  • 0

Good day, my question is about optimizing sql query.
The following query is slow:

SELECT id, name,
, (SELECT rank_time FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1)::date AS rank_time
, (SELECT host_c    FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS host_c
, (SELECT index_pa  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS index_pa
, (SELECT links_pa  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS links_pa
, (SELECT index_pb  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS index_pb
, (SELECT links_pb  FROM stage_rank WHERE stage = stage.id ORDER BY rank_time DESC LIMIT 1) AS links_pb
FROM stage
ORDER BY name;

I think it mostly because of repeated select from stage_rank, is it possible to make this select done once, and get all fields in single hit ?

Also any postgresql specific feaures might be of help here ?

  • 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-06-04T03:23:24+00:00Added an answer on June 4, 2026 at 3:23 am

    In PostgreSQL, you can select the whole record as a field and expand it later:

    SELECT  id, name, (sr).*
    FROM    (
            SELECT  id, name,
                    (
                    SELECT  stage_rank
                    FROM    stage_rank
                    WHERE   stage = stage.id
                    ORDER BY
                            rank_time DESC
                    LIMIT 1
                    ) sr
            FROM    stage
            ) q
    ORDER BY
            name
    

    or rewrite the query:

    SELECT  DISTINCT ON (s.name, s.id, sr.rank_time, sr.id)
            s.id, s.name, sr.*
    FROM    stage s
    JOIN    stage_rank sr
    ON      sr.stage = s.id
    ORDER BY
            s.name, s.id, sr.rank_time DESC, sr.id DESC
    

    or rewrite it the other way:

    SELECT  id, name, (sr).*
    FROM    (
            SELECT  s.id, s.name, sr, ROW_NUMBER() OVER (PARTITION BY s.id ORDER BY sr.rank_time DESC, sr.id DESC) rn
            FROM    stage
            JOIN    stage_rank sr
            ON      sr.stage = s.id
            ) q
    WHERE   rn = 1
    ORDER BY
            name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, friends. Once again stupid question about Obj-C from newbie :) I'm trying
Good day everyone. I have a question about making and using derived classes of
Good day! I have got a question about the Internet Explorer 9. Can anyone
Hello and good day to all. I have a little question about like statement
Good day I have question about displaying html documents in a windows forms applications.
Good day to all. I have a question about flash... is it possible to
Good day. I have the following question: How to put the following object [DataContract]
Good day, IDE Visual Studio 2010 .NET 3.5 Platform WinForms The SO question difference
Good day, I receive data from a communication channel and display it. Parallel, I
Good day, If I have for example the documents which have the following fields

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.