Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cEHkr-0008V7-5s for pgsql-performance@arkaria.postgresql.org; Tue, 06 Dec 2016 15:34:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cEHkq-0003ml-Oi for pgsql-performance@arkaria.postgresql.org; Tue, 06 Dec 2016 15:34:04 +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_2) (envelope-from ) id 1cEHkq-0003mS-3M for pgsql-performance@postgresql.org; Tue, 06 Dec 2016 15:34:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cEHkj-0004k6-HB for pgsql-performance@postgresql.org; Tue, 06 Dec 2016 15:34:02 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id uB6FXtYm026352; Tue, 6 Dec 2016 10:33:55 -0500 From: Tom Lane To: Andrey Povazhnyi cc: pgsql-performance@postgresql.org Subject: Re: Slow query question In-reply-to: <48E80576-4047-436C-97DD-59EA7BE4DE34@gmail.com> References: <48E80576-4047-436C-97DD-59EA7BE4DE34@gmail.com> Comments: In-reply-to Andrey Povazhnyi message dated "Tue, 06 Dec 2016 17:35:23 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <26350.1481038435.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 06 Dec 2016 10:33:55 -0500 Message-ID: <26351.1481038435@sss.pgh.pa.us> X-Pg-Spam-Score: -4.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 Andrey Povazhnyi writes: > We=E2=80=99ve got a strange planner behavior on a query to one of our big= ger tables after we upgraded to postgres 9.6.1 recently. The basic problem with this query is that there are no good alternatives. The planner believes there are about 53K rows matching the WHERE condition. (I assume this estimate is roughly in line with reality, else we have different problems to talk about.) It can either scan down the "id" index and stop when it finds the 30th row matching WHERE, or it can use the "symbol" index to read all 53K rows matching WHERE and then sort them by "id". Neither one of those is going to be speedy; but the more rows there are matching WHERE, the better the first way is going to look. If you're worried about doing this a lot, it might be worth your while to provide a 2-column index on (source, id) --- in that order --- which would allow a query plan that directly finds the required 30 rows as consecutive index entries. Possibly this could replace your index on "source" alone, depending on how much bigger the 2-col index is and how many queries have no use for the second column. See https://www.postgresql.org/docs/current/static/indexes.html particularly 11.3 - 11.5. regards, tom lane --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance