From a7171eccf60d9d3a6bbde166f3b4e1a2b9e31020 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 3 Feb 2018 14:54:01 -0500 Subject: [PATCH] Replace EventTotal Updates in zmstats.pl with triggers which will hold locks less. Move stats updates to zmaudit.pl --- db/zm_create.sql.in | 68 ++++++++++----------------------------------- 1 file changed, 15 insertions(+), 53 deletions(-) diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index f94d3755b..9855460c1 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -229,17 +229,11 @@ CREATE TABLE `Events_Hour` ( ) ENGINE=@ZM_MYSQL_ENGINE@; delimiter // -DROP TRIGGER IF EXISTS Events_Hour_insert_trigger// - -CREATE TRIGGER Events_Hour_insert_trigger AFTER INSERT ON Events_Hour -FOR EACH ROW BEGIN - UPDATE Monitors SET HourEvents = COALESCE(HourEvents,0)+1 WHERE Id=NEW.MonitorId; -END; -// DROP TRIGGER IF EXISTS Events_Hour_delete_trigger// CREATE TRIGGER Events_Hour_delete_trigger BEFORE DELETE ON Events_Hour FOR EACH ROW BEGIN - UPDATE Monitors SET HourEvents = COALESCE(HourEvents,1)-1, + UPDATE Monitors SET + HourEvents = COALESCE(HourEvents,1)-1, HourEventDiskSpace=COALESCE(HourEventDiskSpace)-COALESCE(OLD.DiskSpace,0) WHERE Id=OLD.MonitorId; END; @@ -258,20 +252,12 @@ CREATE TABLE `Events_Day` ( ) ENGINE=@ZM_MYSQL_ENGINE@; delimiter // -DROP TRIGGER IF EXISTS Events_Day_insert_trigger// -CREATE TRIGGER Events_Day_insert_trigger AFTER INSERT ON Events_Day -FOR EACH ROW BEGIN - UPDATE Monitors SET - DayEvents = COALESCE(DayEvents,0)+1, - DayEventDiskSpace=COALESCE(DayEventDiskSpace)+COALESCE(NEW.DiskSpace,0) - WHERE Id=NEW.MonitorId; -END; -// DROP TRIGGER IF EXISTS Events_Day_delete_trigger// CREATE TRIGGER Events_Day_delete_trigger BEFORE DELETE ON Events_Day FOR EACH ROW BEGIN - UPDATE Monitors SET DayEvents = COALESCE(DayEvents,1)-1, - DayEventDiskSpace=COALESCE(DayEventDiskSpace)-COALESCE(OLD.DiskSpace,0) + UPDATE Monitors SET + DayEvents = COALESCE(DayEvents,1)-1, + DayEventDiskSpace=COALESCE(DayEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0) WHERE Id=OLD.MonitorId; END; // @@ -289,21 +275,12 @@ CREATE TABLE `Events_Week` ( ) ENGINE=@ZM_MYSQL_ENGINE@; delimiter // -DROP TRIGGER IF EXISTS Events_Week_insert_trigger// -CREATE TRIGGER Events_Week_insert_trigger AFTER INSERT ON Events_Week -FOR EACH ROW BEGIN - UPDATE Monitors SET - WeekEvents = COALESCE(WeekEvents,0)+1, - WeekEventDiskSpace=COALESCE(WeekEventDiskSpace)+COALESCE(NEW.DiskSpace,0) - WHERE Id=NEW.MonitorId; -END; -// DROP TRIGGER IF EXISTS Events_Week_delete_trigger// CREATE TRIGGER Events_Week_delete_trigger BEFORE DELETE ON Events_Week FOR EACH ROW BEGIN UPDATE Monitors SET - WeekEvents = COALESCE(WeekEvents,1)-1 - WeekEventDiskSpace=COALESCE(WeekEventDiskSpace)-COALESCE(OLD.DiskSpace,0) + WeekEvents = COALESCE(WeekEvents,1)-1, + WeekEventDiskSpace=COALESCE(WeekEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0) WHERE Id=OLD.MonitorId; END; // @@ -322,21 +299,12 @@ CREATE TABLE `Events_Month` ( ) ENGINE=@ZM_MYSQL_ENGINE@; delimiter // -DROP TRIGGER IF EXISTS Events_Month_insert_trigger// -create trigger Events_Month_insert_trigger after insert on Events_Month -FOR EACH ROW BEGIN - UPDATE Monitors SET - MonthEvents = COALESCE(MonthEvents,0)+1, - MonthEventDiskSpace=COALESCE(MonthEventDiskSpace)+COALESCE(NEW.DiskSpace,0) - WHERE Id=NEW.MonitorId; -END; -// DROP TRIGGER IF EXISTS Events_Month_delete_trigger// CREATE TRIGGER Events_Month_delete_trigger BEFORE DELETE ON Events_Month FOR EACH ROW BEGIN UPDATE Monitors SET - MonthEvents = COALESCE(MonthEvents,1)-1 - MonthEventDiskSpace=COALESCE(MonthEventDiskSpace)-COALESCE(OLD.DiskSpace,0) + MonthEvents = COALESCE(MonthEvents,1)-1, + MonthEventDiskSpace=COALESCE(MonthEventDiskSpace,0)-COALESCE(OLD.DiskSpace,0) WHERE Id=OLD.MonitorId; END; // @@ -429,9 +397,9 @@ delimiter // /* The assumption is that when an Event is inserted, it has no size yet, so don't bother updating the DiskSpace, just the count. * The DiskSpace will get update in the Event Update Trigger */ -create trigger event_insert_trigger after insert on Events -for each row - begin +CREATE TRIGGER event_insert_trigger AFTER INSERT ON Events +FOR EACH ROW + BEGIN INSERT INTO Events_Hour (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); INSERT INTO Events_Day (EventId,MonitorId,StartTime,DiskSpace) VALUES (NEW.Id,NEW.MonitorId,NEW.StartTime,0); @@ -441,21 +409,15 @@ for each row HourEvents = COALESCE(DayEvents,0)+1, DayEvents = COALESCE(DayEvents,0)+1, WeekEvents = COALESCE(DayEvents,0)+1, - MonthEvents = COALESCE(DayEvents,0)+1 + MonthEvents = COALESCE(DayEvents,0)+1, TotalEvents = COALESCE(TotalEvents,0)+1 WHERE Id=NEW.MonitorId; -end; +END; // -delimiter ; - - -drop trigger if exists event_delete_trigger; - -delimiter // +DROP TRIGGER IF EXISTS event_delete_trigger// CREATE TRIGGER event_delete_trigger BEFORE DELETE ON Events - FOR EACH ROW BEGIN call update_storage_stats(OLD.StorageId, -OLD.DiskSpace);