pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: Robert Poor <rdpoor@gmail.com>
To: Scott Marlowe <scott.marlowe@gmail.com>
To: pgsql-performance@postgresql.org
Subject: Re: slow self-join query
Date: Mon, 19 Mar 2012 09:27:06 -0500
Message-ID: <4F66FBEA020000250004640C@gw.wicourts.gov> (raw)
In-Reply-To: <CAGHqdqX7mh8y_=ceeZbrzNkHwDj7qQR+Ge=8voKLhPfQC6PrOw@mail.gmail.com>
References: <CAGHqdqWxW2F=xX4N7Haxe_qEPSLA82z_A+-P6_tMAGHUk3wGqQ@mail.gmail.com>
	<CAOR=d=00LZM8=YCR_fqVFyKJjmidYMMLHm_v=+a0qXYfkCVkeQ@mail.gmail.com>
	<CAGHqdqUuE8rKRqfORwRMOBBSQj+QLd6trdE5b2BC3OhuOgFoQw@mail.gmail.com>
	<CAOR=d=05GXYEB4n8WJNFzN8__xT2pFVdEXxhodPB9fzsSw+1Pw@mail.gmail.com>
	<CAGHqdqXqLNXQ48cidqP+nS4Zp+22xWrOanMkJX4tY4jri1eYxQ@mail.gmail.com>
	<CAOR=d=3Td3ZchcfYwQVMwxm+jN2BuEmHCZC6n3s4tBxdT=GJVA@mail.gmail.com>
	<CAGHqdqVk1ZCNY-xP0D=fBLLFCFkjNQEoDoYBONXYv-iHrQTdpA@mail.gmail.com>
	<CAOR=d=1OYRF8mHJf6VPGe3rimeT3jim8ps0h76SiVtuPV9dDmQ@mail.gmail.com>
	<CAGHqdqU4C7JhdCdb47OHO6KTKa4YwxSAZffnMyLYsVD0Bt1T5g@mail.gmail.com>
	<CAOR=d=3c2RYFCSR+TCLkuLWZ+kK5433SKqJtv_h2qdZpoER_yQ@mail.gmail.com>
	<CAGHqdqX7mh8y_=ceeZbrzNkHwDj7qQR+Ge=8voKLhPfQC6PrOw@mail.gmail.com>

Robert Poor <rdpoor@gmail.com> wrote:
> among users that follow leader 321, who are the most widely
> followed leaders?", or more formally, find all the users that are
> followers of user 321 (via inner join on F1)  Of those users,
> tally up their leaders so we know which leaders are most popular.
 
It sounds like you're looking for something like this:
 
SELECT leader_id, count(*) as count
  FROM user_associations x
  WHERE exists
        (
          SELECT * FROM user_associations y
            WHERE y.follower_id = x.follower_id
              AND y.leader_id = 321
        )
  GROUP BY leader_id
;
 
>  create_table "user_associations", :force => true do |t|
>   t.integer  "follower_id"
>   t.integer  "leader_id"
>  end
 
I don't really know what that means.  In the future, it would make
things easier on those who are trying to help if you either post the
SQL form or go into psql and type `\d tablename`.
 
-Kevin



view thread (11+ messages)  latest in thread

Message-ID: <4F66FBEA020000250004640C@gw.wicourts.gov>
Permalink:  ../4F66FBEA020000250004640C@gw.wicourts.gov/
Also on:    postgresql.org/message-id/4F66FBEA020000250004640C@gw.wicourts.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: Kevin.Grittner@wicourts.gov, rdpoor@gmail.com, scott.marlowe@gmail.com
  Subject: Re: slow self-join query
  In-Reply-To: <4F66FBEA020000250004640C@gw.wicourts.gov>
  References: <CAGHqdqWxW2F=xX4N7Haxe_qEPSLA82z_A+-P6_tMAGHUk3wGqQ@mail.gmail.com> <CAOR=d=00LZM8=YCR_fqVFyKJjmidYMMLHm_v=+a0qXYfkCVkeQ@mail.gmail.com> <CAGHqdqUuE8rKRqfORwRMOBBSQj+QLd6trdE5b2BC3OhuOgFoQw@mail.gmail.com> <CAOR=d=05GXYEB4n8WJNFzN8__xT2pFVdEXxhodPB9fzsSw+1Pw@mail.gmail.com> <CAGHqdqXqLNXQ48cidqP+nS4Zp+22xWrOanMkJX4tY4jri1eYxQ@mail.gmail.com> <CAOR=d=3Td3ZchcfYwQVMwxm+jN2BuEmHCZC6n3s4tBxdT=GJVA@mail.gmail.com> <CAGHqdqVk1ZCNY-xP0D=fBLLFCFkjNQEoDoYBONXYv-iHrQTdpA@mail.gmail.com> <CAOR=d=1OYRF8mHJf6VPGe3rimeT3jim8ps0h76SiVtuPV9dDmQ@mail.gmail.com> <CAGHqdqU4C7JhdCdb47OHO6KTKa4YwxSAZffnMyLYsVD0Bt1T5g@mail.gmail.com> <CAOR=d=3c2RYFCSR+TCLkuLWZ+kK5433SKqJtv_h2qdZpoER_yQ@mail.gmail.com> <CAGHqdqX7mh8y_=ceeZbrzNkHwDj7qQR+Ge=8voKLhPfQC6PrOw@mail.gmail.com> <4F66FBEA020000250004640C@gw.wicourts.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox