I am trying to find the best way to process a record with a timestamp field whereby it would automatically move from one table to another table after 30 minutes.
If you are using MySQL 5.1.6 and later, check out MySQL events.
Events work the same with other routines, you can CREATE, ALTER, and even DROP them.
Martin Psinas has basic examples of this. (Just found online, we're not friends, lol)
First, you'll have to enable events scheduler:
SET GLOBAL event_scheduler = ON;
Then, you can create your event like (base from your question):
CREATE EVENT ProcessRecord
ON SCHEDULE EVERY 30 MINUTE
--STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
--ENDS CURRENT_TIMESTAMP + INTERVAL 1 YEAR
DO
BEGIN
INSERT INTO ...
SELECT ...
END
Otherwise, you can go with windows scheduled tasks, if you're using windows platform.
Related
I have a table loaded in MySQl workbench that I would like to refresh on a daily basis as the data changes.
Is there a way to refresh the data already held in SQL without having to remove the table and reload a fresh copy?
If you want to update the table every day in MySQL Workbench you can create an event.
Something like this
CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL recurring Event',NOW());
From Source
I have 2 databases, one of them is log, I want to make an event schedule in the main db but write a log in the db of logs
mainDB (event schedule doing something in mainDB and writing log in LOGDB)
LOGDB
I just don't know how to record data from one db event to another db
could someone tell me an example?
That is quite wage,
but you can do this
USE mainDB;
DELIMITER $$
CREATE EVENT e_daily
ON SCHEDULE
EVERY 1 DAY
COMMENT 'explain here what has to be done each day'
DO
BEGIN
DELETE FROM mainDB.mytable WHERE ID > 10;
INSERT INTO LOGDB.mytable (time, total)
VALUES (NOW(),10);
END $$
DELIMITER ;
CEATE EVENT has some Restrictions that has to be observed.
The different schemas/Databses are addressed by writing the name of the database before a table name and add a dot like mainDB.mytable
The correct syntax of your queries should be tested, before starting an event.
Usually you make during testing, that it runs once or twice before ending, so that you can check the result.
If I create an event in mysql with the interval every day like this:
CREATE EVENT `event`
ON SCHEDULE EVERY '1' DAY ON COMPLETION PRESERVE
ENABLE COMMENT '' DO
UPDATE table SET something = something + 1;
On what time of the day will it execute? I would assume 0AM but the specification does not mention it.
Yes, I am aware that it is possible to specify specific times, however I would like to keep it simple.
Not specifying STARTS is the same as using STARTS
CURRENT_TIMESTAMP—that is, the action specified for the event begins
repeating immediately upon creation of the event.
from the MySQL Reference
I would like to delete records from my database every 2 minutes.
I have a user table where I would like to delete users who are active after 2 minutes. I have read a little about using mysql event scheduler but unsure if I can achieve it?
wanted to ask if anybody has previously done anything similar who could help me start ?
You Can Create An Event SCHEDULE on your Mysql Server
First thing You must switch the event SCHEDULE to on that is because its Always off by default Run this Sql Query
SET GLOBAL event_scheduler = ON;
After That You can create an Event Schedule to delete your records from the table every 2 min You can use a query like this
DELIMITER $$
CREATE EVENT IF NOT EXISTS EventName
ON SCHEDULE EVERY 2 MINUTE
DO
BEGIN
DELETE FROM Your Table WHERE Your Conditions if Exists;
END$$
DELIMITER ;
This Event will automatically deletes your specific records every 2 min
I'm creating a database where i want to create a trigger.
This trigger will delete a row if the current time is 5 min over the requesttime in the table.
The requesttime is a datetime attribute in the table.
How can i make a trigger which do this?
You don't want to do this. First, triggers only run when something is happening inside the database (such as an insert or update). They are not the same as scheduled jobs. Second, you generally don't want to do this in a scheduled job. Instead, just create a view:
create view v_table as
select t.*
from table t
where requesttime >= now() - interval 5 minute;
This will return valid requests.
Then, at your leisure, delete the old rows -- if you really need to. There is a good chance you might want to keep them around to know what happened in the past. But you can delete them on Sunday morning at 3:00 a.m. or some other slow time, if you want.
You may want to consider Event Scheduler added in MySQL 5.1.6. Events can be seen as an alternative to Cronjob.
Their main advantages are:
Platform independent; directly written in MySsql
Ability to list all events SELECT * FROM INFORMATION_SCHEMA.EVENTS;
Built in error logging option
Syntax is something similar to:
CREATE EVENT my_event
ON SCHEDULE
EVERY 1 MINUTE
STARTS '2015-01-01 00:15:00'
COMMENT 'Removes forgotten password tokens older thank 1 week.'
DO
DELETE FROM my_table WHERE some_timestamp > NOW();