X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2DB9653C91 for ; Thu, 3 Mar 2005 13:42:53 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 68794-10 for ; Thu, 3 Mar 2005 13:42:49 +0000 (GMT) Received: from mx5.net.nih.gov (mx5.net.nih.gov [165.112.130.36]) by svr1.postgresql.org (Postfix) with ESMTP id 3EDAE52A8D for ; Thu, 3 Mar 2005 13:42:48 +0000 (GMT) Received: from mx5.net.nih.gov (localhost [127.0.0.1]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j23DgqC7029973 for ; Thu, 3 Mar 2005 08:42:52 -0500 Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j23Dgq8Y029967; Thu, 3 Mar 2005 08:42:52 -0500 In-Reply-To: <42271277.6040603@atlas.cz> References: <4226EF8A.3020403@atlas.cz> <2d042e6545ef2506cdced66867172c57@mail.nih.gov> <42271277.6040603@atlas.cz> Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: Content-Transfer-Encoding: 7bit Cc: PostgreSQL SQL From: Sean Davis Subject: Re: how to speed up these queries ? Date: Thu, 3 Mar 2005 08:42:51 -0500 To: Dracula 007 X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/43 X-Sequence-Number: 20522 On Mar 3, 2005, at 8:34 AM, Dracula 007 wrote: > >> It looks like you are going to always do a sequential scan on the >> tables, as you always look a the entire table(s). How often do you >> do the query as compared to the load on the database? If you do the >> query often relative to the load, could you keep a table of counts >> something like: >> create table summarize_use ( >> action_date date, >> sessions_count int, >> visitors_count int, >> isp_count int) >> and then use triggers from the sessions and actions to increment the >> various counts in the summarize_use table based on the action_date >> and session_date date parts? The summarize_use table would then look >> like: > > I can't exactly predict how often these queries will be executed - > this is an example of one of many different queries that are executed > over these two tables (in these two tables the main part of statistics > of our web application is stored). And for every query there can be > different parameter values etc. > > It definitely will be less than 1% of all queries run on the server, > but the problem is that running of it consumes most of the cpu, so all > the other queries have to wait and timeouts. And we don't want the > visitors on the web to wait ... > > As a temporary solution we build all the possible results once a week > (every Monday on 00:00) and store it in a separate tables, so it's > pretty fast to view, but it takes about 3 hours to rebuild all the > possible stats (total, by month, by day, by week) for different view > (pages, articles, visitors, etc). I still hope I'll be able to speed > it up somehow. > > The solution using a triggers looks nice, I'll try that and it > probably will work, but I can't predict how complicated it will be to > log all the interesting stats. > > t.v. > If your group by is at its finest grain only daily, then the most you will run these queries is daily, correct? Could you try some of your queries doing: select BLAH,BLAH,BLAH from BLAH where action_year=2005 and action_day=3 and action_month=3 after building indices on the columns? If this is fast (which it very well may be), then you can simply run that set of queries daily and insert the result into your "big stats" table, as presumably all other data in the "big stats" table is static if the date has past. That would save you writing the triggers, which could be complicated from what you are saying.... Sean