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
Related
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.
DROP EVENT `deleteTestEntries`;
CREATE DEFINER=`root`#`localhost` EVENT `deleteTestEntries`
ON SCHEDULE EVERY 1 MINUTE
STARTS '2018-05-25 18:17:01'
ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM lead_master WHERE lead_master.lname LIKE '%Test%'
What is wrong in above event.
It has been created with no errors but not performing any action.
I simply want to delete the records from my lead_master table where lname is 'Test'
Go into my.ini file and add this line,most probably this is the issue.
event_scheduler = on
Restart mysql.Apparently you can even set it on the fly
I finally did it with cron jobs.
I created a controller which calls a model function contains query to delete record.
and set the cron job which calls the controller after specific interval of time.
I am guessing that you don't need an event every minute. Just define a view to filter out the records you don't want:
create view v_lead_master as
select lm.*
from lead_master lm
where lm.lname not like '%Test%';
Then schedule a job or event every day at a slow period to delete the rows you want. Your application should use the view. Your testing should use the base table.
In MySQL phpmyadmin environment, when i create an event in which a DROP TABLE statement is set to occur, DROP TABLE (silently) does not occur, because my MySQL install, apparently, expects a confirmation.
DROP EVENT IF EXISTS test.target_ultra_sync ;
CREATE EVENT test.target_ultra_sync
ON SCHEDULE EVERY 30 SECOND
DO
DROP TABLE IF EXISTS `test`.`ultra`;
CREATE TABLE `test`.`ultra` AS SELECT * FROM `target_ultra`;
The same occurs with TRUNCATE and DELETE.
How can i suppress this behavior?
I don't get any prompts in phpMyAdmin from the event scheduler when I try a variant of your event.
It appears that your event syntax is off just a bit (I had to apply a minor modification), as you have compound statements after the DO that needs a BEGIN and END section like so:
DROP EVENT IF EXISTS test.target_ultra_sync ;
CREATE EVENT test.target_ultra_sync
ON SCHEDULE EVERY 30 SECOND
DO
BEGIN
DROP TABLE IF EXISTS `test`.`ultra`;
CREATE TABLE `test`.`ultra` AS SELECT * FROM `target_ultra`;
END
After that event change is applied, you can then check that the Executed_events variable is counting (roughly twice a minute based on your 30 second setting) by running this SQL command:
show status WHERE variable_name = 'Executed_events'
After you've run that query once in phpMyAdmin, you can simply refresh the query to get updated results as shown in the image below.
Of course, if you have other scheduled events it will be hard to know if the counts are strictly from this event alone, unless the others have a longer time that do not run as frequently.
Should you find that the Executed_events count is NOT increasing after creating the event, ensure that the event scheduler is running! This can be checked in phpMyAdmin here:
I've got table with products (changes dynamically) and table for data export. I need a trigger which once a day will add new products from table products into table export. Mine code is:
CREATE TRIGGER auto_update
BEFORE UPDATE
FOR EACH ROW
SET export.name = product.prod_name, export.link=product.prod_link, export.price=product.prod_price
WHERE product.prod_type='product';
But this code don't works. How to fix it? Any ideas?
Since you want this code to execute on scheduled basis, you may want to use the Event Scheduler instead.
Please see here: https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
From this you can do things like:
CREATE EVENT e_store_ts
ON SCHEDULE
EVERY 10 SECOND
DO
INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP());
This will create a new event to execute every 10 seconds and insert a unix time stamp into table mytable. You can alter events with ALTER and you can drop them with DROP. Have fun!
So in your case create an event that executes every day, that performs the expression you wish to execute (DO clause).
I have a mysql database with a table storing logs. This table receives records every 10 second.
I would like to truncate my table every week, that means after 80.000 records. I'd like to do a stored procedure which checks at every insert if there are more than 80.00. If there are more, the table is truncated.
I can't find how to use the if statement with SQL.
I know it would be better to do a script in a crontab to connect to the db and delete the table once a week, but i have the constraint to do it directly by SQL stored procedure.
Thank you in advance for any help !
You can do this with MySQL Event Scheduler, which is basically like cron, but runs as a thread within the MySQL Server. With it you can schedule the execution of an SQL statement or procedure.
First you have to turn it on:
SET GLOBAL event_scheduler = ON;
Then you can create an event:
CREATE EVENT MyEvent
ON SCHEDULE EVERY 1 WEEK
DO
TRUNCATE TABLE MyTable;
Read more about it here:
https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html
https://dev.mysql.com/doc/refman/5.7/en/create-event.html