Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TZLag-0004Y6-Hn for pgsql-performance@postgresql.org; Fri, 16 Nov 2012 13:04:14 +0000 Received: from blu0-omc4-s32.blu0.hotmail.com ([65.55.111.171]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TZLab-00018K-Nu for pgsql-performance@postgresql.org; Fri, 16 Nov 2012 13:04:13 +0000 Received: from BLU002-W71 ([65.55.111.136]) by blu0-omc4-s32.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 16 Nov 2012 05:04:05 -0800 Message-ID: Content-Type: multipart/alternative; boundary="_f1dfc22a-eba8-4778-89f4-f5596b7da5d5_" X-Originating-IP: [109.238.231.210] From: David Popiashvili To: "pgsql-performance@postgresql.org" Subject: Re: PostgreSQL strange query plan for my query Date: Fri, 16 Nov 2012 17:04:05 +0400 Importance: Normal In-Reply-To: References: , MIME-Version: 1.0 X-OriginalArrivalTime: 16 Nov 2012 13:04:05.0610 (UTC) FILETIME=[DB89E4A0:01CDC3FA] X-Pg-Spam-Score: -1.8 (-) X-Archive-Number: 201211/182 X-Sequence-Number: 48548 --_f1dfc22a-eba8-4778-89f4-f5596b7da5d5_ Content-Type: text/plain; charset="windows-1251" Content-Transfer-Encoding: quoted-printable All right=2C after some discussion on StackOverflow=2C we found out that in= correct query plan is generated due to the fact that there is a LIMIT keywo= rd in the query. I guess Postgresql expects to find appropriate rows faster= and that's why it generates a seq scan on the table. If I remove LIMIT 100= 0 everything is executed in several milliseconds and query plan looks like = this: Hash Join (cost=3D2662004.85..14948213.44 rows=3D22661658 width=3D138) (ac= tual time=3D0.105..0.105 rows=3D0 loops=3D1) Hash Cond: (p."PaymentOrderId= " =3D po."Id") -> Seq Scan on "Payments" p (cost=3D0.00..5724570.00 rows= =3D350000000 width=3D18) (actual time=3D0.018..0.018 rows=3D1 loops=3D1) -= > Hash (cost=3D2583365.85..2583365.85 rows=3D2614480 width=3D120) (actual= time=3D0.046..0.046 rows=3D0 loops=3D1) Buckets: 8192 Batches: 64 = Memory Usage: 0kB -> Hash Join (cost=3D904687.05..2583365.85 rows= =3D2614480 width=3D120) (actual time=3D0.046..0.046 rows=3D0 loops=3D1) = Hash Cond: (po."UserId" =3D u."Id") -> Seq Scan on = "PaymentOrders" po (cost=3D0.00..654767.00 rows=3D40000000 width=3D24) (ac= tual time=3D0.003..0.003 rows=3D1 loops=3D1) -> Hash (cost= =3D850909.04..850909.04 rows=3D1980881 width=3D96) (actual time=3D0.016..0.= 016 rows=3D0 loops=3D1) Buckets: 8192 Batches: 32 Memo= ry Usage: 0kB -> Hash Join (cost=3D1.20..850909.04 row= s=3D1980881 width=3D96) (actual time=3D0.016..0.016 rows=3D0 loops=3D1) = Hash Cond: (u."RoleId" =3D r."Id") = -> Seq Scan on "Users" u (cost=3D0.00..718598.20 rows=3D30000220 w= idth=3D80) (actual time=3D0.002..0.002 rows=3D1 loops=3D1) = -> Hash (cost=3D1.19..1.19 rows=3D1 width=3D16) (actual time=3D0= .009..0.009 rows=3D0 loops=3D1) Buckets: 102= 4 Batches: 1 Memory Usage: 0kB -> Seq Sca= n on "Roles" r (cost=3D0.00..1.19 rows=3D1 width=3D16) (actual time=3D0.00= 9..0.009 rows=3D0 loops=3D1) Filter: (= ("Name")::text =3D 'Moses2333'::text) = Rows Removed by Filter: 15Total runtime: 0.209 ms According to Erwin Brandstetter I also tried pushing the query in a subquer= y and applying LIMIT there: SELECT *FROM ( SELECT * FROM "Roles" AS r JOIN "Users"= AS u ON u."RoleId" =3D r."Id" JOIN "PaymentOrders" AS po ON p= o."UserId" =3D u."Id" JOIN "Payments" AS p ON p."PaymentOrderId" = =3D po."Id" WHERE r."Name" =3D 'Moses' ) xLIMIT 1000=3B but this solution also generates incorrect query plan. Any idea how to solv= e this query without omitting LIMIT keyword?Thanks > Subject: RE: [PERFORM] PostgreSQL strange query plan for my query > Date: Fri=2C 16 Nov 2012 13:55:41 +0100 > From: laurenz.albe@wien.gv.at > To: dato0011@hotmail.com=3B pgsql-performance@postgresql.org >=20 > David Popiashvili wrote: > > I have database with few hundred millions of rows. I'm running the > following query: > >=20 > > select * from "Payments" as p > > inner join "PaymentOrders" as po > > on po."Id" =3D p."PaymentOrderId" > > inner join "Users" as u > > On u."Id" =3D po."UserId" > > INNER JOIN "Roles" as r > > on u."RoleId" =3D r."Id" > > Where r."Name" =3D 'Moses' > > LIMIT 1000 > > When the where clause finds a match in database=2C I get the result in > several milliseconds=2C but if I > > modify the query and specify a non-existent r."Name" in where clause=2C > it takes too much time to > > complete. I guess that PostgreSQL is doing a sequential scan on the > Payments table (which contains the > > most rows)=2C comparing each row one by one. > > Isn't postgresql smart enough to check first if Roles table contains > any row with Name 'Moses'? > >=20 > > Roles table contains only 15 row=2C while Payments contains ~350 > million. > >=20 > > I'm running PostgreSQL 9.2.1. >=20 > > Here'e explain analyse results: http://explain.depesz.com/s/7e7 >=20 > Can you also show the plan for the good case? >=20 > Yours=2C > Laurenz Albe = --_f1dfc22a-eba8-4778-89f4-f5596b7da5d5_ Content-Type: text/html; charset="windows-1251" Content-Transfer-Encoding: quoted-printable
All right=2C after some discussion on StackOverflow=2C we found out that incorrect query plan is g= enerated due to the fact that there is a LIMIT keyword in the query. I gues= s Postgresql expects to find appropriate rows faster and that's why it gene= rates a seq scan on the table. If I remove LIMIT 1000 everything is execute= d in several milliseconds and query plan looks like this:

H= ash Join  =3B(cost=3D2662004.85..14948213.44 rows=3D22661658 width=3D13= 8) (actual time=3D0.105..0.105 rows=3D0 loops=3D1)
 =3B Hash Cond: (p= ."PaymentOrderId" =3D po."Id")
 =3B ->=3B  =3BSeq Scan on "Paym= ents" p  =3B(cost=3D0.00..5724570.00 rows=3D350000000 width=3D18) (actu= al time=3D0.018..0.018 rows=3D1 loops=3D1)
 =3B ->=3B  =3BHash =  =3B(cost=3D2583365.85..2583365.85 rows=3D2614480 width=3D120) (actual = time=3D0.046..0.046 rows=3D0 loops=3D1)
 =3B  =3B  =3B  = =3B Buckets: 8192  =3BBatches: 64  =3BMemory Usage: 0kB
 =3B =  =3B  =3B  =3B ->=3B  =3BHash Join  =3B(cost=3D904687= .05..2583365.85 rows=3D2614480 width=3D120) (actual time=3D0.046..0.046 row= s=3D0 loops=3D1)
 =3B  =3B  =3B  =3B  =3B  =3B &n= bsp=3B Hash Cond: (po."UserId" =3D u."Id")
 =3B  =3B  =3B &nb= sp=3B  =3B  =3B  =3B ->=3B  =3BSeq Scan on "PaymentOrders= " po  =3B(cost=3D0.00..654767.00 rows=3D40000000 width=3D24) (actual ti= me=3D0.003..0.003 rows=3D1 loops=3D1)
 =3B  =3B  =3B  =3B=  =3B  =3B  =3B ->=3B  =3BHash  =3B(cost=3D850909.04.= .850909.04 rows=3D1980881 width=3D96) (actual time=3D0.016..0.016 rows=3D0 = loops=3D1)
 =3B  =3B  =3B  =3B  =3B  =3B  =3B=  =3B  =3B  =3B Buckets: 8192  =3BBatches: 32  =3BMemor= y Usage: 0kB
 =3B  =3B  =3B  =3B  =3B  =3B  = =3B  =3B  =3B  =3B ->=3B  =3BHash Join  =3B(cost=3D1.= 20..850909.04 rows=3D1980881 width=3D96) (actual time=3D0.016..0.016 rows= =3D0 loops=3D1)
 =3B  =3B  =3B  =3B  =3B  =3B &nb= sp=3B  =3B  =3B  =3B  =3B  =3B  =3B Hash Cond: (u."= RoleId" =3D r."Id")
 =3B  =3B  =3B  =3B  =3B  = =3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B ->=3B =  =3BSeq Scan on "Users" u  =3B(cost=3D0.00..718598.20 rows=3D300002= 20 width=3D80) (actual time=3D0.002..0.002 rows=3D1 loops=3D1)
 =3B &= nbsp=3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B &nbs= p=3B  =3B  =3B  =3B ->=3B  =3BHash  =3B(cost=3D1.19..= 1.19 rows=3D1 width=3D16) (actual time=3D0.009..0.009 rows=3D0 loops=3D1)
 =3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B &n= bsp=3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B Bucke= ts: 1024  =3BBatches: 1  =3BMemory Usage: 0kB
 =3B  =3B &= nbsp=3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B &nbs= p=3B  =3B  =3B  =3B  =3B  =3B ->=3B  =3BSeq Scan = on "Roles" r  =3B(cost=3D0.00..1.19 rows=3D1 width=3D16) (actual time= =3D0.009..0.009 rows=3D0 loops=3D1)
 =3B  =3B  =3B  =3B &= nbsp=3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B &nbs= p=3B  =3B  =3B  =3B  =3B  =3B  =3B Filter: (("Name"= )::text =3D 'Moses2333'::text)
 =3B  =3B  =3B  =3B  = =3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B  =3B=  =3B  =3B  =3B  =3B  =3B  =3B Rows Removed by Filt= er: 15
Total runtime: 0.209 ms

According to =3BErwin B= randstetter I also = tried pushing the query in a subquery and applying LIMIT there:

SELECT *
FROM  =3B(
 =3B  =3BSELECT *
 =3B  =3BFROM  =3B "Roles"=  =3B  =3B  =3B  =3B AS r  =3B
 =3B  = =3BJOIN  =3B "Users"  =3B  =3B  =3B  =3B AS u  =3BO= N u."RoleId" =3D r."Id"
 =3B  =3BJOIN  =3B "PaymentOr= ders" AS po ON po."UserId" =3D u."Id"
 =3B  =3BJOIN  = =3B "Payments"  =3B  =3B  =3BAS p  =3BON p."PaymentOrderId"= =3D po."Id"
 =3B  =3BWHERE  =3Br."Name" =3D 'Moses'<= /div>
 =3B ) x
LIMIT  =3B1000=3B
<= /div>

but this solution also genera= tes incorrect query plan. Any idea how to solve this query without =3Bo= mitting =3BLIMIT keyword?
Thanks

>=3B Subject: RE: [PERFORM] PostgreSQL strange query plan = for my query
>=3B Date: Fri=2C 16 Nov 2012 13:55:41 +0100
>=3B Fr= om: laurenz.albe@wien.gv.at
>=3B To: dato0011@hotmail.com=3B pgsql-per= formance@postgresql.org
>=3B
>=3B David Popiashvili wrote:
&g= t=3B >=3B I have database with few hundred millions of rows. I'm running = the
>=3B following query:
>=3B >=3B
>=3B >=3B select * = from "Payments" as p
>=3B >=3B inner join "PaymentOrders" as po
&= gt=3B >=3B on po."Id" =3D p."PaymentOrderId"
>=3B >=3B inner join = "Users" as u
>=3B >=3B On u."Id" =3D po."UserId"
>=3B >=3B IN= NER JOIN "Roles" as r
>=3B >=3B on u."RoleId" =3D r."Id"
>=3B &= gt=3B Where r."Name" =3D 'Moses'
>=3B >=3B LIMIT 1000
>=3B >= =3B When the where clause finds a match in database=2C I get the result in<= br>>=3B several milliseconds=2C but if I
>=3B >=3B modify the quer= y and specify a non-existent r."Name" in where clause=2C
>=3B it takes= too much time to
>=3B >=3B complete. I guess that PostgreSQL is doi= ng a sequential scan on the
>=3B Payments table (which contains the>=3B >=3B most rows)=2C comparing each row one by one.
>=3B >= =3B Isn't postgresql smart enough to check first if Roles table contains>=3B any row with Name 'Moses'?
>=3B >=3B
>=3B >=3B Roles= table contains only 15 row=2C while Payments contains ~350
>=3B milli= on.
>=3B >=3B
>=3B >=3B I'm running PostgreSQL 9.2.1.
>= =3B
>=3B >=3B Here'e explain analyse results: http://explain.depesz= .com/s/7e7
>=3B
>=3B Can you also show the plan for the good cas= e?
>=3B
>=3B Yours=2C
>=3B Laurenz Albe
=
= --_f1dfc22a-eba8-4778-89f4-f5596b7da5d5_--