Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UOJsJ-0000UR-36 for pgsql-performance@arkaria.postgresql.org; Sat, 06 Apr 2013 03:33:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UOJsI-0000zv-DY for pgsql-performance@arkaria.postgresql.org; Sat, 06 Apr 2013 03:33:06 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UOJsH-0000zQ-0P for pgsql-performance@postgresql.org; Sat, 06 Apr 2013 03:33:05 +0000 Received: from nm10-vm3.bullet.mail.ne1.yahoo.com ([98.138.91.140]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UOJs9-0002hT-F9 for pgsql-performance@postgresql.org; Sat, 06 Apr 2013 03:33:04 +0000 Received: from [98.138.90.48] by nm10.bullet.mail.ne1.yahoo.com with NNFMP; 06 Apr 2013 03:32:56 -0000 Received: from [98.138.89.240] by tm1.bullet.mail.ne1.yahoo.com with NNFMP; 06 Apr 2013 03:32:56 -0000 Received: from [127.0.0.1] by omp1013.mail.ne1.yahoo.com with NNFMP; 06 Apr 2013 03:32:56 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 330124.27965.bm@omp1013.mail.ne1.yahoo.com Received: (qmail 12254 invoked by uid 60001); 6 Apr 2013 03:32:56 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1365219176; bh=rnY9Y4ZAGDBCHiX2WpQTTjoECCX9xe0ck0uLyc0WMgM=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=lL6jbthyXbiaqznb1gV3EGrNJlP9VGciq49KC5OPY9v4GACN6bpdzvgmCuWgyBS+PnX2G70f43+b6HZ8pYTyHCMlMwEx64xdz74F7ngCukTplXguG5W+bnkxpBjItPlwVdNfjktf0FHUGU4SskGttts/P99rvmf1kFFm4eObieM= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=Qb7IQl4y+LBT4YSFuJY5OZMNndjOPF8+jNUUA3RLF4bWRea4K38r6a9vhyS3MoAvaF/NF9/8mirgeFq4loCtFNMwlgeTe0DxtFAZ6C3BHchNuaIcw/REWdn3RELm5Hs4A+tSt3MbfgQe1pOMSnhg7dHuDQtnP4ay9qB+fFu6xqI=; X-YMail-OSG: Dk7I9f8VM1mfBmajSZhlqaUItOckEJh.JDnlnGjIwscSQ.e FdWElfBaQ0fOduiWM3umYTgggIec4MI3ve55E0hVBme4gBO909E5HdDJXDxx 8EaAYfcm36RS3lpIYO00RLdsrUWryE4en4KXwCyLs4fuZKnvCa3.LW7BO.R6 HAaLNCSA3vIKDC2LGZkWPmLgtTG7_fb6p9riJAYyyl6BGWDUaXYYJjil0sAK zCoUcvjRSTIkfJ33XPyEKg_mm63b6WYJn0tbaLP6bZ9RX_ROW1.YVirn83xL wN6BPOh5wt4Iy.v4xbpalZiIdMw.zjxejnDlb7XfuYOMVjRHlYUAzrDAM2FJ 1aBGmYNtZb.yXXDl.Edx3AAv3RzZvtErJDkKhgYYpOkkqd6znjhln06XMmRg 4HsWi.YbI6THP8kK5CvhNL.WRHKM7fKyCDDiyZOna1VF.LDXtGsivLQHujnk dE7Qxr.MqImcHZEymG_cFGqhJqDVAlFTV5WtXIt1dH.zwHhP6gi2oCAc1.6Z HW8VjtT6F0P_cmjosCcUFYk6GlSiqGL_JeuC8d6o1.4fRw6vMbrvvxsmy_zn KGOMDestzFkrOQQXhICAqN3umCKx6pmBgXzeRnGRflAlq1lqLzVOwYC4c7EL mIkLKNYhblT9oueZ78TkuqabDYz63YDXKqrEa.ohRlhlkkITtBf5SDL0NDSz RhEx4ccly0XXydpHsw3EKSbkw8oLPE5HSCSCKUrPp27UKvhdflvqfuILN8Mf 7j2aLHel44JCpTXVpsyDbjhDjDT1So.rL7o8h Received: from [98.210.146.78] by web125904.mail.ne1.yahoo.com via HTTP; Fri, 05 Apr 2013 20:32:56 PDT X-Rocket-MIMEInfo: 002.001, CgpKb2UgLS0KCj5fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fXwo.IEZyb206IEpvZSBWYW4gRHlrIDxqb2VAdGFuZ2EuY29tPgo.VG86IEdyZWcgV2lsbGlhbXNvbiA8Z3dpbGxpYW1zb24zOUB5YWhvby5jb20.IAo.Q2M6ICJwZ3NxbC1wZXJmb3JtYW5jZUBwb3N0Z3Jlc3FsLm9yZyIgPHBnc3FsLXBlcmZvcm1hbmNlQHBvc3RncmVzcWwub3JnPiAKPlNlbnQ6IEZyaWRheSwgQXByaWwgNSwgMjAxMyA3OjU2IFBNCj5TdWJqZWN0OiBSZTogW1BFUkZPUk1dIHNsb3cgam9pbnM_Cj4gCj4KPk9uIEZyaSwBMAEBAQE- X-Mailer: YahooMailWebService/0.8.140.532 References: <1365213250.66493.YahooMailNeo@web125901.mail.ne1.yahoo.com> Message-ID: <1365219176.2155.YahooMailNeo@web125904.mail.ne1.yahoo.com> Date: Fri, 5 Apr 2013 20:32:56 -0700 (PDT) From: Greg Williamson Reply-To: Greg Williamson Subject: Re: slow joins? Cc: "pgsql-performance@postgresql.org" In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.9 (--) 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 Joe -- >________________________________ > From: Joe Van Dyk >To: Greg Williamson =20 >Cc: "pgsql-performance@postgresql.org" = =20 >Sent: Friday, April 5, 2013 7:56 PM >Subject: Re: [PERFORM] slow joins? >=20 > >On Fri, Apr 5, 2013 at 6:54 PM, Greg Williamson = wrote: > >Joe -- >> >>>________________________________ >>> From: Joe Van Dyk >>>To: pgsql-performance@postgresql.org >>>Sent: Friday, April 5, 2013 6:42 PM >>>Subject: Re: [PERFORM] slow joins? >> >>> >>> >>>(https://gist.github.com/joevandyk/df0df703f3fda6d14ae1/raw/c15cae813913= b7f8c35b24b467a0c732c0100d79/gistfile1.txt shows a non-wrapped version of t= he queries and plan) >>> >>> >>> >>> >>>On Fri, Apr 5, 2013 at 6:38 PM, Joe Van Dyk wrote: >>> >>>On 9.2.4, running two identical queries except for the value of a column= in the WHERE clause. Postgres is picking very different query plans, the f= irst is much slower than the second. >>>> >>>> >>>>Any ideas on how I can speed this up? =A0I have btree indexes for all t= he columns used in the query. >>>> >>>>explain analyze =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 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 >>>>SELECT COUNT(*) =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 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 >>>>FROM purchased_items pi =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 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 >>>>inner join line_items li on li.id =3D pi.line_item_id =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 >>>>inner join products =A0 =A0 =A0on products.id =3D li.product_id =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 >>>>WHERE products.drop_shipper_id =3D 221; >>>> >>>>=A0Aggregate =A0(cost=3D193356.31..193356.32 rows=3D1 width=3D0) (actua= l time=3D2425.225..2425.225 rows=3D1 loops=3D1) >>>>=A0 =A0-> =A0Hash Join =A0(cost=3D78864.43..193160.41 rows=3D78360 widt= h=3D0) (actual time=3D726.612..2424.206 rows=3D8413 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0Hash Cond: (pi.line_item_id =3D li.id) >>>>=A0 =A0 =A0 =A0 =A0-> =A0Seq Scan on purchased_items pi =A0(cost=3D0.00= ..60912.39 rows=3D3724639 width=3D4) (actual time=3D0.008..616.812 rows=3D3= 724639 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0-> =A0Hash =A0(cost=3D77937.19..77937.19 rows=3D5649= 9 width=3D4) (actual time=3D726.231..726.231 rows=3D8178 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Buckets: 4096 =A0Batches: 4 =A0Memory Us= age: 73kB >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Hash Join =A0(cost=3D1684.33..7793= 7.19 rows=3D56499 width=3D4) (actual time=3D1.270..723.222 rows=3D8178 loop= s=3D1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Hash Cond: (li.product_id = =3D products.id) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Seq Scan on line_items= li =A0(cost=3D0.00..65617.18 rows=3D2685518 width=3D8) (actual time=3D0.08= 1..392.926 rows=3D2685499 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Hash =A0(cost=3D1676.6= 0..1676.60 rows=3D618 width=3D4) (actual time=3D0.835..0.835 rows=3D618 loo= ps=3D1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Buckets: 1024 = =A0Batches: 1 =A0Memory Usage: 22kB >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Bitmap Hea= p Scan on products =A0(cost=3D13.07..1676.60 rows=3D618 width=3D4) (actual = time=3D0.185..0.752 rows=3D618 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Rech= eck Cond: (drop_shipper_id =3D 221) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> = =A0Bitmap Index Scan on index_products_on_drop_shipper_id =A0(cost=3D0.00..= 12.92 rows=3D618 width=3D0) (actual time=3D0.125..0.125 rows=3D618 loops=3D= 1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0Index Cond: (drop_shipper_id =3D 221) >>>>=A0Total runtime: 2425.302 ms >>>> >>>> >>>>explain analyze =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 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 >>>>SELECT COUNT(*) =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 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 >>>>FROM purchased_items pi =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 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 >>>>inner join line_items li on li.id =3D pi.line_item_id =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 >>>>inner join products =A0 =A0 =A0on products.id =3D li.product_id =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 >>>>WHERE products.drop_shipper_id =3D 2; =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= =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 =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 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 >>>> >>>>=A0Aggregate =A0(cost=3D29260.40..29260.41 rows=3D1 width=3D0) (actual = time=3D0.906..0.906 rows=3D1 loops=3D1) >>>>=A0 =A0-> =A0Nested Loop =A0(cost=3D0.00..29254.38 rows=3D2409 width=3D= 0) (actual time=3D0.029..0.877 rows=3D172 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0-> =A0Nested Loop =A0(cost=3D0.00..16011.70 rows=3D1= 737 width=3D4) (actual time=3D0.021..0.383 rows=3D167 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Index Scan using index_products_on= _drop_shipper_id on products =A0(cost=3D0.00..80.41 rows=3D19 width=3D4) (a= ctual time=3D0.010..0.074 rows=3D70 loops=3D1) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Index Cond: (drop_shipper_id= =3D 2) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Index Scan using index_line_items_= on_product_id on line_items li =A0(cost=3D0.00..835.70 rows=3D279 width=3D8= ) (actual time=3D0.002..0.004 rows=3D2 loops=3D70) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Index Cond: (product_id =3D = products.id) >>>>=A0 =A0 =A0 =A0 =A0-> =A0Index Only Scan using purchased_items_line_ite= m_id_idx on purchased_items pi =A0(cost=3D0.00..7.60 rows=3D2 width=3D4) (a= ctual time=3D0.002..0.003 rows=3D1 loops=3D167) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Index Cond: (line_item_id =3D li.id) >>>>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Heap Fetches: 5 >>>>=A0Total runtime: 0.955 ms >>>>(11 rows) >>>> >>> >> >> >>Does drop_shipper+id have a much larger number of rows which is making th= e scanner want to avoid an indexed scan or otherwise prefer a sequential sc= an on products and on line_items ? >> > > >Assuming you mean products.drop_shipper_id? There are more rows matched fo= r the first one vs the second one.=A0 >70 products rows match drop_shipper_id=3D2, 618 match drop_shipper_id=3D22= 1. >=A0 >What are the stats settings for these tables ? >> > > >Whatever the defaults are. >=A0 I mis-pasted the tables -- both line_items and purchased items are getting = sequential scans for the relevant rows; it is possible that that there's en= ough difference to tip the planner to use sequential scans. You might try increasing the stats being collected on those two tables, run= analyze on all the tables in the query, and try it again. GW --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance