Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xmk0x-0001xf-Ln for pgsql-performance@arkaria.postgresql.org; Fri, 07 Nov 2014 13:55:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xmk0w-0003OX-9b for pgsql-performance@arkaria.postgresql.org; Fri, 07 Nov 2014 13:55:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xmk0v-0003OQ-5K for pgsql-performance@postgresql.org; Fri, 07 Nov 2014 13:55:45 +0000 Received: from relay6-d.mail.gandi.net ([2001:4b98:c:538::198]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Xmk0q-0002SV-OU for pgsql-performance@postgresql.org; Fri, 07 Nov 2014 13:55:43 +0000 Received: from mfilter32-d.gandi.net (mfilter32-d.gandi.net [217.70.178.163]) by relay6-d.mail.gandi.net (Postfix) with ESMTP id 3AAF3FB8E6; Fri, 7 Nov 2014 14:55:37 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter32-d.gandi.net Received: from relay6-d.mail.gandi.net ([217.70.183.198]) by mfilter32-d.gandi.net (mfilter32-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id 32k-dW2gAwvJ; Fri, 7 Nov 2014 14:55:35 +0100 (CET) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-098-027-058-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id BD3A4FB8C9; Fri, 7 Nov 2014 14:55:34 +0100 (CET) Message-ID: <545CCF54.1010205@dunslane.net> Date: Fri, 07 Nov 2014 08:55:32 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: Vlad Arkhipov , David Rowley CC: pgsql-performance Subject: Re: Postgres does not use indexes with OR-conditions References: <545C479B.7010104@dc.baikal.ru> <545C5352.4050309@dc.baikal.ru> In-Reply-To: <545C5352.4050309@dc.baikal.ru> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 11/07/2014 12:06 AM, Vlad Arkhipov wrote: > It was just a minimal example. The real query looks like this. > > select * > from commons.financial_documents fd > where fd.creation_time < '2011-11-07 10:39:07.285022+08' > or (fd.creation_time = '2011-11-07 10:39:07.285022+08' and > fd.financial_document_id < 100) > order by fd.creation_time desc > limit 200 > > I need to rewrite it in the way below to make Postgres use the index. > > select * > from commons.financial_documents fd > where fd.creation_time <= '2011-11-07 10:39:07.285022+08' > and ( > fd.creation_time < '2011-11-07 10:39:07.285022+08' > or (fd.creation_time = '2011-11-07 10:39:07.285022+08' and > fd.financial_document_id < 100) > ) > order by fd.creation_time desc > limit 200 > First, please do not top-post on the PostgreSQL lists. See Second, the last test for fd.creation_time in your query seems redundant. Could you not rewrite it as something this?: where fd.creation_time <= '2011-11-07 10:39:07.285022+08' and (fd.creation_time < '2011-11-07 10:39:07.285022+08' or fd.financial_document_id < 100) cheers andrew -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance