Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 445C59FB1BB for ; Sun, 29 Jul 2007 13:44:33 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 03238-03 for ; Sun, 29 Jul 2007 13:44:28 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from hm32.locaweb.com.br (hm32-3.locaweb.com.br [200.234.205.225]) by postgresql.org (Postfix) with SMTP id 57A959FA616 for ; Sun, 29 Jul 2007 13:44:29 -0300 (ADT) Received: (qmail 12358 invoked from network); 29 Jul 2007 16:44:26 -0000 Received: from unknown (10.1.10.142) by hm32.locaweb.com.br with QMQP; 29 Jul 2007 16:44:26 -0000 Received: from unknown (HELO brunolaptop) (bruno@ravnus.com@201.29.113.232) by hm456.locaweb.com.br with SMTP; 29 Jul 2007 16:44:36 -0000 From: "Bruno Rodrigues Siqueira" To: "'Decibel!'" , "'Ragnar'" Cc: Subject: RES: RES: select on 1milion register = 6s Date: Sun, 29 Jul 2007 13:44:23 -0300 Message-ID: <005b01c7d1ff$b96ea3d0$e900a8c0@brunolaptop> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 Thread-Index: AcfR/olfYaFndOb1QpyMfR3fPdA/0QAAGC9A X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 In-Reply-To: <20070729163536.GM25704@nasby.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.63 tagged_above=0 required=5 tests=AWL=0.629, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200707/413 X-Sequence-Number: 26097 Look it EXPLAIN ANALYZE select to_char(data_encerramento,'mm/yyyy') as opcoes_mes, to_char(data_encerramento,'yyyy-mm') as ordem from detalhamento_bas where data_encerramento = '01/12/2006' GROUP BY opcoes_mes, ordem ORDER BY ordem DESC **************************************************************************** QUERY PLAN Sort (cost=60.72..60.72 rows=1 width=8) (actual time=4.586..4.586 rows=0 loops=1) Sort Key: to_char(data_encerramento, 'yyyy-mm'::text) -> HashAggregate (cost=60.72..60.72 rows=1 width=8) (actual time=4.579..4.579 rows=0 loops=1) -> Index Scan using detalhamento_bas_idx3005 on detalhamento_bas (cost=0.00..60.67 rows=105 width=8) (actual time=4.576..4.576 rows=0 loops=1) Index Cond: (data_encerramento = '2006-12-01 00:00:00'::timestamp without time zone) Total runtime: 4.629 ms //////////////////////////////////////////////////////////////////////////// EXPLAIN ANALYZE select to_char(data_encerramento,'mm/yyyy') as opcoes_mes, to_char(data_encerramento,'yyyy-mm') as ordem from detalhamento_bas where data_encerramento >= '01/12/2006' and data_encerramento < '01/02/2007' GROUP BY opcoes_mes, ordem ORDER BY ordem DESC **************************************************************************** QUERY PLAN Sort (cost=219113.10..219113.10 rows=4 width=8) (actual time=10079.212..10079.213 rows=2 loops=1) Sort Key: to_char(data_encerramento, 'yyyy-mm'::text) -> HashAggregate (cost=219113.09..219113.09 rows=4 width=8) (actual time=10079.193..10079.195 rows=2 loops=1) -> Seq Scan on detalhamento_bas (cost=0.00..217945.41 rows=2335358 width=8) (actual time=0.041..8535.792 rows=2335819 loops=1) Filter: ((data_encerramento >= '2006-12-01 00:00:00'::timestamp without time zone) AND (data_encerramento < '2007-02-01 00:00:00'::timestamp without time zone)) Total runtime: 10079.256 ms Strange!!! Why does the index not works? All my querys doesn't work with range dates.... I don't know what to do... Please, help! Bruno -----Mensagem original----- De: Decibel! [mailto:decibel@decibel.org] Enviada em: domingo, 29 de julho de 2007 13:36 Para: Ragnar Cc: Bruno Rodrigues Siqueira; pgsql-performance@postgresql.org Assunto: Re: RES: [PERFORM] select on 1milion register = 6s On Sat, Jul 28, 2007 at 10:36:16PM +0000, Ragnar wrote: > On lau, 2007-07-28 at 17:12 -0300, Bruno Rodrigues Siqueira wrote: > > > where > > > > to_char( data_encerramento ,'yyyy-mm') > > between '2006-12' and '2007-01' > > assuming data_encerramento is a date column, try: > WHERE data_encerramento between '2006-12-01' and '2007-01-31' IMO, much better would be: WHERE data_encerramento >= '2006-12-01' AND data_encerramento < '2007-02-01' This means you don't have to worry about last day of the month or timestamp precision. In fact, since the field is actually a timestamp, the between posted above won't work correctly. -- Decibel!, aka Jim Nasby decibel@decibel.org EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)