If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Tricky trigger question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-15-09, 16:18
attrcat attrcat is offline
Registered User
 
Join Date: May 2009
Posts: 7
Tricky trigger question

Hi all,

I am trying to figure out a way to dynamically manage the amount of entries in two different tables. I think a trigger with a stored procedure would be the best way to do this, but I can't figure out how to best write it.

What I need to have happen is the following:

I have two tables of information. The first table should only hold one day's worth of information, and the second should hold three month's worth of information. I want to design a trigger that takes any entry in the first table that is over a day old and deletes it, inserting the values into the historical table. I want to do a similar thing with the historical table, deleting any entry once it becomes more than 3 months old.

I have a field called "timestamp", so I can compare this against the current date/time if that sounds reasonable.

Can anyone figure this out? Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 05-15-09, 17:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
that's ~so~ inefficient, excessively complex, error-prone, and needless

i have a better solution that does not involve a trigger, nor a stored procedure, nor any additional effort on your part whatsoever, and yet it can still deliver the exact same benefits as what you're trying to do

would you be interested?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-15-09, 18:44
attrcat attrcat is offline
Registered User
 
Join Date: May 2009
Posts: 7
I'm certainly interested, though I must warn you I have no money to spend on additional software
Reply With Quote
  #4 (permalink)  
Old 05-15-09, 19:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
well, this will be good news for you then, because there is no additional software required either

the solution? it's actually quite simple

forget about the separate table for one day's worth of information, and the other table for three month's worth

just have a single table which collects all your information indefinitely

there, wasn't that easy?

no stored proc, no trigger, no development effort, no extra software...

neat, eh?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-15-09, 20:04
attrcat attrcat is offline
Registered User
 
Join Date: May 2009
Posts: 7
well, the only problem with this solution is that it uses up too much disk space. That was the reason for only storing 3 months worth of historical data in the first place, I guess I should have said that....
Reply With Quote
  #6 (permalink)  
Old 05-15-09, 20:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by attrcat
well, the only problem with this solution is that it uses up too much disk space.
i disagree, it does not

although i suppose it depends on what you mean by "too much"

how many rows do you expect to collect in 3 months? in 3 years? in 3 decades?

make realistic estimates, please

also, what kind of data are we talking about here? banking transactions? friends of friends? football pool bets? urls scraped off teh interwebs? license plates of cars that you've seen?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-15-09, 21:39
attrcat attrcat is offline
Registered User
 
Join Date: May 2009
Posts: 7
There will be an entry made to the tables once every two seconds for each person on a team, with 10 fields per entry. A year of this would be a HUGE amount of data. Also, it is necessary to have a table with JUST one day's worth of data for analysis
Reply With Quote
  #8 (permalink)  
Old 05-16-09, 00:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
sorry, i don't see why you insist on splitting up a table when you don't have to

good luck with your project

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 05-16-09, 05:29
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I have 100Mb of data.
I am going to split it into 10 chunks of 10Mb.

I still have 100Mb of data!

You design is not saving any diskspace whatsoever. Rudy is quite correct in his suggestions of using a single table.
__________________
George
Twitter | Blog
Reply With Quote
  #10 (permalink)  
Old 05-16-09, 15:49
attrcat attrcat is offline
Registered User
 
Join Date: May 2009
Posts: 7
OK, well forget about the two different tables plan...what if I just had one table, and I wanted it to be truncated at 3 months worth of data. How could I accomplish this?
Reply With Quote
  #11 (permalink)  
Old 05-16-09, 18:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by attrcat
OK, well forget about the two different tables plan...what if I just had one table, and I wanted it to be truncated at 3 months worth of data. How could I accomplish this?
one way would be to copy the last 3 months' worth of data into a new table, delete the old one, and rename the new one

but i say it once again, you do ~not~ need to do this

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On