Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 98298633036 for ; Mon, 26 Apr 2010 06:34:01 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 58865-10 for ; Mon, 26 Apr 2010 09:33:51 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.159]) by mail.postgresql.org (Postfix) with ESMTP id BB27D6323AD for ; Mon, 26 Apr 2010 06:33:50 -0300 (ADT) Received: by fg-out-1718.google.com with SMTP id 19so1916530fgg.1 for ; Mon, 26 Apr 2010 02:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=Ois4r7XmiM9rLtRQ2K5yOK51EPVcuWnUGK6BKpfw6ro=; b=L1L4HCqPtKDI5bieySGgKCIr2YnZT/Qm08JVCdrXxgo2DbteYSd51CIe+AFtAVXgwj Fr8Cc8RbBxPrVw7S20sTXalGge94j6hfsOSM+gHfgZonZMCcpF+iTWatoHWl0vS4CAek nlUcFGbtsPRhbc0XnIk4Zryd9ygh82TNOWZ0E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=wnmmFQuVpfIjLS3gRHwMJI9gXpbPHgDzN3zA+VFLC9mRLrtD+YhseoRH81Ujb8FRRy I8QMVwgthxs6qtNalmA2PmUowwEzA9GosrAsno/9hRk5PH+t6xuujQmLKwXdEYDlApiK EHb7fMh+4ryctRB0E8Hy1sVEtH1Yo8AT2NyeQ= Received: by 10.239.190.10 with SMTP id v10mr343838hbh.180.1272274429119; Mon, 26 Apr 2010 02:33:49 -0700 (PDT) MIME-Version: 1.0 Received: by 10.239.158.134 with HTTP; Mon, 26 Apr 2010 02:33:29 -0700 (PDT) In-Reply-To: <4BD4FFE7.9090305@dc.baikal.ru> References: <4BD0161A.4010908@dc.baikal.ru> <4BD0430C.1050308@2ndquadrant.com> <4BD10805.9060004@dc.baikal.ru> <4BD4FFE7.9090305@dc.baikal.ru> From: =?ISO-8859-1?Q?C=E9dric_Villemain?= Date: Mon, 26 Apr 2010 11:33:29 +0200 Message-ID: Subject: Re: Optimization idea To: Vlad Arkhipov Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.499 tagged_above=-10 required=5 tests=BAYES_05=-0.5, FREEMAIL_FROM=0.001 X-Spam-Level: X-Archive-Number: 201004/315 X-Sequence-Number: 38565 2010/4/26 Vlad Arkhipov : > >> On Thu, Apr 22, 2010 at 10:37 PM, Vlad Arkhipov >> wrote: >> >>> >>> I don't think this is just an issue with statistics, because the same >>> problem arises when I try executing a query like this: >>> >> >> I'm not sure how you think this proves that it isn't a problem with >> statistics, but I think what you should be focusing on here, looking >> back to your original email, is that the plans that are actually much >> faster have almost as much estimated cost as the slower one. =A0Since >> all your data is probably fully cached, at a first cut, I might try >> setting random_page_cost and seq_page_cost to 0.005 or so, and >> adjusting effective_cache_size to something appropriate. >> >> ...Robert >> >> > > Ok. I thougth it's quite obvious because of these two queries. I can't > understand why the estimated rows count is 40040 in the first plan. In the first query, the planner doesn't use the information of the 2,3,4. It just does a : I'll bet I'll have 2 rows in t1 (I think it should say 3, but it doesn't) So it divide the estimated number of rows in the t2 table by 5 (different values) and multiply by 2 (rows) : 40040. In the second query the planner use a different behavior : it did expand the value of t1.t to t2.t for each join relation and find a costless plan. (than the one using seqscan on t2) We are here in corner case situation where n_distinc valuest < statistics on the column and where we might be able to improve the planner decision. I believe I have already read something on this topic on -hackers... > > test=3D# explain analyze select * from t2 join t1 on t1.t =3D t2.t where > t1.t in (2,3,4); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 QUERY PLAN > -------------------------------------------------------------------------= ----------------------------------------- > Hash Join =A0(cost=3D1.09..2319.87 rows=3D40040 width=3D32) (actual > time=3D0.050..356.269 rows=3D400 loops=3D1) > =A0Hash Cond: (t2.t =3D t1.t) > =A0-> =A0Seq Scan on t2 =A0(cost=3D0.00..1543.00 rows=3D100100 width=3D16= ) (actual > time=3D0.013..176.087 rows=3D100100 loops=3D1) > =A0-> =A0Hash =A0(cost=3D1.07..1.07 rows=3D2 width=3D16) (actual time=3D0= .023..0.023 > rows=3D3 loops=3D1) > =A0 =A0 =A0 =A0Buckets: 1024 =A0Batches: 1 =A0Memory Usage: 1kB > =A0 =A0 =A0 =A0-> =A0Seq Scan on t1 =A0(cost=3D0.00..1.07 rows=3D2 width= =3D16) (actual > time=3D0.006..0.014 rows=3D3 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0Filter: (t =3D ANY ('{2,3,4}'::bigint[])) > Total runtime: 356.971 ms > (8 rows) > > test=3D# explain analyze select * from t2 join t1 on t1.t =3D t2.t where > t1.t =3D 2 union all select * from t2 join t1 on t1.t =3D t2.t where t1.t= =3D > 3 union all select * from t2 join t1 on t1.t =3D t2.t where t1.t =3D 4; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0QUERY PLAN > -------------------------------------------------------------------------= --------------------------------------------------- > Append =A0(cost=3D0.00..112.42 rows=3D407 width=3D32) (actual time=3D0.04= 8..3.487 > rows=3D400 loops=3D1) > =A0-> =A0Nested Loop =A0(cost=3D0.00..47.51 rows=3D197 width=3D32) (actua= l > time=3D0.045..1.061 rows=3D200 loops=3D1) > =A0 =A0 =A0 =A0-> =A0Seq Scan on t1 =A0(cost=3D0.00..1.06 rows=3D1 width= =3D16) (actual > time=3D0.011..0.014 rows=3D1 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0Filter: (t =3D 2) > =A0 =A0 =A0 =A0-> =A0Index Scan using t_idx on t2 =A0(cost=3D0.00..44.48 = rows=3D197 > width=3D16) (actual time=3D0.026..0.382 rows=3D200 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0Index Cond: (pg_temp_2.t2.t =3D 2) > =A0-> =A0Nested Loop =A0(cost=3D0.00..32.67 rows=3D117 width=3D32) (actua= l > time=3D0.019..0.599 rows=3D100 loops=3D1) > =A0 =A0 =A0 =A0-> =A0Seq Scan on t1 =A0(cost=3D0.00..1.06 rows=3D1 width= =3D16) (actual > time=3D0.003..0.006 rows=3D1 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0Filter: (t =3D 3) > =A0 =A0 =A0 =A0-> =A0Index Scan using t_idx on t2 =A0(cost=3D0.00..30.43 = rows=3D117 > width=3D16) (actual time=3D0.010..0.211 rows=3D100 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0Index Cond: (pg_temp_2.t2.t =3D 3) > =A0-> =A0Nested Loop =A0(cost=3D0.00..28.17 rows=3D93 width=3D32) (actual > time=3D0.017..0.534 rows=3D100 loops=3D1) > =A0 =A0 =A0 =A0-> =A0Seq Scan on t1 =A0(cost=3D0.00..1.06 rows=3D1 width= =3D16) (actual > time=3D0.005..0.008 rows=3D1 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0Filter: (t =3D 4) > =A0 =A0 =A0 =A0-> =A0Index Scan using t_idx on t2 =A0(cost=3D0.00..26.18 = rows=3D93 > width=3D16) (actual time=3D0.007..0.187 rows=3D100 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0Index Cond: (pg_temp_2.t2.t =3D 4) > Total runtime: 4.190 ms > (17 rows) > > > -- > Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org= ) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance > --=20 C=E9dric Villemain