X-Original-To: pgsql-performance@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3A514476711 for ; Thu, 15 May 2003 11:09:19 -0400 (EDT) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with ESMTP id 22806-10 for ; Thu, 15 May 2003 11:08:48 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id E9F3D476546 for ; Thu, 15 May 2003 11:08:47 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 8BE73D622; Thu, 15 May 2003 08:08:52 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 81A245C04; Thu, 15 May 2003 08:08:52 -0700 (PDT) Date: Thu, 15 May 2003 08:08:52 -0700 (PDT) From: Stephan Szabo To: =?Windows-1252?Q?Sub_Director_-_Sistemas_Inform=E1ticos?= Cc: Subject: Re: [ADMIN] nested query too expensive In-Reply-To: <003f01c31ae4$b15a59e0$db00a8c0@219> Message-ID: <20030515080135.M60859-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN Content-Transfer-Encoding: QUOTED-PRINTABLE X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/185 X-Sequence-Number: 1964 [Moving to -performance, since it's more on topic there] On Thu, 15 May 2003, [Windows-1252] Sub Director - Sistemas Inform=E1ticos = wrote: > This a relatively simple nested query that we try to use, but it finish i= n a "seq scan" with a > too high cost, so we had to use a little orthodox solution creating a tem= poral table into the > terminal and scanning this table row's one by one making individual query= s for each one. > > Any body knows how to make the query work in "index scan" mode ? > explain select w.*,b.nombre from (select nro_insc,cod_estab,cuitempre,imp= uesto,sum(monto_impo) > as totret,sum(monto_rete) as suma_rete,tipodoc,documento from detadj whe= re nro_insc=3D390009 > and cod_estab=3D0 and ano=3D2003 and mes=3D4 and per=3D2 and sec=3D0 gro= up by nro_insc,cod_estab, > cuitempre,impuesto,tipodoc,documento) w LEFT OUTER JOIN retper b on (w.t= ipodoc=3Db.tipodoc > and btrim(w.documento) like btrim(b.documento) and btrim(w.cuitempre) li= ke btrim(b.cuitempre) > and w.nro_insc=3Db.nro_insc and w.cod_estab=3Db.cod_estab) If you're doing a condition on a bunch of columns, you might want a multi-column index, since postgres is only going to use one of the indexes below I believe and it may not be considered selective enough on just one of those conditions. And you're doing cross datatype comparisons, which is likely to screw it up as well (why is tipodoc an integer in one and a numeric in the other for example?) I'd also say you might want to consider upgrading to 7.3.x since the explain format looks like that from 7.2 or earlier. Also explain analyze output would tell us what is actually taking the time and could be useful as well. > Indexes: > cuitemp_btrim, > docu_btrim, > retper_cod_estab, > retper_cuitempre, > retper_documento, > retper_nombre, > retper_nro_insc, > retper_tipodoc > > ________________________________________________ > > Table "detadj" ( 18.500.000 rows ) > > Column | Type | Modifiers > ------------+-----------------------+----------- > cuitempre | character varying(20) | > sec | numeric(10,0) | > per | numeric(10,0) | > mes | numeric(10,0) | > ano | numeric(10,0) | > nro_insc | numeric(10,0) | > cod_estab | numeric(10,0) | > nobli | character varying(20) | > cod_act | character varying(20) | > tipo_agen | character varying(1) | > monto_impo | double precision | > alicuota | double precision | > monto_rete | double precision | > tipodoc | numeric(10,0) | > documento | character varying(20) | > impuesto | numeric(10,0) | > tipo_dato | numeric(10,0) | > id | character varying(11) | > tipo_comp | numeric(10,0) | > letra | character varying(1) | > terminal | numeric(10,0) | > numero | character varying(20) | > fecha | date | > ningbru | character varying(20) | > graba | date | > hora | character varying(4) | > opera | numeric(10,0) | > puesto | numeric(10,0) | > Indexes: > ano_detadj, > ano_mes_per, > cod_estab, > cuitempre, > cuitempre_btrim, > documento_btrim, > impue, > mes_detadj, > nro_insc_detadj, > per_detadj, > sec