Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiePX-0000QN-QC for pgsql-performance@arkaria.postgresql.org; Wed, 12 Dec 2012 04:59:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TiePW-00061Y-Ny for pgsql-performance@arkaria.postgresql.org; Wed, 12 Dec 2012 04:59:10 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiePV-00061S-90 for pgsql-performance@postgresql.org; Wed, 12 Dec 2012 04:59:09 +0000 Received: from mail-lb0-f169.google.com ([209.85.217.169]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiePT-0005nk-LW for pgsql-performance@postgresql.org; Wed, 12 Dec 2012 04:59:08 +0000 Received: by mail-lb0-f169.google.com with SMTP id gk1so201831lbb.28 for ; Tue, 11 Dec 2012 20:59:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :message-id:references:to:x-mailer; bh=cpO51IaA6qXNyp19+Lkb9V+34T5/HBwL98GBAzTT24c=; b=NvW37YXaKrrSCT/Fnd9xMyZg0y+K/UQkJGWXYm+Mnk0MEBTpSgBaa3L/d0PVJ/3/50 nc1MoUQgA5LqYucRW7frc5EqogchiKls7HqIejnaFNm7Mn3PgHdajJhvhOY61dijBwJQ lNbzVDyj7TZNEKEoE4ir3klmOXEQrbt+2TtQdjOHyEVIJx1gB8oT60l+Xe4V6asL3/q5 uEQgT1xzMjL17N6i2G9mpOM/DV1O+PyifiG9/vieG1qllPfLMQ9qW40V+HSJJu3UasIU fjNHLrQ4PLnH5+36NpOXzyjt10n96FoB5SlmyAPI5KjAfuBQqyF/0A26xdGkKPHsIE5X f8nA== Received: by 10.112.47.168 with SMTP id e8mr242682lbn.46.1355288346396; Tue, 11 Dec 2012 20:59:06 -0800 (PST) Received: from [192.168.20.5] ([176.195.39.85]) by mx.google.com with ESMTPS id ti4sm10081661lab.1.2012.12.11.20.59.05 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 11 Dec 2012 20:59:05 -0800 (PST) Content-Type: multipart/alternative; boundary="Apple-Mail=_D028E9C1-FB15-4FBD-BC4E-33B9A729F6CE" Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: hash join vs nested loop join From: Evgeny Shishkin In-Reply-To: <8485F899-D139-41F9-B2E6-2D0F5B253367@gmail.com> Date: Wed, 12 Dec 2012 08:59:04 +0400 Cc: "pgsql-performance@postgresql.org" Message-Id: <3A5ED8C4-B611-4AF1-BFB1-AA05F9F594D8@gmail.com> References: <8485F899-D139-41F9-B2E6-2D0F5B253367@gmail.com> To: Huan Ruan X-Mailer: Apple Mail (2.1499) X-Pg-Spam-Score: -2.6 (--) 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 --Apple-Mail=_D028E9C1-FB15-4FBD-BC4E-33B9A729F6CE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=iso-8859-1 On Dec 12, 2012, at 8:57 AM, Evgeny Shishkin = wrote: >=20 > On Dec 12, 2012, at 8:44 AM, Huan Ruan wrote: >=20 >>=20 >> On 12 December 2012 15:33, Evgeny Shishkin = wrote: >> Optimiser thinks that nested loop is more expensive, because of point = PK lookups, which a random io. >> Can you set random_page_cost to 2 or 3 and try again? >>=20 >> Hi Evgeny >>=20 >> Thanks for the quick reply. Setting random_page_cost to 3 doesn't = make a difference, but to 2 makes the optimiser to choose nested loop. = However, with such a small penalty for random I/O, I'm worry about this = setting will make other small queries incorrectly use index when it = should be a sequential scan though. I understand random I/O is = expensive, but in this case the optimiser already knows the big table is = really big, should it consider a sequential scan will be slower than an = index lookup? Scan 170 million records vs index lookup of 50,000 = records. Any thoughts? >>=20 >=20 > Yes, this is the most common issue for me.=20 > Usually you just have to find the right combination of random and seq = scan costs, shared_buffers and effective_cache_size. > If some of the queries work well with another value of, say, = random_page_cost, then, since it is per session parameter, you can SET = it in your session before the query. But over time your table may change = in size and distribution and everything brakes. No speaking about = general ugliness from application standpoint. >=20 > May be somebody more experienced would help. >=20 > Also you can set different costs per tablespace. >=20 >> Thanks >> Huan >=20 Added CC. --Apple-Mail=_D028E9C1-FB15-4FBD-BC4E-33B9A729F6CE Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=iso-8859-1 itparanoia@gmail.com> = wrote:
huan.ruan.it@gmail.com> = wrote:


On 12 December 2012 15:33, = Evgeny Shishkin <itparanoia@gmail.com> = wrote:
Optimiser thinks that nested loop is more expensive, because of = point PK lookups, which a random io.
Can you set = random_page_cost to 2 or 3 and try = again?

Hi Evgeny

Thanks for the quick reply. Setting random_page_cost to 3 doesn't make a = difference, but to 2 makes the optimiser to choose nested loop. However, = with such a small penalty for random I/O, I'm worry about this setting = will make other small queries incorrectly use index when it should be a = sequential scan though. I understand random I/O is expensive, but in = this case the optimiser already knows the big table is really big, = should it consider a sequential scan will be slower than an index = lookup? Scan 170 million records vs index lookup of 50,000 records. Any = thoughts?


<= /div>
Yes, this is the most common issue for = me. 
Usually you just have to find the right combination = of random and seq scan costs, shared_buffers and = effective_cache_size.
If some of the queries work well with = another value of, say, random_page_cost, then, since it is per session = parameter, you can SET it in your session before the query. But over = time your table may change in size and distribution and everything = brakes. No speaking about general ugliness from application = standpoint.

May be somebody more experienced = would help.

Also you can set different costs = per tablespace.

Thanks
Huan


Added = CC.

= --Apple-Mail=_D028E9C1-FB15-4FBD-BC4E-33B9A729F6CE--