Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id D070C9FA691 for ; Fri, 21 Sep 2007 12:38:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 76433-09 for ; Fri, 21 Sep 2007 12:38:02 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from fmx23.freemail.hu (fmx23.freemail.hu [195.228.245.73]) by postgresql.org (Postfix) with SMTP id CD9A49F9A77 for ; Fri, 21 Sep 2007 12:38:01 -0300 (ADT) Received: (qmail 92455 invoked from network); 21 Sep 2007 17:36:25 +0200 Received: from fm03.freemail.hu (195.228.245.103) by fmx23.freemail.hu with SMTP; 21 Sep 2007 17:36:25 +0200 Received: (qmail 87027 invoked by uid 1909201); 21 Sep 2007 17:36:25 +0200 Date: Fri, 21 Sep 2007 17:36:25 +0200 (CEST) From: Denes Daniel Subject: Query planner unaware of possibly best plan To: pgsql-performance@postgresql.org Message-ID: X-Originating-IP: [193.226.224.189] X-HTTP-User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-2 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/367 X-Sequence-Number: 26912 Hi, I think the query planner is unaware of the possibly best plan in some situations. See the test case below: -- --------------------------------------------------- -- CREATE TABLE tparent ( id integer NOT NULL, ord integer NOT NULL, CONSTRAINT par_pkey_id PRIMARY KEY (id), CONSTRAINT par_uniq_ord UNIQUE (ord) ); CREATE TABLE tchild ( par_id integer NOT NULL, ord integer NOT NULL, CONSTRAINT chi_pkey_parid_ord PRIMARY KEY (par_id, ord), CONSTRAINT chi_fkey FOREIGN KEY (par_id) REFERENCES tparent(id) ); INSERT INTO tparent VALUES (1, 3); INSERT INTO tparent VALUES (2, 1); INSERT INTO tparent VALUES (3, 4); INSERT INTO tparent VALUES (4, 5); INSERT INTO tparent VALUES (5, 2); INSERT INTO tchild VALUES (1, 2); INSERT INTO tchild VALUES (1, 1); INSERT INTO tchild VALUES (2, 1); INSERT INTO tchild VALUES (2, 3); INSERT INTO tchild VALUES (2, 2); INSERT INTO tchild VALUES (3, 1); INSERT INTO tchild VALUES (3, 2); INSERT INTO tchild VALUES (4, 1); INSERT INTO tchild VALUES (5, 2); INSERT INTO tchild VALUES (5, 1); ANALYZE tparent; ANALYZE tchild; SET enable_seqscan TO false; SET enable_bitmapscan TO false; SET enable_hashjoin TO false; SET enable_mergejoin TO false; SET enable_sort TO false; EXPLAIN ANALYZE SELECT * FROM tparent JOIN tchild ON tchild.par_id = tparent.id WHERE tparent.ord BETWEEN 1 AND 4 ORDER BY tparent.ord, tchild.ord; -- --------------------------------------------------- -- Sort (cost=100000132.10..100000140.10 rows=8 width=16) (actual time=0.440..0.456 rows=9 loops=1) Sort Key: tparent.ord, tchild.ord -> Nested Loop (cost=0.00..84.10 rows=8 width=16) (actual time=0.179..0.270 rows=9 loops=1) -> Index Scan using par_uniq_ord on tparent (cost=0.00..20.40 rows=4 width=8) (actual time=0.089..0.098 rows=4 loops=1) Index Cond: ((ord >= 1) AND (ord <= 4)) -> Index Scan using chi_pkey_parid_ord on tchild (cost=0.00..9.93 rows=2 width=8) (actual time=0.023..0.028 rows=2 loops=4) Index Cond: (tchild.par_id = "outer".id) -- --------------------------------------------------- -- Even though I forced the nested loop plan using both indexes (that returns the rows in the correct order), there is a needless sort step on the top, consuming half of the time even on such small tables. Now it's clear why the planner did not choose this plan, why I had to force it: because it isn't the best if the sort is still there. The first time I posted this ( http://archives.postgresql.org/pgsql-general/2007-05/msg01306.php ) and read Tom's answer I was convinced that this is rarely a problem, but now I don't think so, since I ran into it for the third time. Can that sort step somehow be eliminated if the NestLoop's outer table is being scanned via a unique index? If not, how can I rewrite my indexes/query in such a way that it's still safe (the rows always come in the order I want), but I don't have to wait for that needless sort? I'm using PostgreSQL 8.1.8. Thanks in advance, Denes Daniel ____________________________________________________ Olvasd az [origo]-t a mobilodon: mini magazinok a Mobizin-en ___________________________________________________ www.t-mobile.hu/mobizin