I have a table in my MYSQL database called sale as shown below:
I want to auto delete the sales that are 3 days old and not validated,
I create event but unfortunately it doesn't work here's my code :
CREATE EVENT `delete_sale` ON SCHEDULE EVERY 1 HOUR DO DELETE FROM sale WHERE sale.is_validated = 0 and sale.date < DATE_SUB(NOW(), INTERVAL 3 DAY)
My best guess is that your event is working, but it's only firing once then deactivating. To fix that, you should add the "ON XOMPLETION PRESERVE" line. Another possibility is that it's not loading properly because it can't quite parse your event code. To really hold MySQL's hand with this, try treating it like a multi statement event, changing the delimiter and adding BEGIN and END. Something like this:
DELIMITER &&
CREATE EVENT `delete_sale`
ON SCHEDULE EVERY 1 HOUR
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM sale WHERE sale.is_validated = 0 and sale.date < DATE_SUB(NOW(), INTERVAL 3 DAY);
END &&
DELIMITER ;
the problem resolved by enabling event_scheduler with the following query SET GLOBAL event_scheduler = ON;
Related
I'm creating a website for a simple poll from registered users. Every month, registered users only allowed to vote 5x for a poll.
Now, i'm going to create a server event with HEIDI SQL on 2 tables which will reset their 5 times votes on each month to 0
Here's my exe code:
CREATE EVENT `resetvalue`
ON SCHEDULE
EVERY 1 MONTH STARTS '2017-10-01 00:00:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Im going to reset this every month'
DO BEGIN
UPDATE tbl_users SET statusmonth = 0
UPDATE tbl_poll SET status1 = 0
UPDATE tbl_poll SET status2 = 0
END
Is this the correct code?
Unfortunately, I asked my friend and then he gave another code that look like this:
DELIMITER $$
CREATE EVENT `dbname`.`resetvalue`
ON SCHEDULE EVERY 1 MONTH
STARTS '2011-06-21 01:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
/(sql_statements)
END$$
DELIMITER ;
I got confused, which one is the correct code? What is a DELIMiTER anyway?
Below is a difference for your event with respect to your friend's event.
Your event will run on 1st of every month at 12:00 am and start date of event is 2017-10-01 00:00:00 but your friend's event will run on 21st at 12:00 am of every month and start date is 2011-06-21 01:00:00.
Your event will not deleted once event is completed. But your friend's event code will be removed once it will done. Here both events are never ending it's affect only if it's 5 times to run or you disable an event.
Other then this I think it's same. For more details you can refer MySQL event scheduler documentation.
https://dev.mysql.com/doc/refman/5.7/en/events-overview.html
I have a MySQL database with one big table in it. After a while, it becomes too full and performance degrades. Every Sunday, I want to delete rows whose last update is older than a certain number of days ago.
How do I do that?
Make a Scheduled Event to run your query every night. Check out Event Scheduler as well.
CREATE EVENT `purge_table` ON SCHEDULE
EVERY 1 DAY
ON COMPLETION NOT PRESERVE
ENABLE
COMMENT ''
DO BEGIN
DELETE FROM my_table WHERE my_timestamp_field <= now() - INTERVAL 5 DAY
END
What is the table design? Do you have a column with a timestamp?
Assuming you do, you could use that timestamp value with a datediff(your_date,CURDATE()) in a delete command.
Delete from table where datediff(date_col, CURDATE ()) > your_num_days.
Self Answer
Make a web server that sends the following SQL to the database every weekend:
DELETE FROM table WHERE timestamp < DATE_SUB(NOW(), INTERVAL 7 DAY);
or
DELETE FROM table
WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))
I might need locking to prevent accumulation of jobs, like so:
DELIMITER //
CREATE EVENT testlock_event ON SCHEDULE EVERY 2 SECOND DO
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
DO RELEASE_LOCK('testlock_event');
END;
IF GET_LOCK('testlock_event', 0) THEN
-- add some business logic here, for example:
-- insert into test.testlock_event values(NULL, NOW());
END IF;
DO RELEASE_LOCK('testlock_event');
END;
//
DELIMITER ;
Final answer:
CREATE EVENT `purge_table` ON SCHEDULE
EVERY 1 DAY
ON COMPLETION NOT PRESERVE
ENABLE
COMMENT ''
DO BEGIN
IF GET_LOCK('purge_table', 0) THEN
DELETE FROM table WHERE timestamp < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY));
END;
Maybe you can provide more information on how you are pushing the data to the DB and how you are working on the DB in general? Therefore we can help you and don't have to struggle with the dark...
I'll provide an easy solution: It's kind of workaround, but works:
Everytime you touch the data you update a time stamp in the updated rows.
Therefore you could easily filter them out every sunday.
UPDATE
The answer, the author provided by himself, was discussed at Stackoverflow and seems not to work in exactly that way, compare the discussion.
I am using SAILS JS and mysql adapter is being used. I have a model named as User with the following fields ID, USERNAME, EMAIL, ACTIVE_STATUS and CREATED_DATE.
By Default, active_status is set as 0. I want to update the status is 1 when created_date + 3 days is equal on Today.
Kindly suggest any possible ways to do this.
Hope you can use MySQL’s Event Scheduler
Activate it by: SET GLOBAL event_scheduler = ON;
Create event syntax:
CREATE EVENT `event_name`
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
DO BEGIN
-- event body
END;
The schedule can be assigned various settings, e.g.
Run once on a specific date/time:
AT ‘YYYY-MM-DD HH:MM.SS’
e.g. AT ‘2011-06-01 02:00.00’
Run once after a specific period has elapsed:
AT CURRENT_TIMESTAMP + INTERVAL n [HOUR|MONTH|WEEK|DAY|MINUTE]
e.g. AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
Run at specific intervals forever:
EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE]
e.g. EVERY 1 DAY
Run at specific intervals during a specific period:
EVERY n [HOUR|MONTH|WEEK|DAY|MINUTE] STARTS date ENDS date
e.g. EVERY 1 DAY STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK ENDS ‘2012-01-01 00:00.00’
Example:
SET GLOBAL event_scheduler = ON; -- enable event scheduler.
SELECT ##event_scheduler; -- check whether event scheduler is ON/OFF
CREATE EVENT e_store_ts -- create your event
ON SCHEDULE
EVERY 24 HOURS -- run every 24 hours
DO
UPDATE myschema.users set active_status = 1
Refer: http://dev.mysql.com/doc/refman/5.5/en/create-event.html
I have one event in my database:
CREATE DEFINER=`root`#`localhost` EVENT `movedate`
ON SCHEDULE
EVERY 1 DAY STARTS '2015-01-08 00:00:05'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Move tasks to current date if production is not fnished at previ'
DO update production set deliverydate = now() where deliverydate < now() and progress <> 2
Problem is that event is not executed even in my.ini in section mysqld
is set
event_scheduler=on
If I run manually query:
update production set deliverydate = now() where deliverydate < now() and progress <> 2
then I get warnings like:
Data truncated for column 'DeliveryDate' at row 3
The same thing, same code works fine on my PC where is installed mysql.
What I should check to see what causes that event is not fired?
I would like to create an event in MySQL in order to change a value in a table. The event checks whether a date (particularly, the TIME() part of a TIMESTAMP field) is more than the current date. Obviously, I have a time_stamp and an active column in a table named 'active'. This is what I have
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT #time:= time_stamp FROM active WHERE user_id = 1;
SELECT #time2:= TIMESTAMP(#time);
SELECT #active:=TIMEDIFF(#time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=if(TIME_TO_SEC(TIME(#active))>=0,1,0) WHERE user_id=1;
As far as I have seen, the SELECT part with the variables works fine, that is, TIME_TO_SEC(TIME(#active)) looks like a regressive count starting with 15 (provided I updated the 'time_stamp' field properly).
The intended behavior is that when TIME_TO_SEC(TIME(#active)) reach 0, the UPDATE query would change the value of the 'active' field from 1 to 0 (the default value is 1). However, it doesn't do anything.
UPDATE: To be precise, sometimes it changes the value to 0 but when TIME_TO_SEC(TIME(#active)) is still positive.
SECOND UPDATE: I recently tried this one:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT #time:= time_stamp FROM active WHERE user_id = 1;
SELECT #time2:= TIMESTAMP(#time);
SELECT #active:=TIMEDIFF(#time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=0 WHERE user_id=1 AND TIME_TO_SEC(TIME(#active))>=0;
which didn't work either.
THIRD UPDATE: I used the suggestion by Adam and I even if it doesn't work as expected, it certainly changes the value 'active' to 0, immediately after I start the event. Ideas?
SOLUTION: Thanks to Adam, I used his code with an IF statement:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
Do
UPDATE active AS t
SET t.active=IF(TIME_TO_SEC(TIMEDIFF(
t.time_stamp, DATE_ADD(NOW(),
INTERVAL -15 SECOND)))>=0,1,0)
WHERE user_id=1
which it works as intended. I don't why his suggestion didn't work, though.
Where did I make a mistake?
Of course, another approach to get this functionality would be greatly appreciated.
Thanks in advance.
What is the value in active.time_stamp where active.user_id = 1?
Also, I think that your statement might be written more simply.
If time_stamp in fact contains timestamps then you don't need the call to TIMESTAMP().
Remove TIME() function from UPDATE query because TIMEDIFF(expr1,expr2) returns expr1 – expr2 expressed as a time value.
So this is how I wrote your statement:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
UPDATE active t
SET t.active = 0
WHERE t.user_id = 1
AND TIME_TO_SEC(
TIMEDIFF(
t.time_stamp, DATE_ADD(NOW(),INTERVAL -15 SECOND)
)
)>=0;
I've tested that when the value in active.time_stamp is greater than now, this event sets the value in active.active to 0.
If that still isn't working you might also try something very basic like:
CREATE EVENT update_status_test
ON SCHEDULE EVERY 1 SECOND
DO
UPDATE active t
SET t.active = 0
WHERE t.user_id = 1;
And if that still isn't working then make sure the event scheduler is actually running. There are several ways to start it, this is one: SET GLOBAL event_scheduler = 1
Other notes:
This may not matter now, but as time goes on you will eventually run into the upper-limit of the TIME type in MySQL.