Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UOIKh-0001Jy-Og for pgsql-performance@arkaria.postgresql.org; Sat, 06 Apr 2013 01:54:20 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UOIKh-0004IV-9J for pgsql-performance@arkaria.postgresql.org; Sat, 06 Apr 2013 01:54:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UOIKg-0004IQ-9w for pgsql-performance@postgresql.org; Sat, 06 Apr 2013 01:54:18 +0000 Received: from nm16-vm0.bullet.mail.ne1.yahoo.com ([98.138.91.49]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UOIKb-0004J0-Ha for pgsql-performance@postgresql.org; Sat, 06 Apr 2013 01:54:17 +0000 Received: from [98.138.90.54] by nm16.bullet.mail.ne1.yahoo.com with NNFMP; 06 Apr 2013 01:54:11 -0000 Received: from [98.138.88.233] by tm7.bullet.mail.ne1.yahoo.com with NNFMP; 06 Apr 2013 01:54:11 -0000 Received: from [127.0.0.1] by omp1033.mail.ne1.yahoo.com with NNFMP; 06 Apr 2013 01:54:11 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 104679.77725.bm@omp1033.mail.ne1.yahoo.com Received: (qmail 67147 invoked by uid 60001); 6 Apr 2013 01:54:11 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1365213251; bh=wBJCz+bwYXNhRzqC0pq/LIIzS/vxAAl0kzzOVBU+hvQ=; 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=JR/KE1L4KCPamPkN2zDRyQYHvo59HKrRLcTI1icFEFfdBBAOI181ms+pPLc4tPphVVBHHhFpYI48Vcb4aTAZNZnzRsPkYvNFfY3F2bOnmDeOHpQp1yfrWzaPo2gcOtDBZnvcTyHPG+m2tgvzvSlVuVGjmhfqC/0Qkxc3CKKp3ZA= 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:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=cuLQ/DBfkbUVfouRhdBOEyBz5xuzw3alYBRWcl4/A0XtXwv+/JzPfqUzpP3oc2jBOeHbNAOUTtabKTuuWnxkIAKn3tC9Qwe7w0UT8ZCbrR6bx9yPkikvZWCF6qleW2jDbJbgqJuBDR6ebwfdvUdc2xs5EqR9Gc0JEcUhjTXCfxw=; X-YMail-OSG: x.CONQMVM1m6Z6RxnBSw5XHZYhbgDvFAh_8fePkD3PKxrgs XJqJaf.pvJ9DPgCu3EWIMadeSPbjNr71Bth.3_rdiRvWrgk3d5nULmwpkpAP sznz671mVwUs81nd9CuIY.AbpXOu0MmTuL4.EQTGYCvdxMaUcLrfnZyZC566 FozcQ269SilSNr.U1EarfpD.JZ6vz737xyn6bUX.h3ko.F3r3KC6Kk2hmGe5 SDiycnlr9F0fSLaYOSG3qMXj_T0sX1tx7pgtylFvuZ3fZm.b2RiA4YaM1pXF i8nU0UPlYN5ss.wxEwstZ7UAJoE8WwceW8PVoXwBEnIsDt1N4rvgnSgkH64v .er_6dR.o7nR1tAFmG1g_X3miFi0k3pYucTLHQC9LGXYTDagY.OI0Kfykttw 8XXdpsnAlVYu7woZ633NEQ8tbeelCK3IJSOwt8qKwt36AakZT99XLz3Yp7rn A3uGqaGWXr3HJ49_hx_r4gmRx1AOThVRUQoJo10t9Am0z8ko3jjNi95ZA1So _6td1OlBlYP2BiDSPRF9FgJR9.CuGizV6DXgCv9CPs9DlX2ynkVg.Q4Nxj1j yYriMBmfb.09_eberuEcn4OtyPkO1FGoFadyz0Lx4mWMolQ9SLPhuMcz6Lu4 QDOCHOJb_Iqd__FjumYtD_GzSaZzZcbs3hjQiRJlapCmTpJ8_Bqjz9b7GzDH Ge0KF6vf5JVCISGiv_oGhZPcof4joPALptcudV.tLC8kGU9v3fzNozMXdlz5 RuIvXUEFIrYU2WFgbfID4N9yKUUGjrtGngand Received: from [98.210.146.78] by web125901.mail.ne1.yahoo.com via HTTP; Fri, 05 Apr 2013 18:54:10 PDT X-Rocket-MIMEInfo: 002.001, Sm9lIC0tCgo.X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KPiBGcm9tOiBKb2UgVmFuIER5ayA8am9lQHRhbmdhLmNvbT4KPlRvOiBwZ3NxbC1wZXJmb3JtYW5jZUBwb3N0Z3Jlc3FsLm9yZyAKPlNlbnQ6IEZyaWRheSwgQXByaWwgNSwgMjAxMyA2OjQyIFBNCj5TdWJqZWN0OiBSZTogW1BFUkZPUk1dIHNsb3cgam9pbnM_Cj4gCj4KPihodHRwczovL2dpc3QuZ2l0aHViLmNvbS9qb2V2YW5keWsvZGYwZGY3MDNmM2ZkYTZkMTRhZTEvcmF3L2MxNWNhZTgxMzkxM2I3ZjhjMzViMjRiNDY3YTBjNzMyYzABMAEBAQE- X-Mailer: YahooMailWebService/0.8.140.532 References: Message-ID: <1365213250.66493.YahooMailNeo@web125901.mail.ne1.yahoo.com> Date: Fri, 5 Apr 2013 18:54:10 -0700 (PDT) From: Greg Williamson Reply-To: Greg Williamson Subject: Re: slow joins? To: "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: -3.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: pgsql-performance@postgresql.org=20 >Sent: Friday, April 5, 2013 6:42 PM >Subject: Re: [PERFORM] slow joins? >=20 > >(https://gist.github.com/joevandyk/df0df703f3fda6d14ae1/raw/c15cae813913b7= f8c35b24b467a0c732c0100d79/gistfile1.txt shows a non-wrapped version of the= 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 i= n the WHERE clause. Postgres is picking very different query plans, the fir= st is much slower than the second. >> >> >>Any ideas on how I can speed this up? =A0I have btree indexes for all the= 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=20 >>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=20 >>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=20 >>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=20 >>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=20 >>WHERE products.drop_shipper_id =3D 221; >> >>=A0Aggregate =A0(cost=3D193356.31..193356.32 rows=3D1 width=3D0) (actual = time=3D2425.225..2425.225 rows=3D1 loops=3D1) >>=A0 =A0-> =A0Hash Join =A0(cost=3D78864.43..193160.41 rows=3D78360 width= =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=3D372= 4639 loops=3D1) >>=A0 =A0 =A0 =A0 =A0-> =A0Hash =A0(cost=3D77937.19..77937.19 rows=3D56499 = width=3D4) (actual time=3D726.231..726.231 rows=3D8178 loops=3D1) >>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Buckets: 4096 =A0Batches: 4 =A0Memory Usag= e: 73kB >>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Hash Join =A0(cost=3D1684.33..77937.= 19 rows=3D56499 width=3D4) (actual time=3D1.270..723.222 rows=3D8178 loops= =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 l= i =A0(cost=3D0.00..65617.18 rows=3D2685518 width=3D8) (actual time=3D0.081.= .392.926 rows=3D2685499 loops=3D1) >>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Hash =A0(cost=3D1676.60.= .1676.60 rows=3D618 width=3D4) (actual time=3D0.835..0.835 rows=3D618 loops= =3D1) >>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Buckets: 1024 =A0B= atches: 1 =A0Memory Usage: 22kB >>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0Bitmap Heap = Scan on products =A0(cost=3D13.07..1676.60 rows=3D618 width=3D4) (actual ti= me=3D0.185..0.752 rows=3D618 loops=3D1) >>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Rechec= k Cond: (drop_shipper_id =3D 221) >>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-> =A0= Bitmap 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=3D1) >>=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=20 >>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=20 >>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=20 >>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=20 >>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=20 >>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=20 >>=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=20 >> >>=A0Aggregate =A0(cost=3D29260.40..29260.41 rows=3D1 width=3D0) (actual ti= me=3D0.906..0.906 rows=3D1 loops=3D1) >>=A0 =A0-> =A0Nested Loop =A0(cost=3D0.00..29254.38 rows=3D2409 width=3D0)= (actual time=3D0.029..0.877 rows=3D172 loops=3D1) >>=A0 =A0 =A0 =A0 =A0-> =A0Nested Loop =A0(cost=3D0.00..16011.70 rows=3D173= 7 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_d= rop_shipper_id on products =A0(cost=3D0.00..80.41 rows=3D19 width=3D4) (act= ual 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 pr= oducts.id) >>=A0 =A0 =A0 =A0 =A0-> =A0Index Only Scan using purchased_items_line_item_= id_idx on purchased_items pi =A0(cost=3D0.00..7.60 rows=3D2 width=3D4) (act= ual 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 the = scanner want to avoid an indexed scan or otherwise prefer a sequential scan= on products and on line_items ? What are the stats settings for these tables ? HTH, Greg WIlliamson --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance