Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cEYsc-0006tr-Fk for pgsql-performance@arkaria.postgresql.org; Wed, 07 Dec 2016 09:51:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cEYsb-0002U8-Oq for pgsql-performance@arkaria.postgresql.org; Wed, 07 Dec 2016 09:51:13 +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 1cEYsb-0002Tw-1j for pgsql-performance@postgresql.org; Wed, 07 Dec 2016 09:51:13 +0000 Received: from mail-qt0-x242.google.com ([2607:f8b0:400d:c0d::242]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cEYsY-0003Ih-IV for pgsql-performance@postgresql.org; Wed, 07 Dec 2016 09:51:11 +0000 Received: by mail-qt0-x242.google.com with SMTP id m48so43772944qta.2 for ; Wed, 07 Dec 2016 01:51:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=M9O4nKmkexvmtwHI48bZMLTNoOkcL6Ivx/PPlTsiAsc=; b=iK1P7qsNEsFF5eVgkZlqq+Hzl4oA3gXFVPoXUI4/EykUswAf3KpUpcrc0qYMCPmBmr PagWnkNBr0OXeSQEx3sM9hhniWyaIgCp9iLCEahVGW/Wr/WNr82/FCvSpV8DAVU4M+w0 7vGLts4J8bGkjN/7tvXAFLkAduWn4CBexUgb/hrJbtpXMq4vC6TGrvPZnZhQBKYAjaRX LcpPRpFgVhYd2ShQdNZEMVhfgNMNxXLRjSoODMmtNigOpKqZ2JearidtB86wUY5Hqm+k drUT6oAQv3ingqANbk4R3RRxHMcryvwpncESc8nmn4E7Eb5ACUNNG39TTC4FAOR2BALI rsoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=M9O4nKmkexvmtwHI48bZMLTNoOkcL6Ivx/PPlTsiAsc=; b=L4xl4T3E9KVPEiXqlj+OvO7jk/f47IF6QIODORsy994uSSffBDwg3ASw+humjnmdJ1 WSBrJkxWYRT/IyikB79jqL75Vxl/+qbcgHWpEoXRlippxVQgzPufkfveFSQoE50XsAGB P1e9XAhZuvGhy8geW5t/wD1nDGferhdb0zmq6i92fIT9eLNC+lllUxzotcGZU6wPF9fU 580ImzZoJonhnnonBSNMqmCBzDjgwaljU37PCIVyzxMoLaNW5Vg3AVLIi9k3UVQDYbi5 tvonJJncw7bup7qsh9YNQlj8zf17yYQPQ6AlgAFiVeRzSO01TOzsS3aj4MEtALiMb7Ri QPKQ== X-Gm-Message-State: AKaTC00Xph+F88XjQGMX47JSUm42AhE6bdS097QH6rxFhVq37Bha8lC8jco1wYWSlVWcuA== X-Received: by 10.25.195.194 with SMTP id t185mr19018507lff.13.1481104269586; Wed, 07 Dec 2016 01:51:09 -0800 (PST) Received: from [192.168.41.106] ([79.122.187.86]) by smtp.gmail.com with ESMTPSA id e78sm4590764lfg.23.2016.12.07.01.51.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Dec 2016 01:51:08 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 10.1 \(3251\)) Subject: Re: Slow query question From: Andrey Povazhnyi In-Reply-To: <26351.1481038435@sss.pgh.pa.us> Date: Wed, 7 Dec 2016 12:51:07 +0300 Cc: pgsql-performance@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <17350FB2-A88E-414D-9CAB-96102394E62E@gmail.com> References: <48E80576-4047-436C-97DD-59EA7BE4DE34@gmail.com> <26351.1481038435@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.3251) X-Pg-Spam-Score: -2.7 (--) 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 Tom, Thank you for a thorough answer. We=E2=80=99ll try the 2-column index. Regards, Andrey Povazhnyi > On Dec 6, 2016, at 6:33 PM, Tom Lane wrote: >=20 > Andrey Povazhnyi writes: >> We=E2=80=99ve got a strange planner behavior on a query to one of our bi= gger tables after we upgraded to postgres 9.6.1 recently. >=20 > 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. >=20 > 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. >=20 > 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