SELECT operation with four joins results in bad performance





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2















I'm having problems with a slow query. The query purpose is to get doers ids for X job who didn't exceed specified limits on realizations and also are in range of possible job places.



Resources





  • Query:



    SELECT DISTINCT doers.id FROM doers
    JOIN doer_locations dl ON dl.doer_id = doers.id
    JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
    LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id AND jr.status IN (1, 2, 3, 4)
    LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id AND jrpd.status IN (1, 2, 3, 4)
    WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
    GROUP BY doers.id, jp.id
    HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


  • Depesz explain


  • Raw explain analyze


  • Simplified Schema



Consideration



I'm not sure if I read the explain correctly but it seems it loses on performance especially when it calculates stuff on the run also HAVING COUNT(DISTINCT) seems pretty expensive.



Additional information



The type of both the lat and long columns is float.










share|improve this question
















bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Regarding doers RIGHT JOIN doer_locations. Is there ever a doer_location without a corresponding doer?

    – Lennart
    Oct 1 '18 at 11:30











  • Shouldn't happen.

    – mist
    Oct 1 '18 at 11:39











  • What is the purpose of the RIGHT JOIN then? Can't you replace it with a plain JOIN?

    – Lennart
    Oct 1 '18 at 11:43






  • 1





    What are the types of lat and long?

    – ypercubeᵀᴹ
    Oct 2 '18 at 8:38






  • 2





    You could use gist indexes and if these are actual geo location points, you could have more accurate results with ll_to_earth() function (and gist indexes). See this answer: dba.stackexchange.com/questions/158349/…

    – ypercubeᵀᴹ
    Oct 2 '18 at 9:28




















2















I'm having problems with a slow query. The query purpose is to get doers ids for X job who didn't exceed specified limits on realizations and also are in range of possible job places.



Resources





  • Query:



    SELECT DISTINCT doers.id FROM doers
    JOIN doer_locations dl ON dl.doer_id = doers.id
    JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
    LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id AND jr.status IN (1, 2, 3, 4)
    LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id AND jrpd.status IN (1, 2, 3, 4)
    WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
    GROUP BY doers.id, jp.id
    HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


  • Depesz explain


  • Raw explain analyze


  • Simplified Schema



Consideration



I'm not sure if I read the explain correctly but it seems it loses on performance especially when it calculates stuff on the run also HAVING COUNT(DISTINCT) seems pretty expensive.



Additional information



The type of both the lat and long columns is float.










share|improve this question
















bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Regarding doers RIGHT JOIN doer_locations. Is there ever a doer_location without a corresponding doer?

    – Lennart
    Oct 1 '18 at 11:30











  • Shouldn't happen.

    – mist
    Oct 1 '18 at 11:39











  • What is the purpose of the RIGHT JOIN then? Can't you replace it with a plain JOIN?

    – Lennart
    Oct 1 '18 at 11:43






  • 1





    What are the types of lat and long?

    – ypercubeᵀᴹ
    Oct 2 '18 at 8:38






  • 2





    You could use gist indexes and if these are actual geo location points, you could have more accurate results with ll_to_earth() function (and gist indexes). See this answer: dba.stackexchange.com/questions/158349/…

    – ypercubeᵀᴹ
    Oct 2 '18 at 9:28
















2












2








2








I'm having problems with a slow query. The query purpose is to get doers ids for X job who didn't exceed specified limits on realizations and also are in range of possible job places.



Resources





  • Query:



    SELECT DISTINCT doers.id FROM doers
    JOIN doer_locations dl ON dl.doer_id = doers.id
    JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
    LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id AND jr.status IN (1, 2, 3, 4)
    LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id AND jrpd.status IN (1, 2, 3, 4)
    WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
    GROUP BY doers.id, jp.id
    HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


  • Depesz explain


  • Raw explain analyze


  • Simplified Schema



Consideration



I'm not sure if I read the explain correctly but it seems it loses on performance especially when it calculates stuff on the run also HAVING COUNT(DISTINCT) seems pretty expensive.



Additional information



The type of both the lat and long columns is float.










share|improve this question
















I'm having problems with a slow query. The query purpose is to get doers ids for X job who didn't exceed specified limits on realizations and also are in range of possible job places.



Resources





  • Query:



    SELECT DISTINCT doers.id FROM doers
    JOIN doer_locations dl ON dl.doer_id = doers.id
    JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
    LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id AND jr.status IN (1, 2, 3, 4)
    LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id AND jrpd.status IN (1, 2, 3, 4)
    WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
    GROUP BY doers.id, jp.id
    HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


  • Depesz explain


  • Raw explain analyze


  • Simplified Schema



Consideration



I'm not sure if I read the explain correctly but it seems it loses on performance especially when it calculates stuff on the run also HAVING COUNT(DISTINCT) seems pretty expensive.



Additional information



The type of both the lat and long columns is float.







postgresql query-performance postgresql-10






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 2 '18 at 22:16









MDCCL

6,85331745




6,85331745










asked Oct 1 '18 at 11:22









mistmist

162




162





bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 4 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Regarding doers RIGHT JOIN doer_locations. Is there ever a doer_location without a corresponding doer?

    – Lennart
    Oct 1 '18 at 11:30











  • Shouldn't happen.

    – mist
    Oct 1 '18 at 11:39











  • What is the purpose of the RIGHT JOIN then? Can't you replace it with a plain JOIN?

    – Lennart
    Oct 1 '18 at 11:43






  • 1





    What are the types of lat and long?

    – ypercubeᵀᴹ
    Oct 2 '18 at 8:38






  • 2





    You could use gist indexes and if these are actual geo location points, you could have more accurate results with ll_to_earth() function (and gist indexes). See this answer: dba.stackexchange.com/questions/158349/…

    – ypercubeᵀᴹ
    Oct 2 '18 at 9:28





















  • Regarding doers RIGHT JOIN doer_locations. Is there ever a doer_location without a corresponding doer?

    – Lennart
    Oct 1 '18 at 11:30











  • Shouldn't happen.

    – mist
    Oct 1 '18 at 11:39











  • What is the purpose of the RIGHT JOIN then? Can't you replace it with a plain JOIN?

    – Lennart
    Oct 1 '18 at 11:43






  • 1





    What are the types of lat and long?

    – ypercubeᵀᴹ
    Oct 2 '18 at 8:38






  • 2





    You could use gist indexes and if these are actual geo location points, you could have more accurate results with ll_to_earth() function (and gist indexes). See this answer: dba.stackexchange.com/questions/158349/…

    – ypercubeᵀᴹ
    Oct 2 '18 at 9:28



















Regarding doers RIGHT JOIN doer_locations. Is there ever a doer_location without a corresponding doer?

– Lennart
Oct 1 '18 at 11:30





Regarding doers RIGHT JOIN doer_locations. Is there ever a doer_location without a corresponding doer?

– Lennart
Oct 1 '18 at 11:30













Shouldn't happen.

– mist
Oct 1 '18 at 11:39





Shouldn't happen.

– mist
Oct 1 '18 at 11:39













What is the purpose of the RIGHT JOIN then? Can't you replace it with a plain JOIN?

– Lennart
Oct 1 '18 at 11:43





What is the purpose of the RIGHT JOIN then? Can't you replace it with a plain JOIN?

– Lennart
Oct 1 '18 at 11:43




1




1





What are the types of lat and long?

– ypercubeᵀᴹ
Oct 2 '18 at 8:38





What are the types of lat and long?

– ypercubeᵀᴹ
Oct 2 '18 at 8:38




2




2





You could use gist indexes and if these are actual geo location points, you could have more accurate results with ll_to_earth() function (and gist indexes). See this answer: dba.stackexchange.com/questions/158349/…

– ypercubeᵀᴹ
Oct 2 '18 at 9:28







You could use gist indexes and if these are actual geo location points, you could have more accurate results with ll_to_earth() function (and gist indexes). See this answer: dba.stackexchange.com/questions/158349/…

– ypercubeᵀᴹ
Oct 2 '18 at 9:28












1 Answer
1






active

oldest

votes


















0














    --I think this is mandatory for your query performance:
--Because you do joins using this columns from parent to child

create index on doer_locations(doer_id);
create index on job_realizations(job_place_id);
create index on job_realizations(doer_id);

--Maybe very big and slowdown other operations...
--create index on job_realizations(lat);
--create index on doer_locations(lat);
--create index on job_realizations(lng);
--create index on doer_locations(lng);

--Maybe not mandatory:
create index on job_realization(realizations_per_place);
create index on job_realization(realizations_per_place_per_doer);

SELECT DISTINCT doers.id FROM doers
JOIN doer_locations dl ON dl.doer_id = doers.id
JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id
LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id
WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
GROUP BY doers.id, jp.id
HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


Please try and if it solve your problem select it as the right answer.






share|improve this answer
























  • As you could see in the explain I posted, I have tried with btree indexes on most of those attributes. I haven't tried with indexes on realizations_per_place / realizations_per_place_per_doer though.

    – mist
    Oct 2 '18 at 7:40












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f218960%2fselect-operation-with-four-joins-results-in-bad-performance%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














    --I think this is mandatory for your query performance:
--Because you do joins using this columns from parent to child

create index on doer_locations(doer_id);
create index on job_realizations(job_place_id);
create index on job_realizations(doer_id);

--Maybe very big and slowdown other operations...
--create index on job_realizations(lat);
--create index on doer_locations(lat);
--create index on job_realizations(lng);
--create index on doer_locations(lng);

--Maybe not mandatory:
create index on job_realization(realizations_per_place);
create index on job_realization(realizations_per_place_per_doer);

SELECT DISTINCT doers.id FROM doers
JOIN doer_locations dl ON dl.doer_id = doers.id
JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id
LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id
WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
GROUP BY doers.id, jp.id
HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


Please try and if it solve your problem select it as the right answer.






share|improve this answer
























  • As you could see in the explain I posted, I have tried with btree indexes on most of those attributes. I haven't tried with indexes on realizations_per_place / realizations_per_place_per_doer though.

    – mist
    Oct 2 '18 at 7:40
















0














    --I think this is mandatory for your query performance:
--Because you do joins using this columns from parent to child

create index on doer_locations(doer_id);
create index on job_realizations(job_place_id);
create index on job_realizations(doer_id);

--Maybe very big and slowdown other operations...
--create index on job_realizations(lat);
--create index on doer_locations(lat);
--create index on job_realizations(lng);
--create index on doer_locations(lng);

--Maybe not mandatory:
create index on job_realization(realizations_per_place);
create index on job_realization(realizations_per_place_per_doer);

SELECT DISTINCT doers.id FROM doers
JOIN doer_locations dl ON dl.doer_id = doers.id
JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id
LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id
WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
GROUP BY doers.id, jp.id
HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


Please try and if it solve your problem select it as the right answer.






share|improve this answer
























  • As you could see in the explain I posted, I have tried with btree indexes on most of those attributes. I haven't tried with indexes on realizations_per_place / realizations_per_place_per_doer though.

    – mist
    Oct 2 '18 at 7:40














0












0








0







    --I think this is mandatory for your query performance:
--Because you do joins using this columns from parent to child

create index on doer_locations(doer_id);
create index on job_realizations(job_place_id);
create index on job_realizations(doer_id);

--Maybe very big and slowdown other operations...
--create index on job_realizations(lat);
--create index on doer_locations(lat);
--create index on job_realizations(lng);
--create index on doer_locations(lng);

--Maybe not mandatory:
create index on job_realization(realizations_per_place);
create index on job_realization(realizations_per_place_per_doer);

SELECT DISTINCT doers.id FROM doers
JOIN doer_locations dl ON dl.doer_id = doers.id
JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id
LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id
WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
GROUP BY doers.id, jp.id
HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


Please try and if it solve your problem select it as the right answer.






share|improve this answer













    --I think this is mandatory for your query performance:
--Because you do joins using this columns from parent to child

create index on doer_locations(doer_id);
create index on job_realizations(job_place_id);
create index on job_realizations(doer_id);

--Maybe very big and slowdown other operations...
--create index on job_realizations(lat);
--create index on doer_locations(lat);
--create index on job_realizations(lng);
--create index on doer_locations(lng);

--Maybe not mandatory:
create index on job_realization(realizations_per_place);
create index on job_realization(realizations_per_place_per_doer);

SELECT DISTINCT doers.id FROM doers
JOIN doer_locations dl ON dl.doer_id = doers.id
JOIN job_places jp ON (jp.lat - 0.3147625620715557) < dl.lat AND (jp.lat + 0.3147625620715557) > dl.lat AND (jp.lng - 0.5001626620527362) < dl.lng AND (jp.lng + 0.5001626620527362) > dl.lng
LEFT JOIN job_realizations jr ON jr.job_place_id = jp.id
LEFT JOIN job_realizations jrpd ON jrpd.job_place_id = jp.id AND jrpd.doer_id = doers.id
WHERE (jp.job_id = 1 AND doers.id IS NOT NULL)
GROUP BY doers.id, jp.id
HAVING COUNT(DISTINCT jr.id) < jp.realizations_per_place AND COUNT(DISTINCT jrpd.id) < jp.realizations_per_place_per_doer


Please try and if it solve your problem select it as the right answer.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 1 '18 at 19:34









Luciano Andress MartiniLuciano Andress Martini

745518




745518













  • As you could see in the explain I posted, I have tried with btree indexes on most of those attributes. I haven't tried with indexes on realizations_per_place / realizations_per_place_per_doer though.

    – mist
    Oct 2 '18 at 7:40



















  • As you could see in the explain I posted, I have tried with btree indexes on most of those attributes. I haven't tried with indexes on realizations_per_place / realizations_per_place_per_doer though.

    – mist
    Oct 2 '18 at 7:40

















As you could see in the explain I posted, I have tried with btree indexes on most of those attributes. I haven't tried with indexes on realizations_per_place / realizations_per_place_per_doer though.

– mist
Oct 2 '18 at 7:40





As you could see in the explain I posted, I have tried with btree indexes on most of those attributes. I haven't tried with indexes on realizations_per_place / realizations_per_place_per_doer though.

– mist
Oct 2 '18 at 7:40


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f218960%2fselect-operation-with-four-joins-results-in-bad-performance%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

الفوسفات في المغرب

Four equal circles intersect: What is the area of the small shaded portion and its height

جامعة ليفربول