X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 7E57F9DD75A for ; Thu, 8 Dec 2005 17:54:00 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 68882-08 for ; Thu, 8 Dec 2005 17:54:00 -0400 (AST) X-Greylist: delayed 03:07:12.637815 by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by postgresql.org (Postfix) with ESMTP id E69C79DCBDC for ; Thu, 8 Dec 2005 17:53:56 -0400 (AST) Received: from mail.aveo.aveopharma.com (67.109.105.227.ptr.us.xo.net [67.109.105.227]) by svr2.postgresql.org (Postfix) with SMTP id BAC21F0B6B for ; Thu, 8 Dec 2005 18:46:43 +0000 (GMT) X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: Joining 2 tables with 300 million rows Date: Thu, 8 Dec 2005 13:47:13 -0500 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [PERFORM] Joining 2 tables with 300 million rows Thread-Index: AcX8HG6Y+DJkrdJTRuiS12nFfWLsPgACzKrg From: "Dmitri Bichko" To: "Amit V Shah" , X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/253 X-Sequence-Number: 16074 What's the problem? You are joining two 300 million row tables in 0.15 of a second - seems reasonable. Dmitri > -----Original Message----- > From: pgsql-performance-owner@postgresql.org=20 > [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of=20 > Amit V Shah > Sent: Thursday, December 08, 2005 11:59 AM > To: 'pgsql-performance@postgresql.org' > Subject: [PERFORM] Joining 2 tables with 300 million rows >=20 >=20 > Hi all, >=20 > First of all, please pardon if the question is dumb! Is it=20 > even feasible or normal to do such a thing ! This query is=20 > needed by a webpage so needs to be lightning fast. Anything=20 > beyond 2-3 seconds is unacceptable performance. >=20 > I have two tables >=20 > CREATE TABLE runresult > ( > id_runresult int8 NOT NULL, > rundefinition_id_rundefinition int4 NOT NULL, > measure_id_measure int4 NOT NULL, > value float4 NOT NULL, > "sequence" varchar(20) NOT NULL, > CONSTRAINT pk_runresult_ars PRIMARY KEY (id_runresult), > )=20 >=20 >=20 > CREATE TABLE runresult_has_catalogtable > ( > runresult_id_runresult int8 NOT NULL, > catalogtable_id_catalogtable int4 NOT NULL, > value int4 NOT NULL, > CONSTRAINT pk_runresult_has_catalogtable PRIMARY KEY=20 > (runresult_id_runresult, catalogtable_id_catalogtable, value) > CONSTRAINT fk_temp FOREIGN KEY (runresult_id_runresult) REFERENCES > runresult(id_runresult) ON UPDATE RESTRICT ON DELETE RESTRICT > )=20 >=20 > Each table has around 300 million records (will grow to=20 > probably billions). Below is the query and the explain analyze -- >=20 > explain analyze SELECT measure.description, runresult.value=20 > FROM ((((rundefinition INNER JOIN runresult ON=20 > rundefinition.id_rundefinition =3D=20 > runresult.rundefinition_id_rundefinition)=20 > INNER JOIN runresult_has_catalogtable ON runresult.id_runresult =3D > runresult_has_catalogtable.runresult_id_runresult)=20 > INNER JOIN runresult_has_catalogtable AS=20 > runresult_has_catalogtable_1 ON runresult.id_runresult =3D > runresult_has_catalogtable_1.runresult_id_runresult)=20 > INNER JOIN runresult_has_catalogtable AS=20 > runresult_has_catalogtable_2 ON runresult.id_runresult =3D > runresult_has_catalogtable_2.runresult_id_runresult)=20 > INNER JOIN measure ON runresult.measure_id_measure =3D=20 > measure.id_measure WHERE=20 > (((runresult_has_catalogtable.catalogtable_id_catalogtable)=3D52)=20 > AND ((runresult_has_catalogtable_1.catalogtable_id_catalogtable)=3D54)=20= > AND ((runresult_has_catalogtable_2.catalogtable_id_catalogtable)=3D55)=20= > AND ((runresult_has_catalogtable.value)=3D15806)=20 > AND ((runresult_has_catalogtable_1.value)=3D1)=20 > AND ((runresult_has_catalogtable_2.value) In (21,22,23,24))=20 > AND ((rundefinition.id_rundefinition)=3D10106)); >=20 > 'Nested Loop (cost=3D0.00..622582.70 rows=3D1 width=3D28) (actual=20 > time=3D25.221..150.563 rows=3D22 loops=3D1)' ' -> Nested Loop =20 > (cost=3D0.00..622422.24 rows=3D2 width=3D52) (actual=20 > time=3D25.201..150.177 rows=3D22 loops=3D1)' > ' -> Nested Loop (cost=3D0.00..622415.97 rows=3D2=20 > width=3D32) (actual > time=3D25.106..149.768 rows=3D22 loops=3D1)' > ' -> Nested Loop (cost=3D0.00..621258.54 rows=3D15=20 > width=3D24) > (actual time=3D24.582..149.061 rows=3D30 loops=3D1)' > ' -> Index Scan using pk_rundefinition on=20 > rundefinition > (cost=3D0.00..3.86 rows=3D1 width=3D4) (actual time=3D0.125..0.147=20 > rows=3D1 loops=3D1)' > ' Index Cond: (id_rundefinition =3D 10106)' > ' -> Nested Loop (cost=3D0.00..621254.54 rows=3D15= > width=3D28) (actual time=3D24.443..148.784 rows=3D30 loops=3D1)' > ' -> Index Scan using > runresult_has_catalogtable_value on=20 > runresult_has_catalogtable (cost=3D0.00..575069.35 rows=3D14437=20 > width=3D8) (actual time=3D0.791..33.036 rows=3D10402 loops=3D1)' > ' Index Cond: (value =3D 15806)' > ' Filter:=20 > (catalogtable_id_catalogtable =3D > 52)' > ' -> Index Scan using pk_runresult_ars on > runresult (cost=3D0.00..3.19 rows=3D1 width=3D20) (actual=20 > time=3D0.007..0.007 rows=3D0 loops=3D10402)' > ' Index Cond: (runresult.id_runresult =3D= > "outer".runresult_id_runresult)' > ' Filter: (10106 =3D > rundefinition_id_rundefinition)' > ' -> Index Scan using=20 > runresult_has_catalogtable_id_runresult > on runresult_has_catalogtable runresult_has_catalogtable_1=20 > (cost=3D0.00..76.65 rows=3D41 width=3D8) (actual time=3D0.015..0.017=20= > rows=3D1 loops=3D30)' > ' Index Cond: > (runresult_has_catalogtable_1.runresult_id_runresult =3D=20 > "outer".runresult_id_runresult)' > ' Filter: ((catalogtable_id_catalogtable =3D=20 > 54) AND (value > =3D 1))' > ' -> Index Scan using pk_measure on measure =20 > (cost=3D0.00..3.12 rows=3D1 > width=3D28) (actual time=3D0.008..0.010 rows=3D1 loops=3D22)' > ' Index Cond: ("outer".measure_id_measure =3D > measure.id_measure)' > ' -> Index Scan using=20 > runresult_has_catalogtable_id_runresult on=20 > runresult_has_catalogtable runresult_has_catalogtable_2 =20 > (cost=3D0.00..79.42 rows=3D65 width=3D8) (actual time=3D0.007..0.010=20= > rows=3D1 loops=3D22)' > ' Index Cond:=20 > (runresult_has_catalogtable_2.runresult_id_runresult =3D > "outer".runresult_id_runresult)' > ' Filter: ((catalogtable_id_catalogtable =3D 55) AND=20 > ((value =3D 21) OR > (value =3D 22) OR (value =3D 23) OR (value =3D 24)))' > 'Total runtime: 150.863 ms' >=20 >=20 >=20 > ---------------------------(end of=20 > broadcast)--------------------------- > TIP 5: don't forget to increase your free space map settings >=20 The information transmitted is intended only for the person or entity to = which it is addressed and may contain confidential and/or privileged mate= rial. Any review, retransmission, dissemination or other use of, or takin= g of any action in reliance upon, this information by persons or entities= other than the intended recipient is prohibited. If you received this in= error, please contact the sender and delete the material from any comput= er