473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Optimizing PgSQL Query function performance

1 New Member
I really need advice on the below, trying to use DB function getpreviousorders..

NOTE: But this DB function got much faster if run directly on DB, without data reference entity data by spring hibernate code in Order entity.

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION "sample"."getpreviousorders"("_sampleid" varchar, "_mrn" varchar, "_createddate" timestamp)
  2.     RETURNS SETOF "sample"."order" AS $BODY$
  3. BEGIN
  4.     RETURN QUERY
  5.         SELECT o1.* FROM "sample"."order" o1 JOIN "sample".patient p1 ON o1.patient_id = p1.id
  6.         WHERE o1.sample_id != _sampleId AND p1.mrn = _mrn
  7.           AND ((o1.collection_date is not null AND o1.collection_date >= _createdDate)
  8.             OR (o1.collection_date is null AND o1.receipt_date is not null
  9.                 AND o1.receipt_date >= _createdDate))
  10.         UNION
  11.         (SELECT o2.* FROM "sample"."order" o2 JOIN "sample".patient p2 ON o2.patient_id = p2.id
  12.          WHERE (o2.status = 1 or o2.status = 2 OR o2.status = 3) AND o2.sample_id != _sampleId
  13.          AND p2.mrn = _mrn
  14.            AND ((o2.collection_date is not null AND o2.collection_date < _createdDate)
  15.              OR (o2.collection_date is null AND o2.receipt_date is not null
  16.                  AND o2.receipt_date < _createdDate)) ORDER by created_date DESC LIMIT 1)
  17.         ORDER by collection_date DESC, receipt_date DESC, created_date DESC;
  18. END; $BODY$
  19.     LANGUAGE plpgsql VOLATILE
  20.                      COST 100
  21.                      ROWS 1000
Jul 12 '21 #1
2 11578
Banfa
9,065 Recognized Expert Moderator Expert
Since the SELECT ... FROM ... part of both halves of the union are the same can you not just use a single select statement that combines the 2 where clauses (into a hideously large clause). I think that will result in not having to process every record twice, once for each SELECT.
Jul 21 '21 #2
Chandler
3 New Member
I would suggest
Step 1: rewrite the query body to
Expand|Select|Wrap|Line Numbers
  1.         SELECT o1.* FROM "sample"."order" o1 JOIN "sample".patient p1 ON o1.patient_id = p1.id
  2.         WHERE o1.sample_id != _sampleId AND p1.mrn = _mrn
  3.           AND o1.collection_date is not null AND o1.collection_date >= _createdDate
  4.  
  5.         union 
  6.  
  7.         SELECT o1.* FROM "sample"."order" o1 JOIN "sample".patient p1 ON o1.patient_id = p1.id
  8.         WHERE o1.sample_id != _sampleId AND p1.mrn = _mrn
  9.           AND o1.collection_date is null AND o1.receipt_date is not null
  10.                 AND o1.receipt_date >= _createdDate
  11.  
  12.         UNION
  13.  
  14.         (
  15.         SELECT o2.* FROM "sample"."order" o2 JOIN "sample".patient p2 ON o2.patient_id = p2.id
  16.          WHERE o2.status in(1,2,3) AND o2.sample_id != _sampleId
  17.          AND p2.mrn = _mrn
  18.            AND o2.collection_date is not null AND o2.collection_date < _createdDate
  19.            UNION ALL
  20.            SELECT o2.* FROM "sample"."order" o2 JOIN "sample".patient p2 ON o2.patient_id = p2.id
  21.          WHERE o2.status in(1,2,3) AND o2.sample_id != _sampleId
  22.          AND p2.mrn = _mrn and o2.collection_date is null AND o2.receipt_date is not null
  23.                  AND o2.receipt_date < _createdDate)
  24.  
  25.                  ORDER by created_date DESC LIMIT 1)
  26.  
  27.         ORDER by collection_date DESC, receipt_date DESC, created_date DESC;
  28.  
Step2. use index advise tool for pg(such as Paw Index Advisor) to accelerate the performance.
Jul 6 '22 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: gord barq | last post by:
I have this query which does a left outer join and it takes forever (like half a day). Here are the results of an explain analysis. mysql> explain SELECT count(searchresult.title) AS number,...
3
by: Robert | last post by:
I am having performance issues on a SQL query in Access. My query is accessing and joining several tables (one very large one). The tables are linked ODBC. The client submits the query to the...
3
by: Alexander Anderson | last post by:
I have a DELETE statement that deletes duplicate data from a table. It takes a long time to execute, so I thought I'd seek advice here. The structure of the table is little funny. The following is...
18
by: Ron Johnson | last post by:
On Mon, 2003-08-25 at 16:28, Gregory S. Williamson wrote: > One of our sysads sent this link ... wondering if there is any comment on it from the world of actual users of linux and a database. > >...
6
by: Ryan | last post by:
I came across a situation that I've been unable to explain and was hoping somebody had an answer: I had written an update query which was taking about 8 seconds to run and considered it too...
1
by: frizzle | last post by:
Hi there, I'm building a forum with a mysql backend. The forum has the following structure: - category |--> forum |--> thread |--> replies Once the user has defined a forum (id), i first...
1
by: mamapossible | last post by:
Hi, I've spent hours trying to optimize this simple query: SELECT count(sites_jobs.id) as jobCount, sites_jobs_categories.frn_site_id, sites_jobs_categories.id, sites_jobs_categories.name,...
0
by: rashmigaikwad | last post by:
Hi All, I need help in optimizing the query mentioned below: SELECT SUM(CASE WHEN PROD_TYP='HBRMC' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) ...
1
by: thoolsur | last post by:
how can i optimize my query using cost based optimizer and how i can select cost of query. please explain me with examples.
1
by: john111 | last post by:
After searching on the internet for 3 hours i couldn't find a single solution of it .... Can you please show me a way to fix it I am using web query function of excel to pull data from a stock...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.