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
Solved

fast query for daily activity?

Posted on 2013-01-27
4
288 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mongo DB 18 62
Import MySQL data into MS Access using VB.Net interface 5 29
Access 2016 Query with Proper Case logic 2 34
SQL Select in Access 2003 3 26
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

839 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