How to apply outer limit offset and filters in the subquery to avoid grouping over the complete table used in...
Isn't a semicolon (';') needed after a function declaration in C++?
Can I do anything else with aspersions other than cast them?
What if you do not believe in the project benefits?
Why do we divide Permutations to get to Combinations?
Is there a way to pause a running process on Linux systems and resume later?
Multiplying elements of a list
How do I know my password or backup information is not being shared when creating a new wallet?
How can I make my enemies feel real and make combat more engaging?
Taking an academic pseudonym?
Will linear voltage regulator step up current?
How many copper coins fit inside a cubic foot?
Why is Shelob considered evil?
How bad is a Computer Science course that doesn't teach Design Patterns?
Can I legally make a website about boycotting a certain company?
How should I ship cards?
What is the reason behind this musical reference to Pinocchio in the Close Encounters main theme?
How can I differentiate duration vs starting time
Can a Hydra make multiple opportunity attacks at once?
How do I add a strong "onion flavor" to the biryani (in restaurant style)?
Does the Resurrection spell consume material components if the target isn’t willing to be resurrected?
Translation for threshold (figuratively)
Use intersection in field calculator
What does @ mean in a hostname in DNS configuration?
Are there any spells or magic items that allow for making of ‘logic gates or wires’?
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 6 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