473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to optimize postgresql with subquires

Hi i have sql that has 4 calculation as the one below with difference being in the interval.
I have cost1 cost2 cost3 cost4 for different interval, Im also having quatities that i multiple to the costs on my outer query. I realize my query takes like 28 sec to return 66 rows.
when i remove the cost subquery calculation it takes a 1 sec. I how can i optimize the ff subquery. Im using the subquery to get the latest cos.NewCostPrice for a given interval. thank you

Expand|Select|Wrap|Line Numbers
  1. (SELECT COALESCE(SUM(cos.NewCostPrice),0) 
  2. FROM  m_costhistory  cos 
  3. LEFT JOIN m_costdetail md ON md.m_costdetail_id=cos.m_costdetail_id
  4. WHERE prod.M_Product_ID = md.M_Product_ID AND prod.ProductType !='A'
  5. AND cos.created >= (('2020-02-17'::date) - interval '3 month' ) 
  6. AND cos.created < ('2020-02-17'::date) 
  7. AND cos.M_CostElement_ID=ce.M_CostElement_ID
  8. AND cos.AD_Client_ID =1000008 AND md.C_AcctSchema_ID = 1000004 
  9. AND cos.AD_Org_ID = CASE WHEN cos.AD_Org_ID =0 Then cos.AD_Org_ID ELSE  1000002  END 
  10. AND cos.created=(SELECT max(created) FROM m_costhistory) 
  11. )as Cost1,
Feb 27 '20 #1
1 2095
Rabbit
12,516 Expert Mod 8TB
It's not necessarily the subquery that's slowing things down. It's that it's correlated to an outer query. Rewrite your query as a non-correlated subquery.

That's really all I can say since you only show us part of the query and you haven't really described what the query is supposed to do. So I can only make guesses.

You need to take out the references to the outer query and rewrite it, most likely with the key fields you need, utilizing a group by, and then joining it to the other query.

Very rarely is a correlated subquery needed. Typically, most correlated subqueries can be rewritten as a regular subquery by utilizing an aggregate query with a group by on the key fields.
Feb 27 '20 #2

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

Similar topics

1
by: Otis Green | last post by:
Vote for or against a new newsgroup proposal. To summarize what you need to do, just send an empty e-mail to postgresql-ballot@netagw.com You will receive a ballot by e-mail. Follow the...
10
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for...
3
by: Marek Lewczuk | last post by:
Hello, I have changed DB from MySQL to PostgreSQL. When I have run my application on PostgreSQL it was disaster - it was much slower than MySQL... I have tried to change PG configuration file...
3
by: Paul Janssen | last post by:
Hello! Can anyone help me out with the following situation: (a) a single query with 550 id's in the IN-clause resulting into 800+ seconds; (b) 550 queries with a single id in the IN-clause...
0
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an...
1
by: Dino Vliet | last post by:
Hi folks, I'm busy with analyzing some data and therefore will have to store 2 big tables containing 50 million obs & 25 million obs. Selecting some interesting obs can decrease these tables with...
0
by: Greg Sabino Mullane | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 This is a PGP-signed copy of the checksums for following PostgreSQL versions: 7.4.5 7.4.4 7.3.7
0
by: Zlatko Matić | last post by:
Hello. Could you, please, help me to optimize my connection string (MS Access 2003, PostgreSQL 8.1.1. and psqlodbc-08_01_0102)? ' PG_ODBC_PARAMETER ACCESS_PARAMETER ' ...
6
by: SR | last post by:
As a starter project for learning Python/PostgreSQL, I am building a Books database that stores information on the books on my bookshelf. Say I have three tables. Table "books" contains rows...
4
by: Alfred | last post by:
The PHP web app that I'm building uses PostgreSQL. Of course it won't work right off the bat, so my web app is going to have to detect that it has never been configured with a database and have to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.