Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 1DD54632E21 for ; Mon, 6 Apr 2009 11:47:14 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 79349-04 for ; Mon, 6 Apr 2009 11:47:02 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f167.google.com (mail-ew0-f167.google.com [209.85.219.167]) by mail.postgresql.org (Postfix) with ESMTP id 1B93363351E for ; Mon, 6 Apr 2009 11:47:10 -0300 (ADT) Received: by ewy11 with SMTP id 11so2967903ewy.19 for ; Mon, 06 Apr 2009 07:47:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=PhXK4MAelN91H8FH8ngwdHFrHng13TgbBzryATmONS4=; b=tw4KJazY2wuKbFR8fsqskmJHhpRvT25oqJlendMD4B2dMY3TPXo+RQEFS6rCJ6VPG/ 2UfDp0dvmJNitzjfxHZaJXcYgzHqRZqd5W54H2C+gVG4ArKDFCVwHEJOrVjx5K4iLZZg cY7W45FgvuPSieFNOeY61dB0ZCXGnKxMK6eV4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=VW0F5dMq5hKjSXPZtfVlEIfX5AGt3SmvEkeIc9pjIZ3JC3C7+F20Kqlz0y8lZXL+i9 NaFFNAuSIsZ+WI6VkmbqLIaMddxRcaRk4dQ9AAL+y+sELP+7fRjwD8bed+DVzULy1myM l51NCHuv3lq8aiZFSKUYTgmgdbiRpNdFb8y10= MIME-Version: 1.0 Received: by 10.210.127.10 with SMTP id z10mr2988999ebc.79.1239029227909; Mon, 06 Apr 2009 07:47:07 -0700 (PDT) In-Reply-To: <49DA13C1.2030200@megafon.hr> References: <49D0D237.7020308@megafon.hr> <3028.1238422571@sss.pgh.pa.us> <49D0E687.50605@megafon.hr> <49D9F39F.40106@megafon.hr> <49DA13C1.2030200@megafon.hr> Date: Mon, 6 Apr 2009 08:47:07 -0600 Message-ID: Subject: Re: Forcing seq_scan off for large table joined with tiny table yeilds improved performance From: Scott Marlowe To: Mario Splivalo Cc: Tom Lane , 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.003 tagged_above=0 required=5 tests=AWL=0.003 X-Spam-Level: X-Archive-Number: 200904/131 X-Sequence-Number: 33498 On Mon, Apr 6, 2009 at 8:37 AM, Mario Splivalo wrote: > Scott Marlowe wrote: >> >> On Mon, Apr 6, 2009 at 6:20 AM, Mario Splivalo >> wrote: >>> >>> Scott Marlowe wrote: >>>> >>>> It's not really solved, it's just a happy coincidence that the current >>>> plan runs well. =A0In order to keep the query planner making good >>>> choices you need to increase stats target for the field in the index >>>> above. =A0The easiest way to do so is to do this: >>>> >>>> alter database mydb set default_statistics_target=3D100; >>>> >>>> and run analyze again: >>>> >>>> analyze; >>> >>> So, i removed the index on field_name, set >>> default_default_statistics_target >>> to 100, analyzed, and the results are the same: >> >> Why did you remove the index? >> > > Because I already have index on that column, index needed to enforce PK > constraint. Here is the original DDL for the table: > > CREATE TABLE photo_info_data > ( > =A0photo_id integer NOT NULL, > =A0field_name character varying NOT NULL, > =A0field_value character varying, > =A0CONSTRAINT photo_info_data_pk PRIMARY KEY (user_id, field_name) > ) > > CREATE INDEX photo_info_data_ix_field_value > =A0ON user_info_data USING btree (field_value); > > So, there is index on (user_id, field_name). Postgres is using index for > user_id (...WHERE user_id =3D 12345) but not on field-name (...WHERE > field_name =3D 'f-spot'). When I add extra index on field name: > > CREATE INDEX photo_info_data_ix__field_name > =A0ON user_info_data USING btree (field_name); > > Then that index is used. On older versions of pgsql, the second of two terms in a multicolumn index can't be used alone. On newer versions it can, but it is much less efficient than if it's a single column index or if the term is the first one not the second.