我有这个触发器。如果传入日志与输入过滤器一致,则不会保存到数据库中。但是,我想保留每个Primitive_filter的“命中数”。我有一个名为hit_rate的列,它是int(30)。有什么方法可以做到这一点?可能具体错误?还是......其他的?求助。
UPDATE Primitive_filters SET hit_rate = hit_rate + 1 where Primitive_filters.id = ???;
触发
delimiter //
CREATE TRIGGER inputFilter
before insert
on Logs
for each row
begin
declare msg varchar(255);
IF (SELECT COUNT(*) FROM Primitive_filters, Primitive_in_filter, Filters WHERE
Filters.name = "input" AND Filters.id = Primitive_in_filter.id_filter AND Primitive_in_filter.id_primitive = Primitive_filters.id AND
(Primitive_filters.id_host LIKE CONCAT('%',(SELECT host FROM Hosts WHERE id = new.id_host),'%') OR Primitive_filters.id_host IS NULL) AND
(Primitive_filters.facility LIKE CONCAT('%',new.facility,'%') OR Primitive_filters.facility IS NULL) AND
(Primitive_filters.priority LIKE CONCAT('%',new.priority,'%') OR Primitive_filters.priority IS NULL) AND
(Primitive_filters.program LIKE CONCAT('%',new.program,'%') OR Primitive_filters.program IS NULL) AND
(new.msg REGEXP Primitive_filters.msg OR Primitive_filters.msg IS NULL)) > 0 THEN CALL raise_error; END IF;
END //
delimiter ;
答案 0 :(得分:0)
这不是您问题的答案。
它只是提示如何解决代码中可能存在的严重性能问题。
不要用这个:
IF (SELECT COUNT(*) FROM ... giant query ...) > 0
THEN CALL raise_error;
END IF;
请改用:
IF EXISTS (SELECT 1 FROM ... giant query ...)
THEN CALL raise_error;
END IF;
前一个条件计算一个计数...... 必须读取查询返回的所有行
如果查询返回十亿行,则必须全部读取 - >因为你问了give me a count of rows
然后,在查询返回计数后,检查:如果查询至少返回一行,则执行某些操作。
后一个条件在查询返回第一行时停止执行查询,从而节省了时间和资源。