Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZB6ma-00023M-77 for pgsql-sql@arkaria.postgresql.org; Fri, 03 Jul 2015 19:37:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZB6mY-0002A1-CV for pgsql-sql@arkaria.postgresql.org; Fri, 03 Jul 2015 19:37:54 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZB6lC-0000jR-30 for pgsql-sql@postgresql.org; Fri, 03 Jul 2015 19:36:30 +0000 Received: from zql.com ([206.222.31.58]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZB6l4-0008JF-Fj for pgsql-sql@postgresql.org; Fri, 03 Jul 2015 19:36:28 +0000 Received: from localhost ([127.0.0.1]) by zql.com with smtp (Exim 4.68) (envelope-from ) id 1ZB6l2-0005qI-Bp; Fri, 03 Jul 2015 15:36:20 -0400 From: "Greg Sabino Mullane" To: pgsql-sql@postgresql.org Subject: Re: Help with 'contestant' query X-PGP-Key: 2529 DF6A B8F7 9407 E944 45B4 BC9B 9067 1496 4AC8 X-Request-PGP: http://www.biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 In-Reply-To: Content-type: text/plain; charset=UTF-8 Date: Fri, 3 Jul 2015 19:36:20 -0000 X-Mailer: JoyMail 3.1.0 Message-ID: X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org -----BEGIN PGP SIGNED MESSAGE----- Hash: RIPEMD160 John Sherwood asked: > SELECT contestants.*, sum(entries.worth) as db_entries, count(entries.id) > as db_actions FROM "contestants" INNER JOIN "entries" ON > "entries"."contestant_id" = "contestants"."id" WHERE > "entries"."campaign_id" IN (SELECT id FROM "campaigns" WHERE > "campaigns"."site_id" = $1) AND (entries.status != 'Invalid') GROUP BY > contestants.id ORDER BY db_actions desc LIMIT 20 OFFSET 0 If you have a lot of 'Invalid' entries, a partial index will help: CREATE INDEX index_entries_on_campaign_id_valid ON entries(campaign_id) WHERE status <> 'Invalid'; > Here's the explain: An EXPLAIN ANALYZE is always better, fwiw. I noticed you have a contestants.* plus a GROUP BY contestants.id, which suggests that a) this is not the exact query, or b) id is the only column in that table. Either way, if you only need the contestant id, you can remove that table from the query, and just use entries.contestant_id instead, getting rid of the IN() clause in the process: SELECT e.contestant_id, SUM(e.worth) AS db_entries, COUNT(e.id) AS db_actions FROM entries e JOIN campaigns c ON (c.id = e.campaign_id AND c.site_id = $1) AND e.status <> 'Invalid' GROUP BY e.contestant_id ORDER BY db_actions DESC LIMIT 20 OFFSET 0; - -- Greg Sabino Mullane greg@turnstep.com End Point Corporation http://www.endpoint.com/ PGP Key: 0x14964AC8 201507031516 http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 -----BEGIN PGP SIGNATURE----- iEYEAREDAAYFAlWW4/UACgkQvJuQZxSWSsgzRgCeLrZAoGZPZV/FSVmSAChFT3lS FSkAoOEEAbH6/RGMqzNxEaW8Fq6OpA0/ =5/ys -----END PGP SIGNATURE----- -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql