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.

  • 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
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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.