Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtkKY-0007fA-Hp for pgsql-performance@arkaria.postgresql.org; Thu, 19 Dec 2013 20:36:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VtkKY-0003KE-0y for pgsql-performance@arkaria.postgresql.org; Thu, 19 Dec 2013 20:36:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtkKW-0003Ix-Nd for pgsql-performance@postgresql.org; Thu, 19 Dec 2013 20:36:24 +0000 Received: from nm41-vm4.bullet.mail.bf1.yahoo.com ([216.109.114.159]) by magus.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VtkKS-0008N1-UU for pgsql-performance@postgresql.org; Thu, 19 Dec 2013 20:36:24 +0000 Received: from [98.139.215.143] by nm41.bullet.mail.bf1.yahoo.com with NNFMP; 19 Dec 2013 20:36:19 -0000 Received: from [98.139.212.251] by tm14.bullet.mail.bf1.yahoo.com with NNFMP; 19 Dec 2013 20:36:19 -0000 Received: from [127.0.0.1] by omp1060.mail.bf1.yahoo.com with NNFMP; 19 Dec 2013 20:36:19 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 314047.90033.bm@omp1060.mail.bf1.yahoo.com Received: (qmail 70901 invoked by uid 60001); 19 Dec 2013 20:36:19 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s1024; t=1387485379; bh=bmF9FXizF2vS3yn8fTAAX4yjv4JmEjGhHzX9Hyh2BQM=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=X8CMmHtJvPvIArg8kfGPRIA9lyOUuNqzy58KGX5G4aXD+NOrGC1uIrNw5/iPgw/J6ETQtH5dWLWqauO4kDRSDlOD4QYKZ66bH3j9ZVaRseB+lXc4PWl+BFJjKEMd7RA+T3t4d9xuNt+4YTql9J8mXeXdGtJXK2J9aEHho1JkrbI= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=ymail.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=YOG+mt5xBbluiNGa6n48CnHyY1RxNObY1i8MqQ25ZmoyG4fzKumVQwgrJsTUBr3hV6/IIHMCzrK1gzHXyDD2mmrAqH4uNHkcYdUQ+nFfkFIDCBun3aTap/PTIZs0/7pErcROyji5W+lEFJoTbcrTmNzl1Pgfr7Sge9aASggkF5E=; X-YMail-OSG: Lo4kiGAVM1kTOCBEno5Tfflvw3S1GLIFWkjg_Q8UIByyHwz CMKIq44l.jCi.A61QR.W28W.XniTuOru96BM0tX4zU8zWLQch.5dlBmUU_Qn 0F5siNMavfnDkXxT1MJU66lm4uM4OE_UbBtYOktjxH9Btu8XUIeBhDWOFauK r.xhVVAxTHnz2_1FmW1gvW36YTj7PIxM6B5PhKWC2HhX5fQMe_6px5uCl86A 8mWWlOBQoZR_OTinpSBYwF.aloJyUVLQ_ajJKXJDKtI.zy1961XltTMD2blM BTG6H2plVqafgzzYyXFVPgQh9jSSxeAm5dE00z2TVq4rjIhIG3B.MdAaUC_F TE33AcOc1zsN.fH0bxZplvgLZ.Ppdn_GCntLoSgyNdJIDXMlSsyRAD4GZoEi ldYNEIrW2AwJJ4INFf.Ox6eBxOUP0zJ9ijRaYp8i6Io9lU8ywgn_0iT7SwGJ iu58BvlVTTMoE2gDWT.26RbdzGtnSNWM6QomW2IcFlY01NW1tFavuKRmi_Ub SwmfTTUrNERcZkA2EhpSLuuJNk10KUVCTa6av24Y8ghu_lq.0nONI6ex04Co RiX3ncEZmL5qfMdVSvQs9S5Cd3sebhr_9J7XcX3g- Received: from [76.255.18.237] by web162902.mail.bf1.yahoo.com via HTTP; Thu, 19 Dec 2013 12:36:19 PST X-Rocket-MIMEInfo: 002.001, TWFyYyBDb3VzaW4gPGNvdXNpbm1hcmNAZ21haWwuY29tPiB3cm90ZToKCj4gVGhlbiB3ZSBpbnNlcnQgbWlzc2luZyBwYXRocy4gVGhpcyBpcyBvbmUgb2YgdGhlIHBsYW5zIHRoYXQgZmFpbAoKPiBpbnNlcnQgaW50byBwYXRoIChwYXRoKQo.wqDCoCBzZWxlY3QgcGF0aCBmcm9tIGJhdGNoCj7CoMKgwqDCoCB3aGVyZSBub3QgZXhpc3RzCj7CoMKgwqDCoMKgwqDCoMKgwqDCoCAoc2VsZWN0IDEgZnJvbSBwYXRoIHdoZXJlIHBhdGgucGF0aD1iYXRjaC5wYXRoKQo.wqDCoMKgwqAgZ3JvdXAgYnkgcGF0aDsKCkkBMAEBAQE- X-Mailer: YahooMailWebService/0.8.172.614 References: <52B311C4.1070108@gmail.com> Message-ID: <1387485379.74465.YahooMailNeo@web162902.mail.bf1.yahoo.com> Date: Thu, 19 Dec 2013 12:36:19 -0800 (PST) From: Kevin Grittner Reply-To: Kevin Grittner Subject: Re: query plan not optimal To: Marc Cousin , "pgsql-performance@postgresql.org" In-Reply-To: <52B311C4.1070108@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.0 (--) 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 Marc Cousin wrote: > Then we insert missing paths. This is one of the plans that fail > insert into path (path) >=A0=A0 select path from batch >=A0=A0=A0=A0 where not exists >=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (select 1 from path where path.path=3Dbatch= .path) >=A0=A0=A0=A0 group by path; I know you said you wanted to focus on a different query, but this one can easily be optimized.=A0 Right now it is checking for an existing row in path for each row in batch; and it only needs to check once for each path.=A0 One way to write it would be: insert into path (path) =A0 select path from (select distinct path from batch) b =A0=A0=A0 where not exists =A0=A0=A0=A0=A0=A0=A0=A0=A0 (select 1 from path p where p.path =3D b.path); > So now we insert into the file table. > > insert into file (pathid,filename) >=A0=A0 select pathid, filename from batch join path using (path); > What I think is the cause of the problem is that the planner > doesn't take into account that we are going to fetch the exact > same values all the time in the path table, so we'll have a very > good hit ratio. It kinda takes that into account for the index part of things via the effective_cache_size setting.=A0 That should normally be set to 50% to 75% of machine RAM. > Maybe the n_distinct from batch.path could be used to refine the > caching effect on the index scan ? Interesting idea. > For now, we work around this by using very low values for > seq_page_cost and random_page_cost for these 2 queries. If you are not already doing so, you might want to try setting cpu_tuple_cost to something in the 0.03 to 0.05 range.=A0 I have found that the default is too low compared to other cpu cost factors, and raising it makes the exact settings for page costs less sensitive -- that is, you get good plans over a wider range of page cost settings.=A0 I have sometimes been unable to get a good plan for a query without boosting this, regardless of what I do with other settings. Running with a development build on my 16GB development PC, I got your fast plan with your "big data" test case by making only this one adjustment from the postgresql.conf defaults: set effective_cache_size =3D '2GB'; I also got the fast plan if I left effective_cache_size at the default and only changed: set cpu_tuple_cost =3D 0.03; I know that there have been adjustments to cost calculations for use of large indexes in both minor and major releases.=A0 If a little sensible tuning of cost factors to better match reality doesn't do it for you, you might want to consider an upgrade. -- Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance