Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpCOI-0007Bq-Fr for pgsql-general@arkaria.postgresql.org; Tue, 15 Dec 2020 15:37:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kpCOG-0003xJ-Jl for pgsql-general@arkaria.postgresql.org; Tue, 15 Dec 2020 15:37:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpCOG-0003x7-A9 for pgsql-general@lists.postgresql.org; Tue, 15 Dec 2020 15:37:28 +0000 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kpCOE-0003KK-5u for pgsql-general@lists.postgresql.org; Tue, 15 Dec 2020 15:37:27 +0000 Received: by mail-ot1-x333.google.com with SMTP id w3so19719948otp.13 for ; Tue, 15 Dec 2020 07:37:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=Z8nCsJ7/4/JOAMwdo5rytmfKCAJLO0wkDskcKO9XXXA=; b=k++xUcnCLMrVFbFo80uGzCSoF1FLAnxUO86/OT8053544nvrsldfLxkGwg7zDryFgW xQ1x4D6K7SkwGyjc8rql+I0b2UUqSeSzz68QIdJvlZPKY9lIuQiq0J7MUU/7Aj5UF6pG +SOXaPBNj1HoCUdQ7dQ+iXtgD2ViDfBVm73JxYgmKuUOKdUsBWB/69c9ZAjlLTm+bH00 AbMwx+F+L+muS1JJEZvA158rGoil0O1wR/m062p9/gQQCDNd2Pe35b5Osywl3iRVVbiK ph/bIeVuiWFgXqnEOZAvKM1mF2qH2tzy4noQUkxOX0CVNktUhJJzlU6po0NWHP2T/bop 5llQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=Z8nCsJ7/4/JOAMwdo5rytmfKCAJLO0wkDskcKO9XXXA=; b=FlJS/f1R367E1uGeFGyUFM4gLxuAuzLhTqooYBJnuFB6mnTG22N435JK+4LL1xWrJz q77Snr9I9A3RSGgstoejiCIrypk9JV9Lp7YhbZ9kGLf3PT1U59OixP3Idi0B1Gv7vZbq uTocdtwBHmdLx541MFZTeoJTxX1lAGGoQrqEo1NJ3NBfC7t7s+Tdvh32DjSLLjRUHRs7 cZ8R+iyOQc5/df6+UB9TUMUcU/swRPX/w1TX49r1P12Ms2atY6IB4FKOukISCTQvSyhg ypRuKGLOrebNVgs5gBw8yk0Frh2rYHetP3byXao0uZ06+SfJoLY9lRKH5vyqPD9p4VlC pF/Q== X-Gm-Message-State: AOAM5334XIAmd+qErlqn8NMeq9253Qa2bSk3Rcvm3+ll/YUSGBsFksGc 2hz25PwSMYiyA8qgIU8QYD7hBR1gm8zVyw== X-Google-Smtp-Source: ABdhPJy7vt09si8k9b3rk2Luo9PxEa0IxQEj70cQ1krk3TB4lvws/1aTN9tu44Bgm3fbcqLnIKVH9g== X-Received: by 2002:a9d:170d:: with SMTP id i13mr23658656ota.106.1608046643840; Tue, 15 Dec 2020 07:37:23 -0800 (PST) Received: from [192.168.1.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id j73sm484598otj.37.2020.12.15.07.37.22 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Dec 2020 07:37:22 -0800 (PST) Subject: Re: Optimize query To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: Date: Tue, 15 Dec 2020 09:37:21 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------AC9AFD71EDE6B38FA65F1487" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------AC9AFD71EDE6B38FA65F1487 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 12/15/20 8:17 AM, Yambu wrote: > Hi > > How would you optimize a query with greater than in where clause eg > > select * from table1 where id > 1000 > > and there is an index on id column The question as written is unanswerable. - Why do you think it needs to be optimized? - What is the table cardinality, and the query cardinality? - What does EXPLAIN say? - Have you run ANALYZE on table1? Bottom line: Postgres *might* think that it is more efficient to scan the whole table. -- Angular momentum makes the world go 'round. --------------AC9AFD71EDE6B38FA65F1487 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 12/15/20 8:17 AM, Yambu wrote:
Hi

How would you optimize a query with greater than in where clause eg

select * from table1 where id > 1000 

and there is an index on id column

The question as written is unanswerable.

- Why do you think it needs to be optimized?
- What is the table cardinality, and the query cardinality?
- What does EXPLAIN say?
- Have you run ANALYZE on table1?

Bottom line: Postgres might think that it is more efficient to scan the whole table.

--
Angular momentum makes the world go 'round.
--------------AC9AFD71EDE6B38FA65F1487--