X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 7F7AF9DC9DA for ; Mon, 19 Dec 2005 16:23:09 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43371-02 for ; Mon, 19 Dec 2005 16:23:03 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from web35510.mail.mud.yahoo.com (web35510.mail.mud.yahoo.com [66.163.179.134]) by postgresql.org (Postfix) with SMTP id 235529DC9DB for ; Mon, 19 Dec 2005 16:22:59 -0400 (AST) Received: (qmail 30742 invoked by uid 60001); 19 Dec 2005 20:22:58 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com.br; h=Message-ID:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=3YcfUsHqrGjrYvZKjya86Byn1lv+6vgO6pAAcCMXtw/61QEJ4dQBg6dmtL4pt08YZyanvdbu/bs2JNXvPvdDB3Z9gyWfp3HN8NyOmRZav/3fXdXVkm400IiuYQki2BdpVDi8Lcmwghb4O5q65GcQVpAl+PonCB+Lo5XjtTvq8nE= ; Message-ID: <20051219202258.30740.qmail@web35510.mail.mud.yahoo.com> Received: from [201.2.221.147] by web35510.mail.mud.yahoo.com via HTTP; Mon, 19 Dec 2005 20:22:58 GMT Date: Mon, 19 Dec 2005 20:22:58 +0000 (GMT) From: Carlos Benkendorf Subject: Is the optimizer choice right? To: pgsql-performance@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-222280443-1135023778=:30675" Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.24 required=5 tests=[AWL=0.239, HTML_MESSAGE=0.001] X-Spam-Score: 0.24 X-Spam-Level: X-Archive-Number: 200512/463 X-Sequence-Number: 16284 --0-222280443-1135023778=:30675 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Hi, We´re running 8.03 and I´m trying to understand why the following SELECT doesn´t use iarchave05 index. If you disable seqscan then iarchave05 index is used and the total runtime is about 50% less than when iarchave05 index is not used. Why is the optimizer not using iarchave05 index? select * from iparq.arript where (anocalc = 2005 and rtrim(inscimob) = rtrim('010100101480010000') and codvencto2 = 1 and parcela2 >= 0) or (anocalc = 2005 and rtrim(inscimob) = rtrim('010100101480010000') and codvencto2 > 1) or (anocalc = 2005 and rtrim(inscimob) > rtrim('010100101480010000')) or (anocalc > 2005) order by anocalc, inscimob, codvencto2, parcela2; Explain analyze with set enable_seqscan and enable_nestloop to on; QUERY PLAN   -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=231852.08..232139.96 rows=115153 width=896) (actual time=38313.953..38998.019 rows=167601 loops=1) Sort Key: anocalc, inscimob, codvencto2, parcela2 -> Seq Scan on arript (cost=0.00..170201.44 rows=115153 width=896) (actual time=56.979..13364.748 rows=167601 loops=1) Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric)) Total runtime: 39247.521 ms (5 rows) Sort (cost=232243.19..232531.55 rows=115346 width=896) (actual time=46590.246..47225.910 rows=167601 loops=1) Sort Key: anocalc, inscimob, codvencto2, parcela2 -> Seq Scan on arript (cost=0.00..170486.86 rows=115346 width=896) (actual time=54.573..13737.535 rows=167601 loops=1) Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric)) Total runtime: 47479.861 ms (5 rows) Sort (cost=232281.07..232569.48 rows=115365 width=896) (actual time=40856.792..41658.379 rows=167601 loops=1) Sort Key: anocalc, inscimob, codvencto2, parcela2 -> Seq Scan on arript (cost=0.00..170515.00 rows=115365 width=896) (actual time=58.584..13529.589 rows=167601 loops=1) Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric)) Total runtime: 41909.792 ms (5 rows) Explain analyze with set enable_seqscan and enable_nestloop to off; ; QUERY PLAN   Index Scan using iarchave05 on arript (cost=0.00..238964.80 rows=115255 width=896) (actual time=13408.139..19814.848 rows=167601 loops=1) Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric)) Total runtime: 20110.892 ms (3 rows) Index Scan using iarchave05 on arript (cost=0.00..239091.81 rows=115320 width=896) (actual time=14238.672..21598.862 rows=167601 loops=1) Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric)) Total runtime: 21967.840 ms (3 rows) Index Scan using iarchave05 on arript (cost=0.00..239115.06 rows=115331 width=896) (actual time=13863.863..20504.503 rows=167601 loops=1) Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric)) Total runtime: 20768.244 ms (3 rows) Table definition: Table "iparq.arript" Column | Type | Modifiers -------------------+-----------------------+----------- anocalc | numeric(4,0) | not null cadastro | numeric(8,0) | not null codvencto | numeric(2,0) | not null parcela | numeric(2,0) | not null inscimob | character varying(18) | not null codvencto2 | numeric(2,0) | not null parcela2 | numeric(2,0) | not null codpropr | numeric(10,0) | not null dtaven | numeric(8,0) | not null ... ... ... Indexes: "pk_arript" PRIMARY KEY, btree (anocalc, cadastro, codvencto, parcela) "iarchave04" UNIQUE, btree (cadastro, anocalc, codvencto, parcela) "iarchave02" btree (inscimob, anocalc, codvencto2, parcela2) "iarchave03" btree (codpropr, dtaven) "iarchave05" btree (anocalc, inscimob, codvencto2, parcela2) Thanks in advance! Benkendorf --------------------------------- Yahoo! doce lar. Faça do Yahoo! sua homepage. --0-222280443-1135023778=:30675 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: 8bit
Hi,
 
We´re running 8.03 and I´m trying to understand why the following SELECT doesn´t use iarchave05 index.
 
If you disable seqscan then iarchave05 index is used and the total runtime is about 50% less than when iarchave05 index is not used.
 
Why is the optimizer not using iarchave05 index?

 select * from iparq.arript
 where
 (anocalc = 2005
 and rtrim(inscimob) = rtrim('010100101480010000')
 and codvencto2 = 1
 and parcela2 >= 0)
 or
 (anocalc = 2005
 and rtrim(inscimob) = rtrim('010100101480010000')
 and codvencto2 > 1)
 or
 (anocalc = 2005
 and rtrim(inscimob) > rtrim('010100101480010000'))
 or
 (anocalc > 2005)
 order by
 anocalc,
 inscimob,
 codvencto2,
 parcela2;

Explain analyze with  set enable_seqscan and enable_nestloop to on;
                                                                                                                                                                   &nbs p;                                  QUERY PLAN                                                                                                                                                                    & nbsp;&nb sp; 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Sort  (cost=231852.08..232139.96 rows=115153 width=896) (actual time=38313.953..38998.019 rows=167601 loops=1)
   Sort Key: anocalc, inscimob, codvencto2, parcela2
   ->  Seq Scan on arript  (cost=0.00..170201.44 rows=115153 width=896) (actual time=56.979..13364.748 rows=167601 loops=1)
         Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric))
 Total runtime: 39247.521 ms
(5 rows)
 Sort  (cost=232243.19..232531.55 rows=115346 width=896) (actual time=46590.246..47225.910 rows=167601 loops=1)
   Sort Key: anocalc, inscimob, codvencto2, parcela2
   ->  Seq Scan on arript  (cost=0.00..170486.86 rows=115346 width=896) (actual time=54.573..13737.535 rows=167601 loops=1)
         Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::nu meric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric))
 Total runtime: 47479.861 ms
(5 rows)

 Sort  (cost=232281.07..232569.48 rows=115365 width=896) (actual time=40856.792..41658.379 rows=167601 loops=1)
   Sort Key: anocalc, inscimob, codvencto2, parcela2
   ->  Seq Scan on arript  (cost=0.00..170515.00 rows=115365 width=896) (actual time=58.584..13529.589 rows=167601 loops=1)
         Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric))
 Total runtime: 41909.7 92 ms
(5 rows)
Explain analyze with  set enable_seqscan and enable_nestloop to off;
                                                                                                                                                                   &nb sp;  ;                             QUERY PLAN                                                                                                                                                                    & nbsp;&nb sp;    
 Index Scan using iarchave05 on arript  (cost=0.00..238964.80 rows=115255 width=896) (actual time=13408.139..19814.848 rows=167601 loops=1)
   Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric))
 Total runtime: 20110.892 ms
(3 rows)

 Index Scan using iarchave05 on arript  (cost=0.00..239091.81 rows=115320 width=896) (actual time=14238.672..21598.862 rows=167601 loops=1)
   Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric))
 Total runtime: 21967.840 ms
(3 rows)

 Index Scan using iarchave05 on arript  (cost=0.00..239115.06 rows=115331 width=896) (actual time=13863.863..20504.503 rows=167601 loops=1)
   Filter: (((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 = 1::numeric) AND (parcela2 >= 0::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) = '010100101480010000'::text) AND (codvencto2 > 1::numeric)) OR ((anocalc = 2005::numeric) AND (rtrim((inscimob)::text) > '010100101480010000'::text)) OR (anocalc > 2005::numeric))
 Total runtime: 20768.244 ms
(3 rows)
Table definition:
                 Table "iparq.arript"
      Column       |         Type          | Modifiers
-------------------+-----------------------+-----------
 anocalc           | numeric(4,0)          | not null
 cadastro          | numeric(8,0)          | not null
 codvencto         | numeric(2,0)          | not null
 parcela           | numeric(2,0)        &nbs p; | not null
 inscimob          | character varying(18) | not null
 codvencto2        | numeric(2,0)          | not null
 parcela2          | numeric(2,0)          | not null
 codpropr          | numeric(10,0)         | not null
 dtaven            | numeric(8,0)          | not null
...
...
...
Indexes:
    "pk_arript" PRIMARY KEY, btree (anocalc, cadastro, codvencto, parcela)
    "iarchave04" UNIQUE, btree (cadastro, anocalc, codvencto, parcela)
    "iarchave02" btree (inscimob, anocalc , codvencto2, parcela2)
    "iarchave03" btree (codpropr, dtaven)
    "iarchave05" btree (anocalc, inscimob, codvencto2, parcela2)
 
Thanks in advance!
 
Benkendorf


Yahoo! doce lar. Faça do Yahoo! sua homepage. --0-222280443-1135023778=:30675--