X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id AE7F0D1B16E for ; Sun, 16 May 2004 08:48:59 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 74287-09 for ; Sun, 16 May 2004 08:48:40 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 97F30D1B2E8 for ; Sun, 16 May 2004 08:48:39 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i4GBmaSx022385 for ; Sun, 16 May 2004 11:48:36 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i4GBgJ8K006814 for pgsql-sql@postgresql.org; Sun, 16 May 2004 11:42:19 GMT From: Gaetano Mendola X-Newsgroups: comp.databases.postgresql.sql Subject: Re: How to speed up a time dimension query Date: Sun, 16 May 2004 13:42:10 +0200 Organization: PYRENET Midi-pyrenees Provider Lines: 72 Message-ID: <40A75392.90309@bigfoot.com> References: <40a3ea79$0$568$e4fe514c@news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@pyrenet.fr To: Hans de Bruin User-Agent: Mozilla Thunderbird 0.6 (Windows/20040502) X-Accept-Language: en-us, en In-Reply-To: <40a3ea79$0$568$e4fe514c@news.xs4all.nl> To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200405/139 X-Sequence-Number: 17853 Hans de Bruin wrote: > Hi there, > > I like to speed up my homepage and need to do something about a query. A > half to one second to get 24 records from a time dimension table a bit > long. This is the table: > > news2=> \d dim_time > And here is the query which in my opinion take way to long: > > news2=> explain analyze select id,day,hour from dim_time > news2-> where id between (now()-interval '25 hours') > news2-> and (now()- interval '1 hour') > news2-> order by id ; This is not what I get with a table similiars to your: test=# select count(*) from user_logs; count --------- 3025880 (1 row) empdb=# explain analyze empdb-# select * empdb-# from user_logs empdb-# where login_time between (now()-interval '25 hours') and empdb-# (now()-interval '1 hour') empdb-# order by login_time; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------- Index Scan using idx_user_logs_login_time on user_logs (cost=0.00..502.67 rows=15130 width=47) (actual time=61.433..61.433 rows=0 loops=1) Index Cond: ((login_time >= (now() - '1 day 01:00:00'::interval)) AND (login_time <= (now() - '01:00:00'::interval))) Total runtime: 61.557 ms (3 rows) what do you obtain if you disable the sequential scan ? do it in this way: set enable_seqscan = off; if you obtain a cost higher then what you get on your explain: cost=590.26..590.44 and of course a lower Total runtime, then you have instruct your engine that is better perform and index scan, you can accoplish this decreasing the following values ( that values are what I use ). cpu_index_tuple_cost = 0.0005 cpu_operator_cost = 0.0025 cpu_tuple_cost = 0.005 decreasing these value you decrease the cost for the index scans if the Total time is higher you have to deal with the parameter effective_cache_size in order to use more ram. Regards Gaetano Mendola