每1分钟发生一次MYSQL事件不起作用
首先,这是OpenCart每1分钟发生一次MYSQL事件不起作用,mysql,mysql-event,Mysql,Mysql Event,首先,这是OpenCart 我有两张桌子: 1.oc_产品(产品id、型号、价格、活动开始、活动结束等) 2.oc_产品_至_类别(产品编号、类别编号) 每个产品都有开始日期和结束日期。我创建了MYSQL事件,它捕获每个过期的产品(event_end
我有两张桌子:
1.oc_产品(产品id、型号、价格、活动开始、活动结束等)
2.oc_产品_至_类别(产品编号、类别编号) 每个产品都有开始日期和结束日期。我创建了MYSQL事件,它捕获每个过期的产品(event_end
CREATE EVENT move_to_archive_category
ON SCHEDULE EVERY 1 MINUTE
STARTS NOW()
DO
INSERT INTO `oc_product_to_category` (product_id, category_id)
SELECT product_id, 68 as category_id
FROM oc_product p WHERE p.event_end < NOW() AND p.event_end <> '0000-00-00';
而且“展示活动”看起来不错
Db Name Definer Time zone Type Execute at Interval value Interval field Starts Ends Status Originator character_set_client collation_connection Database Collation
events move_to_archive_category root@localhost SYSTEM RECURRING NULL 1 MINUTE 2016-08-15 13:37:54 NULL ENABLED 1 utf8 utf8_general_ci utf8_general_ci
我在当地工作,不是住在这里
有什么想法吗
提前感谢!:) 我建议打开声纳。我的个人资料页面上挂着3个事件链接。因此,我创建了几个助手表(也可以在这些链接中看到),以帮助您打开声纳,查看事件中发生了什么。请注意,您可以像我在这些链接中所做的那样扩展它以进行性能跟踪 记住,事件的成功或失败(在你的头脑中)是基于数据的,它们是无声的。但是追踪发生的事情,当你在其中发展时,你可以极大地提高你的快乐水平 事件:
DROP EVENT IF EXISTS move_to_archive_category;
DELIMITER $$CREATE EVENT move_to_archive_category
ON SCHEDULE EVERY 1 MINUTE STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DO
BEGIN
DECLARE incarnationId int default 0;
DECLARE evtAlias varchar(20);
SET evtAlias:='move_2_archive';
INSERT incarnations(usedBy) VALUES (evtAlias);
SELECT LAST_INSERT_ID() INTO incarnationId;
INSERT EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
SELECT incarnationId,evtAlias,1,'Event Fired, begin looking',now();
INSERT INTO `oc_product_to_category` (product_id, category_id)
SELECT product_id, 68 as category_id
FROM oc_product p WHERE p.event_end < NOW() AND p.event_end <> '0000-00-00';
-- perhaps collect metrics for above insert and use that in debugMsg below
-- perhaps with a CONCAT into a msg
INSERT EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
SELECT incarnationId,evtAlias,10,'INSERT finished',now();
-- pretend there is more stuff
-- ...
-- ...
INSERT EvtsLog(incarnationId,evtName,step,debugMsg,dtWhenLogged)
SELECT incarnationId,evtAlias,99,'Event Finished',now();
END $$
DELIMITER ;
create table oc_product_to_category
( product_id INT not null,
category_id INT not null
);
create table oc_product
( product_id INT not null,
event_end datetime not null
);
drop table if exists incarnations;
create table incarnations
( -- NoteA
-- a control table used to feed incarnation id's to events that want performance reporting.
-- The long an short of it, insert a row here merely to acquire an auto_increment id
id int auto_increment primary key,
usedBy varchar(50) not null
-- could use other columns perhaps, like how used or a datetime
-- but mainly it feeds back an auto_increment
-- the usedBy column is like a dummy column just to be fed a last_insert_id()
-- but the insert has to insert something, so we use usedBy
);
drop table if exists EvtsLog;
create table EvtsLog
( id int auto_increment primary key,
incarnationId int not null, -- See NoteA (above)
evtName varchar(20) not null, -- allows for use of this table by multiple events
step int not null, -- facilitates reporting on event level performance
debugMsg varchar(1000) not null,
dtWhenLogged datetime not null
-- tweak this with whatever indexes your can bear to have
-- run maintenance on this table to rid it of unwanted rows periodically
-- as it impacts performance. So, dog the rows out to an archive table or whatever.
);
打开事件:
show variables where variable_name='event_scheduler'; -- OFF currently
SET GLOBAL event_scheduler = ON; -- turn her on
SHOW EVENTS in so_gibberish; -- confirm
确认Evt正在启动:
SELECT * FROM EvtsLog WHERE step=1 ORDER BY id DESC; -- verify with our sonar
有关这些助手表的更多详细信息,请访问“我的个人资料”页面上的事件链接。几乎只是性能跟踪和报告的一个链接
您还将注意到,在您最初关注的实际表中有任何数据时,这并不重要。这可以在以后进行,并且可以通过将自定义字符串CONCAT转换为字符串变量(用于计数等)在evt日志表中报告。并在第10步或第20步中报告
关键是,如果你不知道发生了什么事,你就完全失明了。
我在mysqlog中看到了以下错误
160816 10:18:00 [ERROR] Event Scheduler: [root@localhost][events.move_to_archive_category] Duplicate entry '29-68' for key 'PRIMARY'
160816 10:18:00 [Note] Event Scheduler: [root@localhost].[events.move_to_archive_category] event execution failed.
我只是在SQL插入中添加INGORE。。。所以最终的结果是
INSERT IGNORE INTO `oc_product_to_category` (product_id, category_id)
确保您在
oc_product
表中有符合存档标准的数据。是的,我创建了新产品,并在end_date=14.08.2016(今天是15.08)发布了它,一分钟后(下一个事件激活)第一次存档后不会存档任何数据。可能是所有数据(符合标准)已在第一次跑步时存档。您可能在oc_product
表中有新数据,但尚未满足标准。您是否可以从oc\u prdouct
表中转储本应存档但尚未完成的数据?事件是否还应删除待存档产品的任何现有oc\u product\u to\u category
记录?否则,该产品可能仍然“附加”到另一个类别。@彼得,这只是一个新记录。无论产品是否属于其他类别。如果产品过期,它将被附加到“存档”(id 68)类别中。Well解释道。在mysql中看到的一些insert语句中可能缺少INTO
。请记录以下内容160816 9:58:00[错误]事件调度器:[root@localhost][events.move_to_archive_category]键“PRIMARY”的重复条目“29-68”
1160816 9:58:00[注意]事件计划程序:[root@localhost][事件。移动到存档类别]事件执行失败
每1分钟重复一次。。。因此,事件正在工作但不受影响,因此您的事件正在启动。您只是插入了已经存在的数据,以便知道事件运行正常。是的,它现在运行正常:)
INSERT IGNORE INTO `oc_product_to_category` (product_id, category_id)