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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:56:39+00:00 2026-06-14T07:56:39+00:00

I’m working with PostgreSQL (I’m a rookie in the database world) and I’d like

  • 0

I’m working with PostgreSQL (I’m a rookie in the database world) and I’d like to know your opinion on the efficiency of this kind of queries I found in the code I’m working with. These queries have a lot of JOINs, and one of them (bold font) has many rows by request. This forces us to GROUP BY request.id in order to obtain a row by request and a field (bold font) with all this rows data.

I think this kind of queries has to lose lots of time looking for all these maximums, but I can’t figure an alternative way of doing this. Any ideas on its efficiency and how to improve it?

SELECT
  request.id AS id,
  max(request_type.name) AS request_type,
  to_char(max(request.timestamp),'DD/mm/YYYY HH24:mi') AS timestamp,
  to_char(max(request.timestamp),'YYYY-mm-DD') AS timestamp_filtering,
  max(state.name) AS request_state,
  max(users.name || ' ' || COALESCE(users.surname,'')) AS create_user,
  max(request.id_create_user) AS id_create_user,
  max(enterprise.name) AS enterprise,
  max(cause_issue.name) AS cause,
  max(request_movements.id_request_state) AS id_state,
  array_to_string(array_agg(DISTINCT act_code.name || '/' || req_res.act_code), ', ') AS act_code, /* here */
  max(revised.code) AS state_revised, 
  max(request_shipment.warehouse) AS warehouse,
  max(req_res.id_warehouse) AS id_warehouse
FROM
  request
  LEFT JOIN users
    ON users.id=request.id_create_user
  LEFT JOIN enterprise
    ON users.id_enterprise=enterprise.id
  LEFT JOIN request_movements
    ON request_movements.id=request.id_request_movement
  LEFT JOIN request_versions
    ON request_versions.id = request_movements.id_version
  LEFT JOIN state
    ON request_movements.id_request_state=state.id
  INNER JOIN request_type
    ON request.id_request_type=request_type.id
  LEFT JOIN cause_issue
    ON request.id_cause_issue=cause_issue.id
  LEFT JOIN request_reserve req_res
    ON req_res.id_request = request.id /* here */
  LEFT JOIN act_code
    ON req_res.id_act_code=act_code.id
  LEFT JOIN request_shipment
    ON (request_shipment.id_request=request.id)
  LEFT JOIN warehouse_enterprise
    ON (warehouse_enterprise.id = request_shipment.id_warehouse_enterprise)
  LEFT JOIN revised
    ON (revised.id = request_shipment.id_revised)
WHERE
  request.id_request_type = "any_type"  
GROUP BY
  request.id

The EXPLAIN returns this.

  • 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-14T07:56:40+00:00Added an answer on June 14, 2026 at 7:56 am

    You can much simplify this query by aggregating values in request_reserve and act_code before you JOIN to the big join. This avoids the need for aggregate functions on all the other columns and should generally be much faster for a larger number of rows.

    SELECT r.id
          ,rt.name AS request_type
          ,to_char(r.timestamp, 'DD/mm/YYYY HH24:mi') AS timestamp
          ,to_char(r.timestamp, 'YYYY-mm-DD') AS timestamp_filtering
          ,s.name AS request_state
          ,u.name || COALESCE(' ' || u.surname, '') AS create_user
          ,r.id_create_user
          ,e.name AS enterprise
          ,c.name AS cause
          ,rm.id_request_state AS id_state
          ,rr.act_code
          ,rd.code AS state_revised
          ,rs.warehouse
          ,rr.id_warehouse
    FROM      request              r
    LEFT JOIN users                u  ON u.id = r.id_create_user
    LEFT JOIN enterprise           e  ON e.id = u.id_enterprise
    LEFT JOIN request_movements    rm ON rm.id = r.id_request_movement
    LEFT JOIN request_versions     rv ON rv.id = rm.id_version
    LEFT JOIN state                s  ON s.id = rm.id_request_state
         JOIN request_type         rt ON rt.id = r.id_request_type
    LEFT JOIN cause_issue          c  ON c.id = r.id_cause_issue
    LEFT JOIN request_shipment     rs ON rs.id_request = r.id
    LEFT JOIN warehouse_enterprise w  ON w.id = rs.id_warehouse_enterprise
    LEFT JOIN revised              rd ON rd.id = rs.id_revised
    LEFT JOIN (
       SELECT rr.id_request, rr.id_warehouse
             ,array_to_string(array_agg(
                 DISTINCT a.name || '/' || rr.act_code), ', ') AS act_code
       FROM   request_reserve rr
       LEFT   JOIN act_code   a ON r.id_act_code = a.id
       GROUP  BY rr.id_request, rr.id_warehouse
       )  rr ON rr.id_request = r.id
    WHERE  r.id_request_type = "any_type";  -- use single quotes for values!
    

    For big queries it is essential that you have a format the human eye can easily parse. Therefore I reformatted before I improved the query.
    I use table aliases to avoid unwieldy identifiers as much as possible.

    Minor improvement to create_user: no trailing space. If either part of the name can be NULL, I suggest this to avoid a dangling space:

    COALESCE(u.name || ' ' || u.surname, u.name, u.surname)
    

    In PostgreSQL 9.1 or later you could use concat_ws().

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.