Solved

fast query for daily activity?

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 71
Access #Deleted data 20 43
Access Report formatting issue 5 22
How to trim a value in SQL 2 27
Read about achieving the basic levels of HRIS security in the workplace.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

733 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