Solved

Automatic Sum in MySQL

Posted on 2007-03-29
3
414 Views
Last Modified: 2008-02-01
Hello,

Please consider the following tables:

Table #1 Day:
ID - int key
Year - int
Month - int
Day - int
DayVal - float

Table #2 Hour:
ID - int key
Year - int
Month - int
Day - int
Hour - int
HourVal - float

Is there anyway to automatically sum all of the HourVals of Table #2 into the DayVal of Table#1 for a given day?  I'd like this to be automatic.  For example, if I poll DayVal at any time it always contains the sum of the values in the sub-table.  I've been investigating using triggers but some sites have claimed this is not a good way to do this.  Am I missing a standard method for doing this.  Any direction would be appreciated.  Just for perspective, this database is going to store approximately 1480 values for each hour.

To take this to the next level, I am also considering going down to data minute-by-minute, and having the hour value be a sum of minutes, but that is not as important right now.

Any direction you could offer on this would be appreciated.
0
Comment
Question by:compsol1993
[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
3 Comments
 
LVL 5

Expert Comment

by:ethan_mmg
ID: 18817548
I can only think of 4 choices:
1) Use a trigger
2) Look for all occurrences in your code where you update HourVal and add more code to keep DayVal in sync
3) Have a periodic process keep generate the proper DayVals (sort of like an Oracle Materialized View). This will cause DayVals to be correct only as of the last time the process ran.
4) Get rid of Table #1 and use access it as a view of Table #2 (i.e. SELECT ID, Year, Month, Day, SUM(HourVal) FROM Table2 GROUP BY ID, Year, Month, Day
0
 

Author Comment

by:compsol1993
ID: 18819417
Ok, it looks from these options that the view would be optimal.  My only concern is the amount of data we are looking at.  Right now the hourly data table contains about 14 million data entries, and that has only accumulated over a year's time.  So next year at this time we can expect 14 million more.

I ran just one parameter's sum, and it took about 5 seconds.  Is this normal, and is this still the optimal choice considering this amount of data.  My thought was that a trigger would always have the data ready because it was calculated on INSERT, not on SELECT.  For the end user, what you advise as the most efficient option.  
0
 
LVL 5

Accepted Solution

by:
ethan_mmg earned 125 total points
ID: 18819647
If you have triggers available on the MySQL version you're using, I'd go with those.

CREATE TRIGGER mytrigger AFTER INSERT ON table1
  FOR EACH ROW BEGIN
    UPDATE table2 SET DayVal = SUM(HourVal) WHERE ID = NEW.ID AND year=NEW.year AND month=NEW.month AND day=NEW.day;
  END;
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

695 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