Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VthnP-0001Bc-Nk for pgsql-performance@arkaria.postgresql.org; Thu, 19 Dec 2013 17:54:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VthnP-0007Ol-3l for pgsql-performance@arkaria.postgresql.org; Thu, 19 Dec 2013 17:54:03 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VthnN-0007Oe-TM for pgsql-performance@postgresql.org; Thu, 19 Dec 2013 17:54:02 +0000 Received: from smtp2.peak6.com ([38.98.137.9]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VthnK-0000S1-PL for pgsql-performance@postgresql.org; Thu, 19 Dec 2013 17:54:01 +0000 X-AuditID: 0a02cb20-b7fab6d000000b57-1a-52b332b395f9 Received: from sswchi5pcas2.peak6.net (Unknown_Domain [10.100.13.24]) by smtp2.peak6.com (Symantec Messaging Gateway) with SMTP id 66.2D.02903.3B233B25; Thu, 19 Dec 2013 11:53:55 -0600 (CST) Received: from [10.10.201.64] (10.10.201.64) by sswchi5pcas2.peak6.net (10.100.13.25) with Microsoft SMTP Server (TLS) id 14.3.158.1; Thu, 19 Dec 2013 11:53:55 -0600 Message-ID: <52B332B3.9050402@optionshouse.com> Date: Thu, 19 Dec 2013 11:53:55 -0600 From: Shaun Thomas User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 MIME-Version: 1.0 To: Kai Sellgren , Subject: Re: Optimizing a query References: In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed content-transfer-encoding: 7bit X-Originating-IP: [10.10.201.64] X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrFIsWRmVeSWpSXmKPExsXClcIrobvZaHOQwec/Zhbvm6Mtrs88x+zA 5LFz1l12jwv7prEHMEU1MNok5uXllySWpCqkpBYn2yr5F5Rk5ucVe+SXFqcquGQWJ+ckZuam FikpZKbYKhkrKRTkJCan5qbmldgqJRYUpOalKNlxKWAAG6CyzDyF1Lzk/JTMvHRbJc9gf10L C1NLXUMlu3hkkNDJkjHh7R+mgg8cFbee72ZrYOxm72Lk5JAQMJG4c2EOI4QtJnHh3nq2LkYu DiGBA4wSm/9sYYJwVjJKND/exgJSxSugL3Fn3iawDhYBVYnWA5fAJrEJ6EpM6l/BBmKLCoRJ tF+YyQxRLyhxcuYTsF4RAR+J/1tugNnCApoS1/svs3YxcgAtCJC4sckEJMwpECix8vxWsDHM AhYSi98cZIew5SW2v50DNlIIaO2K5olsEEcrSPy/8o5pAqPgLCTbZiFpn4WkfQEj8ypG8eKy xOSMTLOC4tySAiO9gtTEbDO9vNSSTYygCGU6rbCDcXOP1SFGAQ5GJR7eA/82BgmxJpYVV+Ye YpTgYFYS4Z2nsDlIiDclsbIqtSg/vqg0J7X4EKMPMDgmMkuJJucDk0deSbyhsamBqaWpoam5 qaERDmElcV6unKlBQgLpwKSRnZpakFoEM46Jg1OqgVH1VZPF82f/dQ9WfX9t66qz4XXIgsSV zp5+vTd6Thgc3fJ1RaHqM/ma7F3Lv2TNep7UKn/73Jq+VdURW3rbq3iNNNPXbL5cEXGkfGYV j9OjGWxZ0TOmsqU8m+PdrRWYb/d4TXiA9Un/2VvuLDSf9viKnPRk0XN1Mo1neBN0rLhKq02W yRg8PKPEUpyRaKjFXFScCAADlKuu/QIAAA== X-Pg-Spam-Score: -1.9 (-) 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 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. -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 500 | Chicago IL, 60604 312-676-8870 sthomas@optionshouse.com ______________________________________________ See http://www.peak6.com/email_disclaimer/ for terms and conditions related to this email -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance