Solved

fast query for daily activity?

Posted on 2013-01-27
4
282 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
  • 2
  • 2
4 Comments
 
LVL 142

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 142

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

803 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