How to optimize join and sub-query in pgSQL
up vote
1
down vote
favorite
I've 2 tables in PostgreSQL, Schema ==> sts
product_price_log
contains columnsproduct_name
(character varying), K (numeric),sale_date
(date)
product_transaction
contains columnsproduct_name
(character varying),price
(numeric),sale_date
(date)
I want to get json data for sale_date = '2018-01-15' and '2018-01-01'
.
Product price should be from product_price_log
table and sale data from product_transaction
table for summarized report.
this my query:
(SELECT
json_agg(j.*)
from(
select
a.sale_date, a.product_name, sum(a.quantity) as quantity, sum( a.quantity * b.price) as total_sale
from(
select
sale_date, product_name, quantity
from
sts.product_transaction p
where
sale_date = '2018-01-15' or sale_date = '2018-01-01' and quantity > 0
)a
inner join (
select * from(
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-15' order by sale_date desc limit 1
union all
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-01' order by sale_date desc limit 1
)a order by sale_date
)r on (a.product_name = r.product_name and a.sale_date = r.sale_date)
group by
sale_date,
product_name
)j)
;
QUERY PLAN
Aggregate (cost=230444.51..230444.52 rows=1 width=28)
-> Subquery Scan on j (cost=230290.98..230444.50 rows=1 width=28)
-> GroupAggregate (cost=230290.98..230444.49 rows=1 width=20)
How to I reduce time cost or optimize this query?
postgresql
add a comment |
up vote
1
down vote
favorite
I've 2 tables in PostgreSQL, Schema ==> sts
product_price_log
contains columnsproduct_name
(character varying), K (numeric),sale_date
(date)
product_transaction
contains columnsproduct_name
(character varying),price
(numeric),sale_date
(date)
I want to get json data for sale_date = '2018-01-15' and '2018-01-01'
.
Product price should be from product_price_log
table and sale data from product_transaction
table for summarized report.
this my query:
(SELECT
json_agg(j.*)
from(
select
a.sale_date, a.product_name, sum(a.quantity) as quantity, sum( a.quantity * b.price) as total_sale
from(
select
sale_date, product_name, quantity
from
sts.product_transaction p
where
sale_date = '2018-01-15' or sale_date = '2018-01-01' and quantity > 0
)a
inner join (
select * from(
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-15' order by sale_date desc limit 1
union all
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-01' order by sale_date desc limit 1
)a order by sale_date
)r on (a.product_name = r.product_name and a.sale_date = r.sale_date)
group by
sale_date,
product_name
)j)
;
QUERY PLAN
Aggregate (cost=230444.51..230444.52 rows=1 width=28)
-> Subquery Scan on j (cost=230290.98..230444.50 rows=1 width=28)
-> GroupAggregate (cost=230290.98..230444.49 rows=1 width=20)
How to I reduce time cost or optimize this query?
postgresql
That's not the full query plan, post the whole thing please. Since joining on ther
subquery would seem quite fast, my guess is thea
query is slow - maybe it returns a huge amount of records, or it searches through a table with a lot of records without using indexes? But I'd need to see the actual query plan to determine that.
– 404
Mar 28 at 19:50
Indexes are present in all where Claus column. Yes, 'a' query returns huge row(s). sts.product_transaction contains 10m row(s) +. Filter: (((sale_date = '2018-01-15'::date) OR (sale_date = '2018-01-01'::date)) AND (quantity > '0'::numeric)). -> Seq Scan on product_price_log product_price_log_1 (cost=0.00..17756.29 rows=638411 width=19) Filter: (sale_date <= '2018-03-15'::date) May I've to use any another technique to optimize this query?
– RKTUXYN
Mar 29 at 16:42
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've 2 tables in PostgreSQL, Schema ==> sts
product_price_log
contains columnsproduct_name
(character varying), K (numeric),sale_date
(date)
product_transaction
contains columnsproduct_name
(character varying),price
(numeric),sale_date
(date)
I want to get json data for sale_date = '2018-01-15' and '2018-01-01'
.
Product price should be from product_price_log
table and sale data from product_transaction
table for summarized report.
this my query:
(SELECT
json_agg(j.*)
from(
select
a.sale_date, a.product_name, sum(a.quantity) as quantity, sum( a.quantity * b.price) as total_sale
from(
select
sale_date, product_name, quantity
from
sts.product_transaction p
where
sale_date = '2018-01-15' or sale_date = '2018-01-01' and quantity > 0
)a
inner join (
select * from(
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-15' order by sale_date desc limit 1
union all
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-01' order by sale_date desc limit 1
)a order by sale_date
)r on (a.product_name = r.product_name and a.sale_date = r.sale_date)
group by
sale_date,
product_name
)j)
;
QUERY PLAN
Aggregate (cost=230444.51..230444.52 rows=1 width=28)
-> Subquery Scan on j (cost=230290.98..230444.50 rows=1 width=28)
-> GroupAggregate (cost=230290.98..230444.49 rows=1 width=20)
How to I reduce time cost or optimize this query?
postgresql
I've 2 tables in PostgreSQL, Schema ==> sts
product_price_log
contains columnsproduct_name
(character varying), K (numeric),sale_date
(date)
product_transaction
contains columnsproduct_name
(character varying),price
(numeric),sale_date
(date)
I want to get json data for sale_date = '2018-01-15' and '2018-01-01'
.
Product price should be from product_price_log
table and sale data from product_transaction
table for summarized report.
this my query:
(SELECT
json_agg(j.*)
from(
select
a.sale_date, a.product_name, sum(a.quantity) as quantity, sum( a.quantity * b.price) as total_sale
from(
select
sale_date, product_name, quantity
from
sts.product_transaction p
where
sale_date = '2018-01-15' or sale_date = '2018-01-01' and quantity > 0
)a
inner join (
select * from(
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-15' order by sale_date desc limit 1
union all
select product_name, price, sale_date from sts.product_price_log where sale_date <= '2018-01-01' order by sale_date desc limit 1
)a order by sale_date
)r on (a.product_name = r.product_name and a.sale_date = r.sale_date)
group by
sale_date,
product_name
)j)
;
QUERY PLAN
Aggregate (cost=230444.51..230444.52 rows=1 width=28)
-> Subquery Scan on j (cost=230290.98..230444.50 rows=1 width=28)
-> GroupAggregate (cost=230290.98..230444.49 rows=1 width=20)
How to I reduce time cost or optimize this query?
postgresql
postgresql
edited Nov 12 at 10:45
a_horse_with_no_name
290k46443537
290k46443537
asked Mar 28 at 17:22
RKTUXYN
466415
466415
That's not the full query plan, post the whole thing please. Since joining on ther
subquery would seem quite fast, my guess is thea
query is slow - maybe it returns a huge amount of records, or it searches through a table with a lot of records without using indexes? But I'd need to see the actual query plan to determine that.
– 404
Mar 28 at 19:50
Indexes are present in all where Claus column. Yes, 'a' query returns huge row(s). sts.product_transaction contains 10m row(s) +. Filter: (((sale_date = '2018-01-15'::date) OR (sale_date = '2018-01-01'::date)) AND (quantity > '0'::numeric)). -> Seq Scan on product_price_log product_price_log_1 (cost=0.00..17756.29 rows=638411 width=19) Filter: (sale_date <= '2018-03-15'::date) May I've to use any another technique to optimize this query?
– RKTUXYN
Mar 29 at 16:42
add a comment |
That's not the full query plan, post the whole thing please. Since joining on ther
subquery would seem quite fast, my guess is thea
query is slow - maybe it returns a huge amount of records, or it searches through a table with a lot of records without using indexes? But I'd need to see the actual query plan to determine that.
– 404
Mar 28 at 19:50
Indexes are present in all where Claus column. Yes, 'a' query returns huge row(s). sts.product_transaction contains 10m row(s) +. Filter: (((sale_date = '2018-01-15'::date) OR (sale_date = '2018-01-01'::date)) AND (quantity > '0'::numeric)). -> Seq Scan on product_price_log product_price_log_1 (cost=0.00..17756.29 rows=638411 width=19) Filter: (sale_date <= '2018-03-15'::date) May I've to use any another technique to optimize this query?
– RKTUXYN
Mar 29 at 16:42
That's not the full query plan, post the whole thing please. Since joining on the
r
subquery would seem quite fast, my guess is the a
query is slow - maybe it returns a huge amount of records, or it searches through a table with a lot of records without using indexes? But I'd need to see the actual query plan to determine that.– 404
Mar 28 at 19:50
That's not the full query plan, post the whole thing please. Since joining on the
r
subquery would seem quite fast, my guess is the a
query is slow - maybe it returns a huge amount of records, or it searches through a table with a lot of records without using indexes? But I'd need to see the actual query plan to determine that.– 404
Mar 28 at 19:50
Indexes are present in all where Claus column. Yes, 'a' query returns huge row(s). sts.product_transaction contains 10m row(s) +. Filter: (((sale_date = '2018-01-15'::date) OR (sale_date = '2018-01-01'::date)) AND (quantity > '0'::numeric)). -> Seq Scan on product_price_log product_price_log_1 (cost=0.00..17756.29 rows=638411 width=19) Filter: (sale_date <= '2018-03-15'::date) May I've to use any another technique to optimize this query?
– RKTUXYN
Mar 29 at 16:42
Indexes are present in all where Claus column. Yes, 'a' query returns huge row(s). sts.product_transaction contains 10m row(s) +. Filter: (((sale_date = '2018-01-15'::date) OR (sale_date = '2018-01-01'::date)) AND (quantity > '0'::numeric)). -> Seq Scan on product_price_log product_price_log_1 (cost=0.00..17756.29 rows=638411 width=19) Filter: (sale_date <= '2018-03-15'::date) May I've to use any another technique to optimize this query?
– RKTUXYN
Mar 29 at 16:42
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49540642%2fhow-to-optimize-join-and-sub-query-in-pgsql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49540642%2fhow-to-optimize-join-and-sub-query-in-pgsql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That's not the full query plan, post the whole thing please. Since joining on the
r
subquery would seem quite fast, my guess is thea
query is slow - maybe it returns a huge amount of records, or it searches through a table with a lot of records without using indexes? But I'd need to see the actual query plan to determine that.– 404
Mar 28 at 19:50
Indexes are present in all where Claus column. Yes, 'a' query returns huge row(s). sts.product_transaction contains 10m row(s) +. Filter: (((sale_date = '2018-01-15'::date) OR (sale_date = '2018-01-01'::date)) AND (quantity > '0'::numeric)). -> Seq Scan on product_price_log product_price_log_1 (cost=0.00..17756.29 rows=638411 width=19) Filter: (sale_date <= '2018-03-15'::date) May I've to use any another technique to optimize this query?
– RKTUXYN
Mar 29 at 16:42