Delete old MySQL records every hour - php

Suppose I include the built in MySQL timestamp into my fields on my database table.
Everytime I update the records it will update the timestamp.
I would like to delete records than are older than an hour.
Any ideas for how best to do this?
I could have a loop checking the timestamp for all records or perhaps a trigger in the database?

You can use a cronjob to schedule a query to be executed in a fixed time interval:
DELETE FROM table_name WHERE time_created < (UNIX_TIMESTAMP() - 3600);
Every time it's run, it will delete all records older than 1 hour (which is presumably what you want).

Related

Deleting from sql database. based on time stamp (cron job)?

I currently have my database updating the timestamp when new records are inserted. It formats like this:
2017-08-24 15:48:30.189182
My question is: how do I delete records that are over, suppose, 3 days old?
I'm assuming I can run a cron job nightly to remove the records older than 3 days. Can someone give me the format for it? I'm not sure how to work with the timestamp data.
Thank you for your help!
different database has different way to compute time difference between two days...
MySQL: DATEDIFF(date1,date2);
Oracle: select date1 - date2 from dual;
and so on...
Assuming the column is of type DATE, you can run a query that deletes all columns where the difference between today and your column is greater than 3.
Replace table with your table, and date_column with your column-name. Again, this assumes that you've got columns that is of the DATE-type, and not a VARCHAR or something.
DELETE FROM table WHERE DATEDIFF(CURDATE(), date_column) > 3
SQLFiddle
MySQL Date functions

How to delete a record automatically in sql and php after 2 days?

I want to automatically delete a record from my database sql after 2 days of inserting the record. I am currently developing a simple reservation. How can I achieve this??
Add an event to your table that runs once every day and deletes those entries. You need to have a date column though to recognize these records.
Assuming you're using some sort of timestamp on insert then you can run a scheduled job that checks the age of the record with datediff and then deletes the record if datediff = 2 days

php database - storing 30 days of data for each user

I am trying to set up a database to record the last 30 days of information for each user. The data will be recorded once a day (i.e. by a cron job) and will be the value of an item (i.e. constantly changes).
What would be the best way to structure this? I was thinking of setting a table and then just storing the 30 days in the table and deleting the 31st day as I add the new day with the cron job (and shifting all of the others up one day) but this doesn't seem very efficient..
Thanks for the help.
What you can do is store the current date with each entry, then in your cron job, delete all entries that are greater than thirty days old.
For example (with MySQL),
DELETE FROM user_statistics WHERE DATEDIFF(NOW(), date_of_record) > 30;
Store the user data with its own date and delete the oldest when you exceed your limit. No need to shift anything.
I'd log by actual date using a DATE column. You can query up "last 30 days" pretty easily in MySQL.
As for purging, the cron job can delete anything older than 30 days pretty easily as well. Or, since it's so easy to ignore anything older than 30 days, you might even choose to not delete older records (at least not every day).

SQL entries that expire after 24 hours

I want to make a table where the entries expire 24 hours after they have been inserted in PHP and MySQL.
Ideally I want to run a "deleting process" every time a user interacts with my server, that deletes old entries. Since this is more frequent you should it will not have large amounts of data to delete so it should only take a few milliseconds.
I have given each entry a date/time added value.
How would I do this?
You could use MySQL's event scheduler either:
to automatically delete such records when they expire:
CREATE EVENT delete_expired_101
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 24 HOUR DO
DELETE FROM my_table WHERE id = 101;
to run an automatic purge of all expired records on a regular basis:
CREATE EVENT delete_all_expired
ON SCHEDULE EVERY HOUR DO
DELETE FROM my_table WHERE expiry < NOW();
you shouldn't do a delete process when a user interacts. it slows down things, you should use a cronjob (every minute / hour)
you'll want to index the added timestamp value and then run DELETE FROM table WHERE added < FROM_UNIXTIME(UNIX_TIMESTAMP()-24*60*60)
maybe you'll want to checkout Partitions, which divide the table into different tables, but it behaves as one table. The advantage is that you don't need to delete the entries and you'll have seperate tables for each day.
i think that YOU think that much data slows down tables. Maybe you should use EXPLAIN (MySQL Manual) and optimize your SELECT queries using indexes (MySQL Manual)
UPDATE Check out eggyal's answer - This is another approach worth taking a look.
You can look into using Cron Job, http://en.wikipedia.org/wiki/Cron Make it run once every 24 hours when it matches your requirement.
This will help
Delete MySQL row after time passes

MySQL - Time difference between one row and the current row being inserted

So I have a MySQL table that looks something like this:
id timestamp action timePassed
1 2012-07-10 22:44:00 start 0
2 2012-07-10 22:44:50 pause 50
3 2012-07-10 22:45:30 play 50
4 2012-07-10 22:47:25 pause 205
5 2012-07-10 22:48:05 play 205
I don't know how obvious it is but what's happening here is basically on every 'pause' row I am calculating the difference in seconds between the current timestamp and the timestamp of the 'start' row.
The only way I can think of doing this is to INSERT a new 'pause' row into the database so the timestamp is generated...then query the database for the timestamp of that 'pause' row...calculate the difference in seconds between that 'pause' row and the 'start' row using PHP...then UPDATE the 'pause' row with the timePassed result.
My question is whether or not there is a better way to do this (i.e. using timediff or some other MySQL command). The issue is the 'pause' timestamp doesn't exist until I make the INSERT, so I feel like that needs to happen first before I can make any calculation?
Well if your table is like this and your requirement is like this then you are on track..
However better if you check the second difference between last row and last to last row. This will give you idea for how many second user has PAUSED or Played the TRACK.
INSERT INTO table (timestamp, action, timePassed)
SELECT now(), 'pause', SECOND(timediff(now(), timestamp))
FROM table
WHERE ID = (Select Max(ID) from Table)
Put action as you like so you can insert time difference between two.
The only way I can think of doing this is to INSERT a new 'pause' row
into the database so the timestamp is generated...then query the
database for the timestamp of that 'pause' row...calculate the
difference in seconds between that 'pause' row and the 'start' row
using PHP...then UPDATE the 'pause' row with the timePassed result.
You can INSERT the already calculated row.
INSERT INTO table (timestamp, action, timePassed)
SELECT now(), 'pause', SECOND(timediff(now(), timestamp))
FROM table
WHERE action = 'start'; -- and/or other WHERE clauses.
This will introduce a skew in time due to the time needed to retrieve the 'start' row (but with index on action (and maybe timestamp if the row is very large), that time should be negligible).
I would recommend not storing timePassed explicitly at all, since by having each event's timestamp, you already have every interval stored once. If you separately store timePassed, you'll be storing the same data in multiple places. To quote E. F. Codd, "If something is true, saying it twice doesn't make it any more true." Also, you're subject to update anomalies when you store the same thing multiple times.
If you want to calculate the interval with a query, this might work.
SELECT event.id,
event.timestamp,
event.action,
TIME_TO_SEC(TIMEDIFF(event.timestamp, start_event.timestamp)) time_passed
FROM event
JOIN (SELECT timestamp
FROM event
WHERE event.action = 'start') start_event ON 1 = 1

Categories