How to apply outer limit offset and filters in the subquery to avoid grouping over the complete table used in...
What does an unprocessed RAW file look like?
How can guns be countered by melee combat without raw-ability or exceptional explanations?
Why Is Image Exporting At Larger Dimensions Than In Illustrator File?
Why is Shelob considered evil?
Did the characters in Moving Pictures not know about cameras like Twoflower's?
How should I ship cards?
SQL Server 2017 crashes when backing up because filepath is wrong
Can I combine Divination spells with Arcane Eye?
Can I legally make a website about boycotting a certain company?
How bad is a Computer Science course that doesn't teach Design Patterns?
What is formjacking?
Use intersection in field calculator
Why are "square law" devices important?
Why does this quiz question say that protons and electrons do not combine to form neutrons?
What does @ mean in a hostname in DNS configuration?
Can a Hydra make multiple opportunity attacks at once?
Have the UK Conservatives lost the working majority and if so, what does this mean?
What is the name of this perspective and how is it constructed?
Is layered encryption more secure than long passwords?
How can I make my enemies feel real and make combat more engaging?
How to play songs that contain one guitar when we have two or more guitarists?
Is there a way to pause a running process on Linux systems and resume later?
How to modify 'inter arma enim silent leges' to mean 'in a time of crisis, the law falls silent'?
Ramanujan's radical and how we define an infinite nested radical
How to apply outer limit offset and filters in the subquery to avoid grouping over the complete table used in subquery in Postgresql
Subqueries run very fast individually, but when joined are very slowGroup By primary key or DISTINCT increase query time over 1000x with limitNeed to find the employees who have switched jobs atleast twiceHow to INNER JOIN and OUTER JOIN within the same table?Effect of where statement in subquery (to get rid of unnecessary join)Filtering UNION ALL result is much slower than filtering each subqueryCan I make this multiple join query faster?Check for existing matches to find the field their grouped-byMissing date handling between subquery and OUTER APPLYCounting and grouping over multiple OUTER JOINs
I have legacy tables similar to the following:
employee
------------------------------------
| employee_id | name
------------------------------------
| 1 | David
| 2 | Mathew
------------------------------------
payroll
-------------------------------------
| employee_id | salary
-------------------------------------
| 2 | 200000
| 3 | 90000
-------------------------------------
I want to get the following data, after joins and filters:
-----------------------------------------------------------
| address_id | employee_id | address
-----------------------------------------------------------
| 1 | 2 | street 1, NY
| 2 | 2 | street 2, DC
------------------------------------------------------------
I have the following query:
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
INNER JOIN
(
SELECT employee_id, ARRAY_AGG(address) as address_arr
FROM addresses
GROUP BY employee_id
) table_address ON table_address.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
Above query gives the desired output but is highly unoptimized as the GROUP BY
operation occurs over the complete addresses table before being used for JOIN
operation the outer query.
Kindly answer:
- How can we avoid the
GROUP BY
operation to occurs over the complete
addresses table by usingLIMIT OFFSET
of the outer query? - Will the condition
WHERE employee.employee_id < 1000000
be applied on subquery before or after theGROUP BY
operation in the inner query. If the condition is applied after theGROUP BY
, how can we avoid that?
Note: There are multiple JOIN
s and subqueries in the actual query being used.
postgresql join postgresql-performance group-by subquery
add a comment |
I have legacy tables similar to the following:
employee
------------------------------------
| employee_id | name
------------------------------------
| 1 | David
| 2 | Mathew
------------------------------------
payroll
-------------------------------------
| employee_id | salary
-------------------------------------
| 2 | 200000
| 3 | 90000
-------------------------------------
I want to get the following data, after joins and filters:
-----------------------------------------------------------
| address_id | employee_id | address
-----------------------------------------------------------
| 1 | 2 | street 1, NY
| 2 | 2 | street 2, DC
------------------------------------------------------------
I have the following query:
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
INNER JOIN
(
SELECT employee_id, ARRAY_AGG(address) as address_arr
FROM addresses
GROUP BY employee_id
) table_address ON table_address.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
Above query gives the desired output but is highly unoptimized as the GROUP BY
operation occurs over the complete addresses table before being used for JOIN
operation the outer query.
Kindly answer:
- How can we avoid the
GROUP BY
operation to occurs over the complete
addresses table by usingLIMIT OFFSET
of the outer query? - Will the condition
WHERE employee.employee_id < 1000000
be applied on subquery before or after theGROUP BY
operation in the inner query. If the condition is applied after theGROUP BY
, how can we avoid that?
Note: There are multiple JOIN
s and subqueries in the actual query being used.
postgresql join postgresql-performance group-by subquery
PS. LIMIT without ORDER BY gives you 100 random records from the whole data array... do you really need in that?
– Akina
Feb 8 at 7:44
You should present us the simplest query you can which still have the issue. If you remove the left join on PAYROLL, does the problem go away?
– jjanes
Feb 9 at 15:18
add a comment |
I have legacy tables similar to the following:
employee
------------------------------------
| employee_id | name
------------------------------------
| 1 | David
| 2 | Mathew
------------------------------------
payroll
-------------------------------------
| employee_id | salary
-------------------------------------
| 2 | 200000
| 3 | 90000
-------------------------------------
I want to get the following data, after joins and filters:
-----------------------------------------------------------
| address_id | employee_id | address
-----------------------------------------------------------
| 1 | 2 | street 1, NY
| 2 | 2 | street 2, DC
------------------------------------------------------------
I have the following query:
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
INNER JOIN
(
SELECT employee_id, ARRAY_AGG(address) as address_arr
FROM addresses
GROUP BY employee_id
) table_address ON table_address.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
Above query gives the desired output but is highly unoptimized as the GROUP BY
operation occurs over the complete addresses table before being used for JOIN
operation the outer query.
Kindly answer:
- How can we avoid the
GROUP BY
operation to occurs over the complete
addresses table by usingLIMIT OFFSET
of the outer query? - Will the condition
WHERE employee.employee_id < 1000000
be applied on subquery before or after theGROUP BY
operation in the inner query. If the condition is applied after theGROUP BY
, how can we avoid that?
Note: There are multiple JOIN
s and subqueries in the actual query being used.
postgresql join postgresql-performance group-by subquery
I have legacy tables similar to the following:
employee
------------------------------------
| employee_id | name
------------------------------------
| 1 | David
| 2 | Mathew
------------------------------------
payroll
-------------------------------------
| employee_id | salary
-------------------------------------
| 2 | 200000
| 3 | 90000
-------------------------------------
I want to get the following data, after joins and filters:
-----------------------------------------------------------
| address_id | employee_id | address
-----------------------------------------------------------
| 1 | 2 | street 1, NY
| 2 | 2 | street 2, DC
------------------------------------------------------------
I have the following query:
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
INNER JOIN
(
SELECT employee_id, ARRAY_AGG(address) as address_arr
FROM addresses
GROUP BY employee_id
) table_address ON table_address.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
Above query gives the desired output but is highly unoptimized as the GROUP BY
operation occurs over the complete addresses table before being used for JOIN
operation the outer query.
Kindly answer:
- How can we avoid the
GROUP BY
operation to occurs over the complete
addresses table by usingLIMIT OFFSET
of the outer query? - Will the condition
WHERE employee.employee_id < 1000000
be applied on subquery before or after theGROUP BY
operation in the inner query. If the condition is applied after theGROUP BY
, how can we avoid that?
Note: There are multiple JOIN
s and subqueries in the actual query being used.
postgresql join postgresql-performance group-by subquery
postgresql join postgresql-performance group-by subquery
edited Feb 8 at 7:20
nimeshkiranverma
asked Feb 8 at 7:04
nimeshkiranvermanimeshkiranverma
1013
1013
PS. LIMIT without ORDER BY gives you 100 random records from the whole data array... do you really need in that?
– Akina
Feb 8 at 7:44
You should present us the simplest query you can which still have the issue. If you remove the left join on PAYROLL, does the problem go away?
– jjanes
Feb 9 at 15:18
add a comment |
PS. LIMIT without ORDER BY gives you 100 random records from the whole data array... do you really need in that?
– Akina
Feb 8 at 7:44
You should present us the simplest query you can which still have the issue. If you remove the left join on PAYROLL, does the problem go away?
– jjanes
Feb 9 at 15:18
PS. LIMIT without ORDER BY gives you 100 random records from the whole data array... do you really need in that?
– Akina
Feb 8 at 7:44
PS. LIMIT without ORDER BY gives you 100 random records from the whole data array... do you really need in that?
– Akina
Feb 8 at 7:44
You should present us the simplest query you can which still have the issue. If you remove the left join on PAYROLL, does the problem go away?
– jjanes
Feb 9 at 15:18
You should present us the simplest query you can which still have the issue. If you remove the left join on PAYROLL, does the problem go away?
– jjanes
Feb 9 at 15:18
add a comment |
3 Answers
3
active
oldest
votes
I am not sure if this is really more efficient, but you could try to join to a derived table that applies the limit.
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The first derived table only selects 100 rows, and the join/group by should then only be done for those 100 employees.
If the optimizer doesn't push that down, you could try a lateral join instead to "force" a push down:
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
LATERAL JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
WHERE a.employee_id = emp.employee_id
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The join condition isn't really needed, but it dosn't hurt either
add a comment |
Maybe
SELECT employee.employee_id, payroll.salary, ARRAY_AGG(addresses.address)
FROM employee
INNER JOIN addresses ON addresses.employee_id = employee.employee_id
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
GROUP BY employee.employee_id
LIMIT 100
OFFSET 0
?
And - do you really need in records where no appropriate records in payroll
table which leads to NULLs in payroll.salary
? Maybe, INNER JOIN is enough?
The query is incorrect. payroll.salary needs to be included in GROUP BY clause, which will unoptimise the query further
– nimeshkiranverma
Feb 8 at 7:48
@nimeshkiranverma payroll.salary needs to be included in GROUP BY clause you may wrap it using any aggregate function which can be applied to this field datatype.
– Akina
Feb 8 at 7:50
add a comment |
I am new to this and had some help , here's what I came up with:
with t as (SELECT employee.employee_id, salary FROM employee LEFT JOIN payroll on payroll.employee_id = employee.employee_id WHERE employee.employee_id < 1000000 LIMIT 100 OFFSET 0)
select t.employee_id, max(t.salary), ARRAY_AGG(address) as address_arr from address left join t on address.employee_id = t.employee_id where address.employee_id = t.employee_id group by t.employee_id;
explain analyze yields
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f229214%2fhow-to-apply-outer-limit-offset-and-filters-in-the-subquery-to-avoid-grouping-ov%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am not sure if this is really more efficient, but you could try to join to a derived table that applies the limit.
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The first derived table only selects 100 rows, and the join/group by should then only be done for those 100 employees.
If the optimizer doesn't push that down, you could try a lateral join instead to "force" a push down:
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
LATERAL JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
WHERE a.employee_id = emp.employee_id
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The join condition isn't really needed, but it dosn't hurt either
add a comment |
I am not sure if this is really more efficient, but you could try to join to a derived table that applies the limit.
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The first derived table only selects 100 rows, and the join/group by should then only be done for those 100 employees.
If the optimizer doesn't push that down, you could try a lateral join instead to "force" a push down:
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
LATERAL JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
WHERE a.employee_id = emp.employee_id
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The join condition isn't really needed, but it dosn't hurt either
add a comment |
I am not sure if this is really more efficient, but you could try to join to a derived table that applies the limit.
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The first derived table only selects 100 rows, and the join/group by should then only be done for those 100 employees.
If the optimizer doesn't push that down, you could try a lateral join instead to "force" a push down:
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
LATERAL JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
WHERE a.employee_id = emp.employee_id
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The join condition isn't really needed, but it dosn't hurt either
I am not sure if this is really more efficient, but you could try to join to a derived table that applies the limit.
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The first derived table only selects 100 rows, and the join/group by should then only be done for those 100 employees.
If the optimizer doesn't push that down, you could try a lateral join instead to "force" a push down:
select emp.employee_id, emp.salary, adr.address_arr
from (
SELECT employee_id, salary, address_arr
FROM employee
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
LIMIT 100
OFFSET 0
) as emp
LATERAL JOIN (
SELECT a.employee_id, ARRAY_AGG(a.address) as address_arr
FROM addresses a
WHERE a.employee_id = emp.employee_id
GROUP BY employee_id
) as adr ON adr.employee_id = emp.employee_id;
The join condition isn't really needed, but it dosn't hurt either
answered Feb 8 at 7:41
a_horse_with_no_namea_horse_with_no_name
40.1k776112
40.1k776112
add a comment |
add a comment |
Maybe
SELECT employee.employee_id, payroll.salary, ARRAY_AGG(addresses.address)
FROM employee
INNER JOIN addresses ON addresses.employee_id = employee.employee_id
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
GROUP BY employee.employee_id
LIMIT 100
OFFSET 0
?
And - do you really need in records where no appropriate records in payroll
table which leads to NULLs in payroll.salary
? Maybe, INNER JOIN is enough?
The query is incorrect. payroll.salary needs to be included in GROUP BY clause, which will unoptimise the query further
– nimeshkiranverma
Feb 8 at 7:48
@nimeshkiranverma payroll.salary needs to be included in GROUP BY clause you may wrap it using any aggregate function which can be applied to this field datatype.
– Akina
Feb 8 at 7:50
add a comment |
Maybe
SELECT employee.employee_id, payroll.salary, ARRAY_AGG(addresses.address)
FROM employee
INNER JOIN addresses ON addresses.employee_id = employee.employee_id
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
GROUP BY employee.employee_id
LIMIT 100
OFFSET 0
?
And - do you really need in records where no appropriate records in payroll
table which leads to NULLs in payroll.salary
? Maybe, INNER JOIN is enough?
The query is incorrect. payroll.salary needs to be included in GROUP BY clause, which will unoptimise the query further
– nimeshkiranverma
Feb 8 at 7:48
@nimeshkiranverma payroll.salary needs to be included in GROUP BY clause you may wrap it using any aggregate function which can be applied to this field datatype.
– Akina
Feb 8 at 7:50
add a comment |
Maybe
SELECT employee.employee_id, payroll.salary, ARRAY_AGG(addresses.address)
FROM employee
INNER JOIN addresses ON addresses.employee_id = employee.employee_id
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
GROUP BY employee.employee_id
LIMIT 100
OFFSET 0
?
And - do you really need in records where no appropriate records in payroll
table which leads to NULLs in payroll.salary
? Maybe, INNER JOIN is enough?
Maybe
SELECT employee.employee_id, payroll.salary, ARRAY_AGG(addresses.address)
FROM employee
INNER JOIN addresses ON addresses.employee_id = employee.employee_id
LEFT JOIN payroll on payroll.employee_id = employee.employee_id
WHERE employee.employee_id < 1000000
GROUP BY employee.employee_id
LIMIT 100
OFFSET 0
?
And - do you really need in records where no appropriate records in payroll
table which leads to NULLs in payroll.salary
? Maybe, INNER JOIN is enough?
edited Feb 8 at 7:21
answered Feb 8 at 7:15
AkinaAkina
4,0461311
4,0461311
The query is incorrect. payroll.salary needs to be included in GROUP BY clause, which will unoptimise the query further
– nimeshkiranverma
Feb 8 at 7:48
@nimeshkiranverma payroll.salary needs to be included in GROUP BY clause you may wrap it using any aggregate function which can be applied to this field datatype.
– Akina
Feb 8 at 7:50
add a comment |
The query is incorrect. payroll.salary needs to be included in GROUP BY clause, which will unoptimise the query further
– nimeshkiranverma
Feb 8 at 7:48
@nimeshkiranverma payroll.salary needs to be included in GROUP BY clause you may wrap it using any aggregate function which can be applied to this field datatype.
– Akina
Feb 8 at 7:50
The query is incorrect. payroll.salary needs to be included in GROUP BY clause, which will unoptimise the query further
– nimeshkiranverma
Feb 8 at 7:48
The query is incorrect. payroll.salary needs to be included in GROUP BY clause, which will unoptimise the query further
– nimeshkiranverma
Feb 8 at 7:48
@nimeshkiranverma payroll.salary needs to be included in GROUP BY clause you may wrap it using any aggregate function which can be applied to this field datatype.
– Akina
Feb 8 at 7:50
@nimeshkiranverma payroll.salary needs to be included in GROUP BY clause you may wrap it using any aggregate function which can be applied to this field datatype.
– Akina
Feb 8 at 7:50
add a comment |
I am new to this and had some help , here's what I came up with:
with t as (SELECT employee.employee_id, salary FROM employee LEFT JOIN payroll on payroll.employee_id = employee.employee_id WHERE employee.employee_id < 1000000 LIMIT 100 OFFSET 0)
select t.employee_id, max(t.salary), ARRAY_AGG(address) as address_arr from address left join t on address.employee_id = t.employee_id where address.employee_id = t.employee_id group by t.employee_id;
explain analyze yields
New contributor
add a comment |
I am new to this and had some help , here's what I came up with:
with t as (SELECT employee.employee_id, salary FROM employee LEFT JOIN payroll on payroll.employee_id = employee.employee_id WHERE employee.employee_id < 1000000 LIMIT 100 OFFSET 0)
select t.employee_id, max(t.salary), ARRAY_AGG(address) as address_arr from address left join t on address.employee_id = t.employee_id where address.employee_id = t.employee_id group by t.employee_id;
explain analyze yields
New contributor
add a comment |
I am new to this and had some help , here's what I came up with:
with t as (SELECT employee.employee_id, salary FROM employee LEFT JOIN payroll on payroll.employee_id = employee.employee_id WHERE employee.employee_id < 1000000 LIMIT 100 OFFSET 0)
select t.employee_id, max(t.salary), ARRAY_AGG(address) as address_arr from address left join t on address.employee_id = t.employee_id where address.employee_id = t.employee_id group by t.employee_id;
explain analyze yields
New contributor
I am new to this and had some help , here's what I came up with:
with t as (SELECT employee.employee_id, salary FROM employee LEFT JOIN payroll on payroll.employee_id = employee.employee_id WHERE employee.employee_id < 1000000 LIMIT 100 OFFSET 0)
select t.employee_id, max(t.salary), ARRAY_AGG(address) as address_arr from address left join t on address.employee_id = t.employee_id where address.employee_id = t.employee_id group by t.employee_id;
explain analyze yields
New contributor
New contributor
answered 13 mins ago
Rahul AhujaRahul Ahuja
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f229214%2fhow-to-apply-outer-limit-offset-and-filters-in-the-subquery-to-avoid-grouping-ov%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
PS. LIMIT without ORDER BY gives you 100 random records from the whole data array... do you really need in that?
– Akina
Feb 8 at 7:44
You should present us the simplest query you can which still have the issue. If you remove the left join on PAYROLL, does the problem go away?
– jjanes
Feb 9 at 15:18