Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WNgY9-0001YX-NT for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Mar 2014 10:38:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WNgY8-0000Wr-GM for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Mar 2014 10:38:12 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WNgY7-0000Wk-EI for pgsql-bugs@postgresql.org; Wed, 12 Mar 2014 10:38:11 +0000 Received: from mail.wmaker.net ([94.23.134.230]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WNgXz-0005ad-Lz for pgsql-bugs@postgresql.org; Wed, 12 Mar 2014 10:38:10 +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 1WNgXx-0005c5-Kf for pgsql-bugs@postgresql.org; Wed, 12 Mar 2014 11:38:01 +0100 From: =?iso-8859-1?Q?Gr=E9gory_Giannoni?= Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Subject: LIMIT causes huge slow down Date: Wed, 12 Mar 2014 11:38:00 +0100 Message-Id: <30655A2C-145C-4DB1-8BF4-F5C3DF6772AB@wmaker.net> To: pgsql-bugs@postgresql.org Mime-Version: 1.0 (Apple Message framework v1283) X-Mailer: Apple Mail (2.1283) X-Pg-Spam-Score: -0.5 (/) 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 Hi people, I'm hitting a strange behavior with our postgres servers : In some cases, u= sing LIMIT causes slowest requests than without LIMIT : webzine=3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, = dimension, galerie_photo.titre, galerie_photo.dimension FROM galerie_photo = JOIN galerie_album USING(id_album) WHERE galerie_album.id_webzine=3D'18' AN= D id_flickr=3D0 AND id_picasa=3D0 ORDER BY id_photo DESC; = 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 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------- Sort (cost=3D37527.48..37542.25 rows=3D5909 width=3D37) (actual time=3D19= .742..21.066 rows=3D8247 loops=3D1) Sort Key: galerie_photo.id_photo Sort Method: quicksort Memory: 1186kB -> Nested Loop (cost=3D0.84..37157.32 rows=3D5909 width=3D37) (actual = time=3D0.044..14.104 rows=3D8247 loops=3D1) -> Index Scan using galerie_album_picasa_flickr_idw_pos_idx on ga= lerie_album (cost=3D0.42..447.58 rows=3D255 width=3D4) (actual time=3D0.03= 2..0.497 rows=3D256 loops=3D1) Index Cond: ((id_picasa =3D 0) AND (id_flickr =3D 0) AND (id= _webzine =3D 18)) -> Index Scan using galerie_photo_id_album_idx on galerie_photo = (cost=3D0.43..143.37 rows=3D59 width=3D37) (actual time=3D0.008..0.038 rows= =3D32 loops=3D256) Index Cond: (id_album =3D galerie_album.id_album) Total runtime: 22.003 ms (9 rows) Adding LIMIT 1 modifies the query plan and slow downs... webzine=3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, = dimension, galerie_photo.titre, galerie_photo.dimension FROM galerie_photo = JOIN galerie_album USING(id_album) WHERE galerie_album.id_webzine=3D18 AND = id_flickr=3D0 AND id_picasa=3D0 ORDER BY id_photo DESC LIMIT 1; = 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=3D0.84..1523.63 rows=3D1 width=3D37) (actual time=3D115.787..= 115.787 rows=3D1 loops=3D1) -> Nested Loop (cost=3D0.84..8998120.94 rows=3D5909 width=3D37) (actua= l time=3D115.787..115.787 rows=3D1 loops=3D1) Join Filter: (galerie_photo.id_album =3D galerie_album.id_album) Rows Removed by Join Filter: 374528 -> Index Scan Backward using galerie_photo_pkey on galerie_photo = (cost=3D0.43..3056353.78 rows=3D1553286 width=3D37) (actual time=3D0.018..= 3.151 rows=3D1464 loops=3D1) -> Materialize (cost=3D0.42..448.86 rows=3D255 width=3D4) (actua= l time=3D0.000..0.029 rows=3D256 loops=3D1464) -> Index Scan using galerie_album_picasa_flickr_idw_pos_idx= on galerie_album (cost=3D0.42..447.58 rows=3D255 width=3D4) (actual time= =3D0.032..0.458 rows=3D256 loops=3D1) Index Cond: ((id_picasa =3D 0) AND (id_flickr =3D 0) A= ND (id_webzine =3D 18)) Total runtime: 115.835 ms (9 rows) increasing the LIMIT parameter up to 8 don't change anything : webzine=3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, = dimension, galerie_photo.titre, galerie_photo.dimension FROM galerie_photo = JOIN galerie_album USING(id_album) WHERE galerie_album.id_webzine=3D18 AND = id_flickr=3D0 AND id_picasa=3D0 ORDER BY id_photo DESC LIMIT 8; = 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=3D0.84..12183.10 rows=3D8 width=3D37) (actual time=3D115.853.= .116.370 rows=3D8 loops=3D1) -> Nested Loop (cost=3D0.84..8998120.94 rows=3D5909 width=3D37) (actua= l time=3D115.852..116.369 rows=3D8 loops=3D1) Join Filter: (galerie_photo.id_album =3D galerie_album.id_album) Rows Removed by Join Filter: 376313 -> Index Scan Backward using galerie_photo_pkey on galerie_photo = (cost=3D0.43..3056353.78 rows=3D1553286 width=3D37) (actual time=3D0.019..= 3.072 rows=3D1471 loops=3D1) -> Materialize (cost=3D0.42..448.86 rows=3D255 width=3D4) (actua= l time=3D0.000..0.030 rows=3D256 loops=3D1471) -> Index Scan using galerie_album_picasa_flickr_idw_pos_idx= on galerie_album (cost=3D0.42..447.58 rows=3D255 width=3D4) (actual time= =3D0.032..0.525 rows=3D256 loops=3D1) Index Cond: ((id_picasa =3D 0) AND (id_flickr =3D 0) A= ND (id_webzine =3D 18)) Total runtime: 116.420 ms (9 rows) But passing LIMIT 9 : webzine=3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, = dimension, galerie_photo.titre, galerie_photo.dimension FROM galerie_photo = JOIN galerie_album USING(id_album) WHERE galerie_album.id_webzine=3D18 AND = id_flickr=3D0 AND id_picasa=3D0 ORDER BY id_photo DESC LIMIT 9; = 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=3D0.84..13705.88 rows=3D9 width=3D37) (actual time=3D118.209.= .6905.990 rows=3D9 loops=3D1) -> Nested Loop (cost=3D0.84..8998120.94 rows=3D5909 width=3D37) (actua= l time=3D118.207..6905.985 rows=3D9 loops=3D1) Join Filter: (galerie_photo.id_album =3D galerie_album.id_album) Rows Removed by Join Filter: 22882297 -> Index Scan Backward using galerie_photo_pkey on galerie_photo = (cost=3D0.43..3056353.78 rows=3D1553286 width=3D37) (actual time=3D0.019..= 191.401 rows=3D89385 loops=3D1) -> Materialize (cost=3D0.42..448.86 rows=3D255 width=3D4) (actua= l time=3D0.000..0.029 rows=3D256 loops=3D89385) -> Index Scan using galerie_album_picasa_flickr_idw_pos_idx= on galerie_album (cost=3D0.42..447.58 rows=3D255 width=3D4) (actual time= =3D0.033..0.464 rows=3D256 loops=3D1) Index Cond: ((id_picasa =3D 0) AND (id_flickr =3D 0) A= ND (id_webzine =3D 18)) Total runtime: 6906.050 ms (9 rows) Reducing the random page cost to 2.5 (I'm on SSD drives) can improve this b= ehavior : webzine=3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, = dimension, galerie_photo.titre, galerie_photo.dimension FROM galerie_photo = JOIN galerie_album USING(id_album) WHERE galerie_album.id_webzine=3D'18' AN= D id_flickr=3D0 AND id_picasa=3D0 ORDER BY id_photo DESC ; = 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 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------- Sort (cost=3D23807.38..23822.15 rows=3D5909 width=3D37) (actual time=3D19= .163..20.660 rows=3D8247 loops=3D1) Sort Key: galerie_photo.id_photo Sort Method: quicksort Memory: 1186kB -> Nested Loop (cost=3D0.84..23437.22 rows=3D5909 width=3D37) (actual = time=3D0.040..13.553 rows=3D8247 loops=3D1) -> Index Scan using galerie_album_picasa_flickr_idw_pos_idx on ga= lerie_album (cost=3D0.42..283.50 rows=3D255 width=3D4) (actual time=3D0.03= 0..0.433 rows=3D256 loops=3D1) Index Cond: ((id_picasa =3D 0) AND (id_flickr =3D 0) AND (id= _webzine =3D 18)) -> Index Scan using galerie_photo_id_album_idx on galerie_photo = (cost=3D0.43..90.21 rows=3D59 width=3D37) (actual time=3D0.008..0.037 rows= =3D32 loops=3D256) Index Cond: (id_album =3D galerie_album.id_album) Total runtime: 21.554 ms (9 rows) webzine=3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, = dimension, galerie_photo.titre, galerie_photo.dimension FROM galerie_photo = JOIN galerie_album USING(id_album) WHERE galerie_album.id_webzine=3D'18' AN= D id_flickr=3D0 AND id_picasa=3D0 ORDER BY id_photo DESC LIMIT 1; = 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 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ----------------- Limit (cost=3D0.72..1284.78 rows=3D1 width=3D37) (actual time=3D6.356..6.= 356 rows=3D1 loops=3D1) -> Nested Loop (cost=3D0.72..7587529.65 rows=3D5909 width=3D37) (actua= l time=3D6.356..6.356 rows=3D1 loops=3D1) -> Index Scan Backward using galerie_photo_pkey on galerie_photo = (cost=3D0.43..1925267.77 rows=3D1553286 width=3D37) (actual time=3D0.026..= 2.123 rows=3D1464 loops=3D1) -> Index Scan using galerie_album_pkey on galerie_album (cost=3D= 0.29..3.64 rows=3D1 width=3D4) (actual time=3D0.002..0.002 rows=3D0 loops= =3D1464) Index Cond: (id_album =3D galerie_photo.id_album) Filter: ((id_webzine =3D 18) AND (id_flickr =3D 0) AND (id_p= icasa =3D 0)) Rows Removed by Filter: 1 Total runtime: 6.402 ms (8 rows) webzine=3D> explain analyze SELECT id_photo, id_album, galerie_photo.etat, = dimension, galerie_photo.titre, galerie_photo.dimension FROM galerie_photo = JOIN galerie_album USING(id_album) WHERE galerie_album.id_webzine=3D'18' AN= D id_flickr=3D0 AND id_picasa=3D0 ORDER BY id_photo DESC LIMIT 9; = 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 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= -------------------- Limit (cost=3D0.72..11557.29 rows=3D9 width=3D37) (actual time=3D6.286..3= 56.399 rows=3D9 loops=3D1) -> Nested Loop (cost=3D0.72..7587529.65 rows=3D5909 width=3D37) (actua= l time=3D6.286..356.395 rows=3D9 loops=3D1) -> Index Scan Backward using galerie_photo_pkey on galerie_photo = (cost=3D0.43..1925267.77 rows=3D1553286 width=3D37) (actual time=3D0.020..= 103.572 rows=3D89385 loops=3D1) -> Index Scan using galerie_album_pkey on galerie_album (cost=3D= 0.29..3.64 rows=3D1 width=3D4) (actual time=3D0.002..0.002 rows=3D0 loops= =3D89385) Index Cond: (id_album =3D galerie_photo.id_album) Filter: ((id_webzine =3D 18) AND (id_flickr =3D 0) AND (id_p= icasa =3D 0)) Rows Removed by Filter: 1 Total runtime: 356.452 ms (8 rows) The tables have been vacuumed, analyzed and reindexed without any change. r= esults are reproductibles. galerie_album has 67033 rows (256 of them have id_webzine=3D18) galerie_photo has 1494738 rows Any idea or workaround ? 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