mysql函數實例-統計日存留率 1.數據庫表 手機用戶的信息保存在一張visitor_user的表中,其中包含了用戶使用手機app軟件時,可以獲取的數據,包括: 商家id、手機設備號、下線時間等信息。 2.功能分析與設計 數據統計報表一般都采用DB的存儲過程或函數進行統計,並將數據保存到數據庫表中,提供前台應用查詢和展示。對於實時性的報表需求,建議在非業務庫上進行統計。一般主庫對外提供業務服務,通過復制等機制將業務數據存儲到專門的數據庫,或者專門的物理節點上,這樣就有效的避免了報表統計功能對正常業務的影響。 3.實現 1.首先,從用戶日統計表中將前天的登錄用戶統計到存留明細表中。 2.然後,從用戶日統計表中將昨天的登錄用戶統計出來,並將前天、昨天都登錄的用戶的存留狀態設置為1。 3.然後,從存留明細表中統計昨天用戶的存留數、存留率,並保存到存留表中。 執行統計采用了mysql的事件機制,定時觸發調用統計函數,進行數據統計。 每天凌晨4點鐘,開始統計昨天的日存留率。 event: 圖片上傳錯誤,待恢復後上傳。
Sql代碼 BEGIN #Routine body goes here... DECLARE _yesterday DATE DEFAULT NULL; DECLARE _before_yesterday DATE DEFAULT NULL; DECLARE _is_success tinyint(1) DEFAULT 0; select DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-%d'),INTERVAL 1 day) INTO _yesterday; select DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-%d'),INTERVAL 2 day) INTO _before_yesterday; #統計前天用戶數,並將前天用戶插入到report_user_remain_day_detail表中 SET _is_success = get_user_remain_day(_before_yesterday); #查詢report_user_login_day_detail,統計昨天的用戶,並將前天和昨天都登錄的用戶狀態設置為1 SET _is_success = update_user_status_remain(_yesterday); #統計存留用戶明細表,將統計數據存儲到統計表report_user_remain_day SET _is_success = insert_user_remain_day(_yesterday); RETURN _is_success; END Sql代碼 CREATE FUNCTION `get_user_remain_day`(`_day_time` date) RETURNS int(1) BEGIN #Routine body goes here... DECLARE stopFlag INT DEFAULT 0 ; DECLARE _device VARCHAR(50) DEFAULT NULL; DECLARE _a_token CHAR(64) DEFAULT '-1'; DECLARE _day_time date; DECLARE _shop_id INT(11) DEFAULT 0; #查詢當天的登錄用戶 DECLARE cur1 CURSOR FOR select shop_id, device, a_token from report_user_login_day_detail WHERE day_time = _day_time; DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopFlag=1; OPEN cur1; FETCH cur1 INTO _shop_id, _device, _a_token; WHILE stopFlag = 0 do INSERT INTO report_user_remain_day_detail(id, shop_id, day_time, device, a_token) values (UUID(), _shop_id, _day_time, _device, _a_token); FETCH cur1 INTO _shop_id, _device, _a_token; END WHILE; CLOSE cur1; RETURN 1; END Sql代碼 CREATE FUNCTION `update_user_status_remain`(`_day_time` date) RETURNS tinyint(1) BEGIN #Routine body goes here... DECLARE stopFlag INT DEFAULT 0 ; DECLARE _device VARCHAR(50) DEFAULT NULL; DECLARE _a_token CHAR(64) DEFAULT '-1'; #查詢昨天的登錄用戶 DECLARE cur1 CURSOR FOR select device, a_token from report_user_login_day_detail WHERE day_time = _day_time; DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopFlag=1; OPEN cur1; FETCH cur1 INTO _device, _a_token; WHILE stopFlag = 0 do UPDATE report_user_remain_day_detail SET status_remain = 1, day_time = _day_time WHERE device = _device; FETCH cur1 INTO _device, _a_token; END WHILE; CLOSE cur1; RETURN 1; END Sql代碼 CREATE FUNCTION `insert_user_remain_day`(`_day_time` date) RETURNS tinyint(1) BEGIN #Routine body goes here... DECLARE stopFlag INT DEFAULT 0 ; DECLARE _status_remain TINYINT(1); DECLARE _remain_count INT DEFAULT 0; DECLARE _all_count INT DEFAULT 0; DECLARE _temp_count INT DEFAULT 0; DECLARE cur2 CURSOR FOR SELECT status_remain, count(device) FROM report_user_remain_day_detail WHERE status_remain = 1 and day_time = _day_time GROUP BY status_remain; DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopFlag=1; OPEN cur2; FETCH cur2 INTO _status_remain, _remain_count; WHILE stopFlag = 0 do IF _status_remain = 1 THEN SET _temp_count = _remain_count; SET _all_count = _all_count + _remain_count; ELSE SET _all_count = _all_count + _remain_count; END IF; INSERT INTO report_user_remain_day(id, day_time, remain_count, remain_percent_day, type_client) VALUES(UUID(), _day_time, _temp_count, (_temp_count / _all_count), 0); FETCH cur2 INTO _status_remain, _remain_count; END WHILE; CLOSE cur2; RETURN 1; END