Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WBdy8-0006mQ-Vd for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Feb 2014 05:27:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WBdy7-0003bf-Iy for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Feb 2014 05:27:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WBdy6-0003bY-Lj for pgsql-bugs@postgresql.org; Fri, 07 Feb 2014 05:27:14 +0000 Received: from qmta03.westchester.pa.mail.comcast.net ([2001:558:fe14:43:76:96:62:32]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WBdy3-0003sr-Ga for pgsql-bugs@postgresql.org; Fri, 07 Feb 2014 05:27:13 +0000 Received: from omta11.westchester.pa.mail.comcast.net ([76.96.62.36]) by qmta03.westchester.pa.mail.comcast.net with comcast id PHSL1n0020mv7h053HT9rA; Fri, 07 Feb 2014 05:27:09 +0000 Received: from [10.59.1.101] ([83.80.19.126]) by omta11.westchester.pa.mail.comcast.net with comcast id PHT01n00B2jCa9P3XHT3WS; Fri, 07 Feb 2014 05:27:07 +0000 Message-ID: <52F46EA4.3080403@comcast.net> Date: Thu, 06 Feb 2014 21:27:00 -0800 From: Harry Rossignol User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: Re: BUG #9135: PostgreSQL doesn't want use index scan instead of (index scan+sort+limit) References: <20140207045518.13610.2870@wrigleys.postgresql.org> <52F46B82.2000805@comcast.net> In-Reply-To: <52F46B82.2000805@comcast.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=q20121106; t=1391750829; bh=ULuQayk1b1cm/bKTHQdY7wwgA8hopdkLVzVY9uAD3xg=; h=Received:Received:Message-ID:Date:From:MIME-Version:To:Subject: Content-Type; b=GL6ApyB1KQAA+cb/UMyCqksEnd8CgaKLZa0KFlCHf1AKerCWD/CRboMKvZCX9c5kK MLtVKNpbGk8QN8nirU2ZK00Pne/KZN8fQd1Z0+OiSTf0VxuQ4zfdRb/a69PsNPEER0 +r0wZSgoKOG0HP8s2bfq5DbOCAj5UapV5UA9IBV0q6+Jll+/c2vOlxanoowL+puHRp vX3OpWoW315WazSdbImfZjHMFftan6zj6wtGQqDw6tGYGaM/1iv9sJ4QpTNkwT4SUa dfDakpnAFumF0Q1WgMtJmcoufUjl+GPBiTXW33pc1DoyWpII5t/T6cUtTDkXK7+1pX Rx2fOieRzeA8w== X-Pg-Spam-Score: -1.3 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On 2/6/2014 9:13 PM, Harry Rossignol wrote: > I would try - > > select * from liexWebmasterProducts this_ where > this_.lwpWebsiteI_.lwpnotForSale > lwpWebsiteId,.lwpCreateDate desc ORDER BY lwpWebsiteI,_.lwpnotForSale , lwpWebsiteId,.lwpCreateDate > Limit 1; > > > > > 2/6/2014 8:55 PM, maxim.boguk@gmail.com wrote: >> The following bug has been logged on the website: >> >> Bug reference: 9135 >> Logged by: Maxim Boguk >> Email address: maxim.boguk@gmail.com >> PostgreSQL version: 9.3.2 >> Operating system: Linux >> Description: >> >> Hi, >> >> One of my customers have very curious situation with simple query and >> index >> usage. I tried different ideas but it doesn't work anyway and now I >> out of >> ideas. It's looks like a bug if I not missing something. >> >> Detail: >> 4GB liexWebmasterProducts table with interesting fields: >> >> lwpid | integer | not null >> default nextval('liexwebmasterproducts_lwpid_seq'::regclass) >> lwpname | text | >> ... >> lwpwebsiteid | integer | >> ... >> lwpnotforsale | boolean | not null >> ... >> lwpcreatedate | timestamp without time zone | not null >> default now() >> ... >> >> Index on the last three fields defined as: >> "i_liexwebmasterproducts_2" btree (lwpwebsiteid, lwpnotforsale, >> lwpcreatedate) >> >> Target query and plan: >> select * >> from liexWebmasterProducts this_ >> where >> this_.lwpWebsiteId=5935 >> and this_.lwpnotForSale=FALSE >> order by this_.lwpCreateDate desc limit 1; >> >> Limit (cost=122.18..122.19 rows=1 width=902) (actual >> time=13.505..13.506 >> rows=1 loops=1) >> -> Sort (cost=122.18..124.57 rows=953 width=902) (actual >> time=13.503..13.503 rows=1 loops=1) >> Sort Key: lwpcreatedate >> Sort Method: top-N heapsort Memory: 27kB >> -> Index Scan using i_liexwebmasterproducts_2 on >> liexwebmasterproducts this_ (cost=0.43..117.42 rows=953 width=902) >> (actual >> time=0.171..10.429 rows=1674 loops=1) >> Index Cond: ((lwpwebsiteid = 5935) AND (lwpnotforsale = >> false)) >> Filter: (NOT lwpnotforsale) >> Total runtime: 13.626 ms >> >> >> I have no idea why Postgresql doesn't want use simple index scan over 3 >> fields... >> set enable_sort to 0; >> have no effect: >> >> Limit (cost=10000000119.90..10000000119.90 rows=1 width=902) (actual >> time=6.591..6.592 rows=1 loops=1) >> -> Sort (cost=10000000119.90..10000000122.24 rows=935 width=902) >> (actual time=6.588..6.588 rows=1 loops=1) >> Sort Key: lwpcreatedate >> Sort Method: top-N heapsort Memory: 27kB >> -> Index Scan using i_liexwebmasterproducts_2 on >> liexwebmasterproducts this_ (cost=0.43..115.22 rows=935 width=902) >> (actual >> time=0.050..3.733 rows=1673 loops=1) >> Index Cond: ((lwpwebsiteid = 5935) AND (lwpnotforsale = >> false)) >> Filter: (NOT lwpnotforsale) >> Total runtime: 6.670 ms >> >> >> It seems somehow related to the: "Filter: (NOT lwpnotforsale)" part >> of the >> query which look like redundant, but it's my pure guessing. >> >> Reindexing the index, vacuum analyze table - provide zero effect on the >> plan. >> >> Generating whole new subset and table via: >> shop=# create table test as select lwpid,lwpwebsiteid, lwpnotforsale, >> lwpcreatedate from liexwebmasterproducts; >> SELECT 6799176 >> shop=# create index CONCURRENTLY test_index_1 on test(lwpwebsiteid, >> lwpnotforsale, lwpcreatedate); >> CREATE INDEX >> shop=# vacuum analyze test; >> VACUUM >> >> Have no effect as well (plan over test table stay the same). >> >> Changing order of the two first fields in index via: >> create index CONCURRENTLY test_index_2 on test(lwpnotforsale, >> lwpwebsiteid, >> wpcreatedate); >> have no effect on the plan too. >> >> Kindly Regards, >> Maksym >> >> >> >> >> > > > -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs