Solved

fast query for daily activity?

Posted on 2013-01-27
4
293 Views
Last Modified: 2013-03-20
I have a large table of daily activity, with a timestamp to the second as one of the columns.
There are several hundred million rows in the yearly log. The timestamp is indexed.
I need to produce a daily summary table for the year. For example, the report would have daily averages for the yearly log. Using count() group by day - is very slow. Is there a way to optimize this query?
0
Comment
Question by:pillmill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38825844
yes, there are possibilities.

you could do a daily accumulation into a dedicated table, and update it once per day, taking only the records from the previous day ...

say you create the table like this:
create table activity_daily_counts ( date_group date primary key, count_value int ) 

Open in new window


and you create a job that does the daily update, using this procedure:
create procedure update_daily_activity_count( @date date = null )
as
begin
  if @date is null   set @date = dateadd(day, -1, cast(getdate() as date))
  
  delete activity_daily_counts where date_group = @date
  insert into activity_daily_counts ( date_group, count_value )
   select @date, count(*)
     from  your_table
   where your_date_field  >= @date
     and your_date_field < dateadd(day, 1, @date)

end 

Open in new window


and you populate that table by either running the procedure once for every date value you can have, or simply by the normal one-shot insert:
  insert into activity_daily_counts ( date_group, count_value )
   select cast(your_date_field as date), count(*)
     from  your_table
group by cast(your_date_field as date)

Open in new window


and your query on the daily_counts table will be VERY fast-
0
 
LVL 16

Accepted Solution

by:
kmslogic earned 500 total points
ID: 38825847
This sounds like an analytics problem, so the big solution might be to look at one of the OLAP servers available for MySQL

I think what'd I try myself to solve this would be to run a nightly process to produce daily totals up through (today - 2 days), then combine those totals with a query of just the last two days from the detail records to generate your daily summary table for the year.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38825849
sorry, I put MS SQL syntax, you have MySQL ...
but the principle is the same ...
0
 
LVL 16

Expert Comment

by:kmslogic
ID: 38825856
Yes sorry I was typing my answer and overlapped angel.  He's the real expert, listen to him!
0

Featured Post

Webinar: Choosing a MySQL HA Solution

Join Percona’s Principal Technical Services Engineer, Marcos Albe as he presents Choosing a MySQL High Availability Solution on Thursday, June 29, 2017 at 10:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question