Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WO94n-0000AW-RN for pgsql-bugs@arkaria.postgresql.org; Thu, 13 Mar 2014 17:05:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WO94n-0000jj-BK for pgsql-bugs@arkaria.postgresql.org; Thu, 13 Mar 2014 17:05:49 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WO94m-0000jd-Jy for pgsql-bugs@postgresql.org; Thu, 13 Mar 2014 17:05:48 +0000 Received: from mail.wmaker.net ([94.23.134.230]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WO94i-0004qT-D2 for pgsql-bugs@postgresql.org; Thu, 13 Mar 2014 17:05:47 +0000 Received: from lmontsouris-656-01-03-42.w80-12.abo.wanadoo.fr ([80.12.88.42] helo=[10.0.0.17]) by mail.wmaker.net with esmtpa (Exim 4.72) (envelope-from ) id 1WO94e-00021R-8Y for pgsql-bugs@postgresql.org; Thu, 13 Mar 2014 18:05:40 +0100 Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Apple Message framework v1283) Subject: Re: LIMIT causes huge slow down From: =?iso-8859-1?Q?Gr=E9gory_Giannoni?= In-Reply-To: <1394642071713-5795717.post@n5.nabble.com> Date: Thu, 13 Mar 2014 18:05:38 +0100 Content-Transfer-Encoding: quoted-printable Message-Id: <3E89FAAB-C4EB-45F0-B7C9-64DA24A73899@wmaker.net> References: <30655A2C-145C-4DB1-8BF4-F5C3DF6772AB@wmaker.net> <53206541.8060200@comcast.net> <1394642071713-5795717.post@n5.nabble.com> To: pgsql-bugs@postgresql.org X-Mailer: Apple Mail (2.1283) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Le 12 mars 2014 =E0 17:34, David Johnston a =E9crit : > Harry Rossignol wrote >> hello >>=20 >> i am just a lowly application developer, but i always include my 'where'= =20 >> fields in my order by specification. >> I.E. ORDER BY id_webzine,id_flickr,id_picasa,id_photo rather then just=20 >> id_photo > [...] > WRT to question posed: probably the easiest workaround is to move the > unlimited query to a WITH clause and then apply the limit separately. >=20 > You should indicate what version of PostgreSQL you are using. >=20 > Sorry I'm not much help on the how and why of the actual plan choices her= e.=20 > The sorting is constant but since the limit and the where clause target > different tables a full evaluation is needed to determine a solution so > picking individual rows, which is what I see happening, doesn't by you > anything. >=20 > But, for all I know your using an old version and this undesirable behavi= or > has already been found and fixed. Hi, thank you for your answer. I'm using PostgreSQL 9.3.3. Anyway, I tried Harry's idea.. and the result is for me very surprising : i= t worked. =3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, galerie= _photo.titre, galerie_photo.dimension FROM galerie_photo JOIN galerie_album= USING(id_album) WHERE galerie_album.id_webzine=3D'18' AND id_flickr=3D0 AN= D id_picasa=3D0 ORDER BY id_photo DESC, id_album DESC LIMIT 10; = QUERY PLAN=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------------- Limit (cost=3D20388.78..20388.80 rows=3D10 width=3D37) (actual time=3D31.4= 02..31.406 rows=3D10 loops=3D1) -> Sort (cost=3D20388.78..20401.30 rows=3D5010 width=3D37) (actual time= =3D31.400..31.401 rows=3D10 loops=3D1) Sort Key: galerie_photo.id_photo, galerie_photo.id_album Sort Method: top-N heapsort Memory: 25kB -> Nested Loop (cost=3D0.84..20280.51 rows=3D5010 width=3D37) (ac= tual time=3D0.087..27.920 rows=3D8247 loops=3D1) -> Index Scan using galerie_album_picasa_flickr_idw_pos_idx = on galerie_album (cost=3D0.42..251.20 rows=3D225 width=3D4) (actual time= =3D0.032..0.529 rows=3D256 loops=3D1) Index Cond: ((id_picasa =3D 0) AND (id_flickr =3D 0) AN= D (id_webzine =3D 18)) -> Index Scan using galerie_photo_id_album_idx on galerie_ph= oto (cost=3D0.43..88.45 rows=3D57 width=3D37) (actual time=3D0.019..0.092 = rows=3D32 loops=3D256) Index Cond: (id_album =3D galerie_album.id_album) Total runtime: 31.465 ms (10 rows) Adding a second order clause make the query planner choose the same plan th= at without the LIMIT, and perfs are OK. It is against my thoughts that adding sort element necessary slow down the = process... but it work (for this particular case). Best regards, Gr=E9gory Giannoni. --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs