X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id A01A0D1B543 for ; Sun, 26 Oct 2003 09:30:48 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 03656-02 for ; Sun, 26 Oct 2003 05:30:19 -0400 (AST) Received: from svana.org (svana.org [203.20.62.76]) by svr1.postgresql.org (Postfix) with ESMTP id DF0C2D1B51B for ; Sun, 26 Oct 2003 05:30:15 -0400 (AST) Received: from kleptog by svana.org with local (Exim 3.35 #1 (Debian)) id 1ADhDY-0005Iu-00; Sun, 26 Oct 2003 20:29:48 +1100 Date: Sun, 26 Oct 2003 20:29:48 +1100 From: Martijn van Oosterhout To: Yonatan Goraly Cc: pgsql-general@postgresql.org Subject: Re: Slow performance with no apparent reason Message-ID: <20031026092948.GC15100@svana.org> Reply-To: Martijn van Oosterhout Mail-Followup-To: Yonatan Goraly , pgsql-general@postgresql.org References: <3F9AEA51.2070701@sbcglobal.net> <20031026074354.GB15100@svana.org> <3F9AF88E.3040101@sbcglobal.net> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="KDt/GgjP6HVcx58l" Content-Disposition: inline In-Reply-To: <3F9AF88E.3040101@sbcglobal.net> User-Agent: Mutt/1.3.28i X-PGP-Key-ID: Length=1024; ID=0x0DC67BE6 X-PGP-Key-Fingerprint: 295F A899 A81A 156D B522 48A7 6394 F08A 0DC6 7BE6 X-PGP-Key-URL: X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/1240 X-Sequence-Number: 51313 --KDt/GgjP6HVcx58l Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Ok, those figures look like you've mever run ANALYZE on that database at all, given you keep getting the default values. EXPLAIN ANALYZE would have given the actual number of matching rows. Given that, the plans are probably extremely suboptimal. Also, do you have (unique) indexes on the columns that need it. So the EXPLAIN ANALYZE output after running ANALYZE over your database would be the next step. Hope this helps, On Sun, Oct 26, 2003 at 01:26:22AM +0300, Yonatan Goraly wrote: > I guess my first message was not accurate, since t1 is a view, that=20 > includes t2. >=20 > Attached are the real queries with their corresponding plans, the first= =20 > one takes 10.8 sec to execute, the second one takes 0.6 sec. >=20 > To simplify, I expanded the view, so the attached query refers to tables= =20 > only. >=20 > Martijn van Oosterhout wrote: >=20 > >Please supply EXPLAIN ANALYZE output. > > > >On Sun, Oct 26, 2003 at 12:25:37AM +0300, Yonatan Goraly wrote: > >=20 > > > >>I am in the process of adding PostgreSQL support for an application, i= n=20 > >>addition to Oracle and MS SQL. > >>I am using PostgreSQL version 7.3.2, Red Hat 9.0 on Intel Pentium III= =20 > >>board. > >> > >>I have a query that generally looks like this: > >> > >>SELECT t1.col1, t2.col1 FROM t1, t2 WHERE t1.x=3Dt2.y AND t2.p=3D'strin= g'=20 > >>AND t2.q=3D1 > >> > >>This query is strikingly slow (about 100 sec when both t1 and t2 has=20 > >>about 1,200 records, compare with less than 4 sec with MS SQL and Oracl= e) > >> > >>The strange thing is that if I remove one of the last 2 conditions=20 > >>(doesn't matter which one), I get the same performance like with the=20 > >>other databases. > >>Since in this particular case both conditions ( t2.p=3D'string', t2.q= =3D1)=20 > >>are not required, I can't understand why having both turns the query so= =20 > >>slow. > >>A query on table t2 alone is fast with or without the 2 conditions. > >> > >>I tired several alternatives, this one works pretty well: > >> > >>SELECT t1.col1, t2.col1 FROM t1, t2 WHERE t1.x=3Dt2.y AND > >> EXISTS ( > >> SELECT * FROM t2 t2a WHERE t2a.p=3D'string' AND t2a.q=3D1 AN= D=20 > >>t2a.y=3Dt2.y ) > >> > >>Since the first query is simpler than the second, it seems to me like a= =20 > >>bug. > >> > >>Please advise > >> > >>Yonatan > >>=20=20=20 > >> > > > >=20 > > > -------------------------------------------------------------------------= ----------------------------------- > slow query(10 sec): >=20 > select ent.ID,ent.TYPE,ent.STATUS,ent.NAME > from (select > e.ID, e.TYPE, e.STATUS, e.NAME > from > ENT_PROJECT e, > (select h.*, > CASE WHEN f1.ID=3D-1 THEN '' ELSE f1.NAME || > CASE WHEN f2.ID=3D-1 THEN '' ELSE ' > ' || f2.NAME || > CASE WHEN f3.ID=3D-1 THEN '' ELSE ' > ' || f3.NAME || > CASE WHEN f4.ID=3D-1 THEN '' ELSE ' > ' || f4.NAME || > CASE WHEN f5.ID=3D-1 THEN '' ELSE ' > ' || f5.NAME || > CASE WHEN f6.ID=3D-1 THEN '' ELSE ' > ' || f6.NAME END= END END END END END as PATH > from COMN_ATTR_HIERARCH h > join ENT_FOLDER f1 on h.FOLDER_ID_1=3Df1.ID > join ENT_FOLDER f2 on h.FOLDER_ID_2=3Df2.ID > join ENT_FOLDER f3 on h.FOLDER_ID_3=3Df3.ID > join ENT_FOLDER f4 on h.FOLDER_ID_4=3Df4.ID > join ENT_FOLDER f5 on h.FOLDER_ID_5=3Df5.ID > join ENT_FOLDER f6 on h.FOLDER_ID_6=3Df6.ID > ) path > where e.STATUS!=3Dcast(-1 as numeric) > and e.ID =3D path.NODE_ID) ent , COMN_ATTR_HIERARCH hier > where hier.NODE_ID=3Dent.ID and hier.HIERARCHY_ID=3D'IMPLEMENTATION' = and hier.DOMAIN=3D1 >=20 >=20 > -------------------------------------------------------------------------= ----------------------------------- > QUERY PLAN > Nested Loop (cost=3D1808.05..1955.27 rows=3D14 width=3D660) > Join Filter: ("outer".id =3D "inner".node_id) > -> Nested Loop (cost=3D0.00..10.82 rows=3D1 width=3D244) > -> Index Scan using idx_hierarch_hierarch_id on comn_attr_hierar= ch hier (cost=3D0.00..5.98 rows=3D1 width=3D32) > Index Cond: ((hierarchy_id =3D 'IMPLEMENTATION'::bpchar) AN= D ("domain" =3D 1::numeric)) > -> Index Scan using pk_ent_project on ent_project e (cost=3D0.0= 0..4.83 rows=3D1 width=3D212) > Index Cond: ("outer".node_id =3D e.id) > Filter: (status <> -1::numeric) > -> Materialize (cost=3D1910.33..1910.33 rows=3D2730 width=3D416) > -> Merge Join (cost=3D1808.05..1910.33 rows=3D2730 width=3D416) > Merge Cond: ("outer".id =3D "inner".folder_id_6) > -> Index Scan using pk_ent_folder on ent_folder f6 (cost= =3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D1808.05..1814.88 rows=3D2730 width=3D384) > Sort Key: h.folder_id_6 > -> Merge Join (cost=3D1275.45..1377.73 rows=3D2730 = width=3D384) > Merge Cond: ("outer".id =3D "inner".folder_id_5) > -> Index Scan using pk_ent_folder on ent_folde= r f5 (cost=3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D1275.45..1282.28 rows=3D2730 = width=3D352) > Sort Key: h.folder_id_5 > -> Merge Join (cost=3D1017.37..1119.64 = rows=3D2730 width=3D352) > Merge Cond: ("outer".id =3D "inner"= .folder_id_4) > -> Index Scan using pk_ent_folder = on ent_folder f4 (cost=3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D1017.37..1024.19 = rows=3D2730 width=3D320) > Sort Key: h.folder_id_4 > -> Merge Join (cost=3D759.2= 8..861.56 rows=3D2730 width=3D320) > Merge Cond: ("outer".id= =3D "inner".folder_id_3) > -> Index Scan using pk= _ent_folder on ent_folder f3 (cost=3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D759.2= 8..766.11 rows=3D2730 width=3D288) > Sort Key: h.folde= r_id_3 > -> Merge Join (= cost=3D501.20..603.47 rows=3D2730 width=3D288) > Merge Cond:= ("outer".id =3D "inner".folder_id_2) > -> Index S= can using pk_ent_folder on ent_folder f2 (cost=3D0.00..52.00 rows=3D1000 w= idth=3D32) > -> Sort (= cost=3D501.20..508.02 rows=3D2730 width=3D256) > Sort = Key: h.folder_id_2 > -> M= erge Join (cost=3D243.11..345.39 rows=3D2730 width=3D256) > = Merge Cond: ("outer".id =3D "inner".folder_id_1) > = -> Index Scan using pk_ent_folder on ent_folder f1 (cost=3D0.00..52.00 r= ows=3D1000 width=3D32) > = -> Sort (cost=3D243.11..249.94 rows=3D2730 width=3D224) > = Sort Key: h.folder_id_1 > = -> Seq Scan on comn_attr_hierarch h (cost=3D0.00..87.30 rows=3D273= 0 width=3D224) >=20 >=20 > -------------------------------------------------------------------------= ----------------------------------- > Fast query (.6 sec): >=20 > select ent.ID,ent.TYPE,ent.STATUS,ent.NAME > from (select > e.ID, e.TYPE, e.STATUS, e.NAME > from > ENT_PROJECT e, > (select h.*, > CASE WHEN f1.ID=3D-1 THEN '' ELSE f1.NAME || > CASE WHEN f2.ID=3D-1 THEN '' ELSE ' > ' || f2.NAME || > CASE WHEN f3.ID=3D-1 THEN '' ELSE ' > ' || f3.NAME || > CASE WHEN f4.ID=3D-1 THEN '' ELSE ' > ' || f4.NAME || > CASE WHEN f5.ID=3D-1 THEN '' ELSE ' > ' || f5.NAME || > CASE WHEN f6.ID=3D-1 THEN '' ELSE ' > ' || f6.NAME END= END END END END END as PATH > from COMN_ATTR_HIERARCH h > join ENT_FOLDER f1 on h.FOLDER_ID_1=3Df1.ID > join ENT_FOLDER f2 on h.FOLDER_ID_2=3Df2.ID > join ENT_FOLDER f3 on h.FOLDER_ID_3=3Df3.ID > join ENT_FOLDER f4 on h.FOLDER_ID_4=3Df4.ID > join ENT_FOLDER f5 on h.FOLDER_ID_5=3Df5.ID > join ENT_FOLDER f6 on h.FOLDER_ID_6=3Df6.ID > ) path > where e.STATUS!=3Dcast(-1 as numeric) > and e.ID =3D path.NODE_ID) ent , COMN_ATTR_HIERARCH hier > where hier.NODE_ID=3Dent.ID and exists( > select * from COMN_ATTR_HIERARCH h2 where h2.HIERARCHY_ID=3D'IMPLEMENT= ATION' and h2.DOMAIN=3D1 and h2.NODE_ID=3Dhier.NODE_ID > and h2.HIERARCHY_ID=3Dhier.HIERARCHY_ID and h2.DOMAIN=3Dhier.DOMAIN) >=20 >=20 > -------------------------------------------------------------------------= ----------------------------------- > QUERY PLAN > Merge Join (cost=3D16145.60..16289.84 rows=3D18539 width=3D660) > Merge Cond: ("outer".id =3D "inner".node_id) > -> Merge Join (cost=3D13782.29..13863.08 rows=3D1358 width=3D244) > Merge Cond: ("outer".id =3D "inner".node_id) > -> Index Scan using pk_ent_project on ent_project e (cost=3D0.0= 0..54.50 rows=3D995 width=3D212) > Filter: (status <> -1::numeric) > -> Sort (cost=3D13782.29..13785.70 rows=3D1365 width=3D32) > Sort Key: hier.node_id > -> Seq Scan on comn_attr_hierarch hier (cost=3D0.00..1371= 1.21 rows=3D1365 width=3D32) > Filter: (subplan) > SubPlan > -> Index Scan using pk_comn_attr_hierarch on comn_= attr_hierarch h2 (cost=3D0.00..4.99 rows=3D1 width=3D316) > Index Cond: (("domain" =3D 1::numeric) AND ("= domain" =3D $2) AND (node_id =3D $0)) > Filter: ((hierarchy_id =3D 'IMPLEMENTATION'::= bpchar) AND (hierarchy_id =3D $1)) > -> Sort (cost=3D2363.32..2370.14 rows=3D2730 width=3D416) > Sort Key: h.node_id > -> Merge Join (cost=3D1808.05..1910.33 rows=3D2730 width=3D416) > Merge Cond: ("outer".id =3D "inner".folder_id_6) > -> Index Scan using pk_ent_folder on ent_folder f6 (cost= =3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D1808.05..1814.88 rows=3D2730 width=3D384) > Sort Key: h.folder_id_6 > -> Merge Join (cost=3D1275.45..1377.73 rows=3D2730 = width=3D384) > Merge Cond: ("outer".id =3D "inner".folder_id_5) > -> Index Scan using pk_ent_folder on ent_folde= r f5 (cost=3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D1275.45..1282.28 rows=3D2730 = width=3D352) > Sort Key: h.folder_id_5 > -> Merge Join (cost=3D1017.37..1119.64 = rows=3D2730 width=3D352) > Merge Cond: ("outer".id =3D "inner"= .folder_id_4) > -> Index Scan using pk_ent_folder = on ent_folder f4 (cost=3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D1017.37..1024.19 = rows=3D2730 width=3D320) > Sort Key: h.folder_id_4 > -> Merge Join (cost=3D759.2= 8..861.56 rows=3D2730 width=3D320) > Merge Cond: ("outer".id= =3D "inner".folder_id_3) > -> Index Scan using pk= _ent_folder on ent_folder f3 (cost=3D0.00..52.00 rows=3D1000 width=3D32) > -> Sort (cost=3D759.2= 8..766.11 rows=3D2730 width=3D288) > Sort Key: h.folde= r_id_3 > -> Merge Join (= cost=3D501.20..603.47 rows=3D2730 width=3D288) > Merge Cond:= ("outer".id =3D "inner".folder_id_2) > -> Index S= can using pk_ent_folder on ent_folder f2 (cost=3D0.00..52.00 rows=3D1000 w= idth=3D32) > -> Sort (= cost=3D501.20..508.02 rows=3D2730 width=3D256) > Sort = Key: h.folder_id_2 > -> M= erge Join (cost=3D243.11..345.39 rows=3D2730 width=3D256) > = Merge Cond: ("outer".id =3D "inner".folder_id_1) > = -> Index Scan using pk_ent_folder on ent_folder f1 (cost=3D0.00..52.00 r= ows=3D1000 width=3D32) > = -> Sort (cost=3D243.11..249.94 rows=3D2730 width=3D224) > = Sort Key: h.folder_id_1 > = -> Seq Scan on comn_attr_hierarch h (cost=3D0.00..87.30 rows=3D273= 0 width=3D224) >=20 > -------------------------------------------------------------------------= ----------------------------------- >=20 > ---------------------------(end of broadcast)--------------------------- > TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match --=20 Martijn van Oosterhout http://svana.org/kleptog/ > "All that is needed for the forces of evil to triumph is for enough good > men to do nothing." - Edmond Burke > "The penalty good people pay for not being interested in politics is to be > governed by people worse than themselves." - Plato --KDt/GgjP6HVcx58l Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE/m5QMY5Twig3Ge+YRAlGvAJsFovy27VJ47nkS9kITZnY+BH+OmQCggdqe 3LGYq5Dy3W8yvIB+z5vc084= =gqtY -----END PGP SIGNATURE----- --KDt/GgjP6HVcx58l--