Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VvFvJ-0004Xa-QX for pgsql-performance@arkaria.postgresql.org; Tue, 24 Dec 2013 00:32:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VvFvI-0000IM-ET for pgsql-performance@arkaria.postgresql.org; Tue, 24 Dec 2013 00:32:36 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VvFvH-0000IF-Fr for pgsql-performance@postgresql.org; Tue, 24 Dec 2013 00:32:35 +0000 Received: from [2404:130:0:10::40:2] (helo=mail.catalyst.net.nz) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VvFvB-0000W9-RB for pgsql-performance@postgresql.org; Tue, 24 Dec 2013 00:32:33 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.catalyst.net.nz (Postfix) with ESMTP id 4859032FBE; Tue, 24 Dec 2013 13:32:25 +1300 (NZDT) X-Virus-Scanned: Debian amavisd-new at catalyst.net.nz Received: from mail.catalyst.net.nz ([127.0.0.1]) by localhost (bertrand.catalyst.net.nz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5eeQ4tiNUmEC; Tue, 24 Dec 2013 13:32:24 +1300 (NZDT) Received: from [192.168.1.64] (122-60-64-230.jetstream.xtra.co.nz [122.60.64.230]) (Authenticated sender: mark.kirkwood) by mail.catalyst.net.nz (Postfix) with ESMTPSA id D009733504; Tue, 24 Dec 2013 13:32:23 +1300 (NZDT) Message-ID: <52B8D613.9030309@catalyst.net.nz> Date: Tue, 24 Dec 2013 13:32:19 +1300 From: Mark Kirkwood User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Shaun Thomas , Kai Sellgren , pgsql-performance@postgresql.org Subject: Re: Optimizing a query References: <52B332B3.9050402@optionshouse.com> In-Reply-To: <52B332B3.9050402@optionshouse.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2404:130:0:10::40:2 (failed) X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 20/12/13 06:53, Shaun Thomas wrote: > On 12/17/2013 08:48 PM, Kai Sellgren wrote: > > This is your select: > >> SELECT * >> FROM "Log" >> LEFT JOIN "NewsArticle" ON "NewsArticle".id = "Log"."targetId" AND >> "Log"."targetType" = 'NewsArticle' >> ORDER BY "Log"."createdAt" DESC >> LIMIT 10 > > This is your index: > >> CREATE INDEX "Log_targetId_targetType_idx" >> ON "Log" >> USING btree >> ("targetId", "targetType" COLLATE pg_catalog."default"); > > Unfortunately, this won't help you. You are not matching on any IDs you > indexed, aside from joining against the article table. You have no WHERE > clause to restrict the data set, so it absolutely must read the entire > table to find the most recent records. Without an index on "createdAt", > how is it supposed to know what the ten most recent records are? > > Add an index to the createdAt column: > > CREATE INDEX idx_log_createdat ON "Log" (createdAt DESC); > > Using that, it should get the ten most recent Log records almost > immediately, including associated article content. > Also, might be worth creating an index on NewsArticle(id) so that the join to this table does not require a full table scan: CREATE INDEX newsarticle_id_idx ON "NewsArticle" (id); (probably not a problem when you only have a few articles - but will be as the volume increases over time). Regards Mark -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance