Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 6C6C763269C for ; Sun, 25 Apr 2010 15:23:01 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 67913-07 for ; Sun, 25 Apr 2010 18:22:39 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-gx0-f221.google.com (mail-gx0-f221.google.com [209.85.217.221]) by mail.postgresql.org (Postfix) with ESMTP id 902106323AD for ; Sun, 25 Apr 2010 15:22:50 -0300 (ADT) Received: by gxk21 with SMTP id 21so758964gxk.14 for ; Sun, 25 Apr 2010 11:22:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:from:date :message-id:subject:to:content-type; bh=2tgIo7Iqkv3zJHZ1u49u7RVkuXnQxpHQHsWEQDcRfII=; b=C74RZSRAPi2U9X45UKAim5j0SmGrDPwNH7jIzTD/Ac/ql4jr6MnLIylyn02g3A//C7 F+sbpn0t10uJf2PGCyJOmoHpWEeNYNrg327YbOhaVl+8y/qZHNvvtiJJ7keKDfyAL3Fx ORy4GRSfys5WmXeCi5pnUMm4W8dI6tpRDe10s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; b=jdFfnbYojEheyDVSQdnHk9g3f+daWhKyPc7osdkPQvn0he1KcFHbBjXdN6cSC7lCiE hxWC/4n6hChcjbU/3DfVZbeEvAj2wyjL8BV2pyHVp16O0ci2glYi4Q2NIEzNkyVp5XvE GGv+lyiKLYpn4unEu0gogyxAbcmBTO122sTUY= Received: by 10.101.131.15 with SMTP id i15mr3682837ann.49.1272219769224; Sun, 25 Apr 2010 11:22:49 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.134.3 with HTTP; Sun, 25 Apr 2010 11:22:29 -0700 (PDT) From: =?KOI8-R?B?68/Sz9TLz9cg4czFy9PBzsTS?= Date: Sun, 25 Apr 2010 22:22:29 +0400 Message-ID: Subject: Planner issue on sorting joining of two tables with limit To: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=001636c924216bc103048513bfd6 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.801 tagged_above=-10 required=5 tests=BAYES_50=0.8, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201004/317 X-Sequence-Number: 38567 --001636c924216bc103048513bfd6 Content-Type: text/plain; charset=ISO-8859-1 Hello, everybody! I'm using PostgreSQL 8.4.3, compiled by Visual C++ build 1400, 32-bit on Windows XP SP3. I use following data model for issue reproducing. CREATE TABLE test1 ( id integer NOT NULL, "value" double precision, CONSTRAINT test1_pkey PRIMARY KEY (id) ); CREATE INDEX test1_value_idx ON test1(value); CREATE TABLE test2 ( id integer NOT NULL, id1 integer NOT NULL REFERENCES test2 (id), "value" double precision, CONSTRAINT test2_pkey PRIMARY KEY (id) ); CREATE INDEX test2_id1_value_idx ON test2(id1, value); Following statements generate 200 rows of test data into test1 table and 1000000 rows of test data into test2 table. INSERT INTO test1 (id, value) (SELECT x, random() from generate_series(1,200) x); INSERT INTO test2 (id, id1, value) (SELECT x, (random()*200)::int + 1, random() from generate_series(1,1000000) x); The following statements return top 10 rows from joining of two tables ordered by table1.value and table2.value. The SELECT t1.value AS value1, t2.value AS value2 FROM test1 t1 JOIN test2 t2 ON t2.id1 = t1.id ORDER BY t1.value, t2.value LIMIT 10 value1 | value2 ---------------------+--------------------- 0.00562104489654303 | 0.00039379671216011 0.00562104489654303 | 0.000658359378576279 0.00562104489654303 | 0.000668979249894619 0.00562104489654303 | 0.000768951140344143 0.00562104489654303 | 0.00121330376714468 0.00562104489654303 | 0.00122168939560652 0.00562104489654303 | 0.00124016962945461 0.00562104489654303 | 0.00134057039394975 0.00562104489654303 | 0.00169069319963455 0.00562104489654303 | 0.00171623658388853 (10 rows) The statement plan doesn't use indexes. So the statement it is slow. Limit (cost=50614.88..50614.91 rows=10 width=16) (actual time=8388.331..8388.382 rows=10 loops=1) -> Sort (cost=50614.88..53102.45 rows=995025 width=16) (actual time=8388.324..8388.340 rows=10 loops=1) Sort Key: t1.value, t2.value Sort Method: top-N heapsort Memory: 17kB -> Hash Join (cost=6.50..29112.75 rows=995025 width=16) (actual time=0.982..6290.516 rows=997461 loops=1) Hash Cond: (t2.id1 = t1.id) -> Seq Scan on test2 t2 (cost=0.00..15406.00 rows=1000000 width=12) (actualtime=0.088..2047.910 rows=1000000 loops=1) -> Hash (cost=4.00..4.00 rows=200 width=12) (actual time=0.870..0.870 rows=200 loops=1) -> Seq Scan on test1 t1 (cost=0.00..4.00 rows=200 width=12) (actual time=0.010..0.428 rows=200 loops=1) Total runtime: 8388.473 ms I can remove ordering by test2.value. SELECT t1.value AS value1, t2.value AS value2 FROM test1 t1 JOIN test2 t2 ON t2.id1 = t1.id ORDER BY t1.value LIMIT 10 Then the result is the same. value1 | value2 ---------------------+--------------------- 0.00562104489654303 | 0.00039379671216011 0.00562104489654303 | 0.000658359378576279 0.00562104489654303 | 0.000668979249894619 0.00562104489654303 | 0.000768951140344143 0.00562104489654303 | 0.00121330376714468 0.00562104489654303 | 0.00122168939560652 0.00562104489654303 | 0.00124016962945461 0.00562104489654303 | 0.00134057039394975 0.00562104489654303 | 0.00169069319963455 0.00562104489654303 | 0.00171623658388853 (10 rows) The statement plan uses indexes and statement runs fast. This plan is exactly what I need. Limit (cost=0.00..0.62 rows=10 width=16) (actual time=0.049..0.148 rows=10 loops=1) -> Nested Loop (cost=0.00..62109.86 rows=995025 width=16) (actual time=0.044..0.107 rows=10 loops=1) -> Index Scan using test1_value_idx on test1 t1 (cost=0.00..19.19 rows=200 width=12) (actual time=0.017..0.017 rows=1 loops=1) -> Index Scan using test2_id1_value_idx on test2 t2 (cost=0.00..248.27 rows=4975 width=12) (actual time=0.013..0.042 rows=10 loops=1) Index Cond: (t2.id1 = t1.id) Total runtime: 0.224 ms So PostgreSQL planner can produce the plan I need but it doesn't produce this plan when I specify particular second ordering column. So is there any way to make planner produce desired plan when particular second ordering column is specified? With best regards, Korotkov Alexander. --001636c924216bc103048513bfd6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello, everybody!

I'm using PostgreSQL 8.4.3, compiled by Visual= C++ build 1400, 32-bit on Windows XP SP3.
I use following data model f= or issue reproducing.

CREATE TABLE test1
(
=A0 id integer NOT NULL,
=A0 "value" double precision,
=A0 = CONSTRAINT test1_pkey PRIMARY KEY (id)
);

CREATE INDEX test1_valu= e_idx ON test1(value);

CREATE TABLE test2
(
=A0 id integer NOT= NULL,
=A0 id1 integer NOT NULL REFERENCES test2 (id),
=A0 "value" do= uble precision,
=A0 CONSTRAINT test2_pkey PRIMARY KEY (id)
);

= CREATE INDEX test2_id1_value_idx ON test2(id1, value);

Following sta= tements generate 200 rows of test data into test1 table and 1000000 rows of= test data into test2 table.

INSERT INTO test1 (id, value) (SELECT x, random() from generate_series(= 1,200) x);

INSERT INTO test2 (id, id1, value) (SELECT x, (random()*2= 00)::int + 1, random() from generate_series(1,1000000) x);

The follo= wing statements return top 10 rows from joining of two tables ordered by ta= ble1.value and table2.value. The

SELECT t1.value AS value1, t2.value AS value2 FROM test1 t1 JOIN test2 = t2 ON t2.id1 =3D t1.id ORDER BY t1.value, t2.v= alue
LIMIT 10

=A0=A0=A0=A0=A0=A0 value1=A0=A0=A0=A0=A0=A0=A0 |= =A0=A0=A0=A0=A0=A0=A0 value2
---------------------+---------------------=
=A00.00562104489654303 |=A0 0.00039379671216011
=A00.00562104489654303 |= 0.000658359378576279
=A00.00562104489654303 | 0.000668979249894619
= =A00.00562104489654303 | 0.000768951140344143
=A00.00562104489654303 |= =A0 0.00121330376714468
=A00.00562104489654303 |=A0 0.00122168939560652
=A00.00562104489654303 |= =A0 0.00124016962945461
=A00.00562104489654303 |=A0 0.00134057039394975<= br>=A00.00562104489654303 |=A0 0.00169069319963455
=A00.0056210448965430= 3 |=A0 0.00171623658388853
(10 rows)

The statement plan doesn't use indexes. So the stateme= nt it is slow.

Limit=A0 (cost=3D50614.88..50614.91 rows=3D10 width= =3D16) (actual time=3D8388.331..8388.382 rows=3D10 loops=3D1)
=A0 ->= =A0 Sort=A0 (cost=3D50614.88..53102.45 rows=3D995025 width=3D16) (actual ti= me=3D8388.324..8388.340 rows=3D10 loops=3D1)
=A0=A0=A0=A0=A0=A0=A0 Sort Key: t1.value, t2.value
=A0=A0=A0=A0=A0=A0=A0= Sort Method:=A0 top-N heapsort=A0 Memory: 17kB
=A0=A0=A0=A0=A0=A0=A0 -&= gt;=A0 Hash Join=A0 (cost=3D6.50..29112.75 rows=3D995025 width=3D16) (actua= l time=3D0.982..6290.516 rows=3D997461 loops=3D1)
=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 Hash Cond: (t2.id1 =3D t1.id)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 ->=A0 Seq Scan on test2 t2=A0 (c= ost=3D0.00..15406.00 rows=3D1000000 width=3D12) (actualtime=3D0.088..2047.9= 10 rows=3D1000000 loops=3D1)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 -&g= t;=A0 Hash=A0 (cost=3D4.00..4.00 rows=3D200 width=3D12) (actual time=3D0.87= 0..0.870 rows=3D200 loops=3D1)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 ->=A0 Seq Scan= on test1 t1=A0 (cost=3D0.00..4.00 rows=3D200 width=3D12) (actual time=3D0.= 010..0.428 rows=3D200 loops=3D1)
Total runtime: 8388.473 ms

I can= remove ordering by test2.value.

SELECT t1.value AS value1, t2.value= AS value2 FROM test1 t1 JOIN test2 t2 ON t2.id1 =3D
t1.id ORDER BY t1.value LIMIT 10

Then the result is the same.

=A0=A0=A0=A0=A0=A0 value1=A0=A0=A0= =A0=A0=A0=A0 |=A0=A0=A0=A0=A0=A0=A0 value2
---------------------+-------= --------------
=A00.00562104489654303 |=A0 0.00039379671216011
=A00.0= 0562104489654303 | 0.000658359378576279
=A00.00562104489654303 | 0.000668979249894619
=A00.00562104489654303 | 0= .000768951140344143
=A00.00562104489654303 |=A0 0.00121330376714468
= =A00.00562104489654303 |=A0 0.00122168939560652
=A00.00562104489654303 |= =A0 0.00124016962945461
=A00.00562104489654303 |=A0 0.00134057039394975
=A00.00562104489654303 |= =A0 0.00169069319963455
=A00.00562104489654303 |=A0 0.00171623658388853<= br>(10 rows)

The statement plan uses indexes and statement runs fast= . This plan is exactly what I need.

Limit=A0 (cost=3D0.00..0.62 rows=3D10 width=3D16) (actual time=3D0.049.= .0.148 rows=3D10 loops=3D1)
=A0 ->=A0 Nested Loop=A0 (cost=3D0.00..62= 109.86 rows=3D995025 width=3D16) (actual time=3D0.044..0.107 rows=3D10 loop= s=3D1)
=A0=A0=A0=A0=A0=A0=A0 ->=A0 Index Scan using test1_value_idx o= n test1 t1=A0 (cost=3D0.00..19.19 rows=3D200 width=3D12) (actual time=3D0.0= 17..0.017 rows=3D1 loops=3D1)
=A0=A0=A0=A0=A0=A0=A0 ->=A0 Index Scan using test2_id1_value_idx on test= 2 t2=A0 (cost=3D0.00..248.27 rows=3D4975 width=3D12) (actual time=3D0.013..= 0.042 rows=3D10 loops=3D1)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Index= Cond: (t2.id1 =3D t1.id)
Total runtime: 0.224 ms

So PostgreSQL planner can produce the plan I= need but it doesn't produce this plan when I specify particular second= ordering column. So is there any way to make planner produce desired plan = when particular second ordering column is specified?

With best regards,
Korotkov Alexander.
--001636c924216bc103048513bfd6--