-3

I am trying to get a list of users who did a pull request on any repos with a specified language.

SELECT distinct(actor_id) as id FROM pull_requests 
JOIN (SELECT id FROM repos WHERE language = 'javascript') as res 
  ON pull_requests.repo_id = res.id

I've been trying to improve the performance of this query. Currently it takes 2sec+ to run.

3 Answers 3

1

First thing -- try a semi-join:

SELECT distinct actor_id as id
FROM pull_requests p
where exists (
  select null
  from repos r
  where p.repo_id = r.id and r.language = 'javascript'
)

Secondly -- verify your distinct is necessary based on this change. It probably is in this case, but semi-joins can often times eliminate the need for distinct where the it's used as a crutch for a 1::many returning multiple rows -- the exists will not multiply results based on multiple matches in the repos table.

0

Try this:

SELECT distinct(A.actor_id) as id FROM pull_requests AS A
INNER JOIN repos AS B ON A.repo_id = B.id AND B.language = 'javascript'

You may need to index on either repo_id and/or id field

0

Try using IN

SELECT distinct(actor_id) as id 
FROM pull_requests 
WHERE pull_requests.repo_id IN (SELECT id FROM repos WHERE language = 'javascript')
1
  • I certainly don't think this answer is wrong, but my $0.02 is that PostgreSQL seems to have dramatic performance differences using an in (subquery) versus a semi-join. Later version of Oracle's CBO seem to automatically adjust the execution plan to what makes sense, but it seems (based on my limited understanding of things) that Pg does literally what you tell it to.
    – Hambone
    Feb 18, 2016 at 3:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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