Implementation of grouping statistics for MYSQL every 10 minutes.
Preface
The content of this article mainly introduces the implementation method of MYSQL grouping statistics every 10 minutes. It will be very useful in drawing the user login and operation conditions in a distribution chart within one day, previously, I only knew how to use stored procedures (although the execution speed is fast, it is really too inflexible ), later, I learned to use the "group by" method of the advanced point to flexibly implement similar functions.
Body:
-- Time_str '2017-11-20 04:31:11 '-- date_str 2016 select concat (left (date_format (time_str,' % y-% m-% d % h: % I '), 15), '0') as time_flag, count (*) as count from 'security '. 'COMMAND _ info' where 'date _ str' = 20161120 group by time_flag order by time_flag; -- 127 rowsselect round (unix_timestamp (time_str)/(10*60) as timekey, count (*) from 'security '. 'COMMAND _ info' where 'date _ str' = 20161120 group by timekey order by timekey; -- 126 rows -- the above two SQL statements have a similar idea -- use "group by" to differentiate, but the method is different. The former can only be at the 10 minute (OR 1 hour) level, the latter can dynamically adjust the interval. The efficiency of the two is similar. You can select concat (date (time_str), '', hour (time_str ),':', round (minute (time_str)/10, 0) * 10), count (*) from 'security '. 'COMMAND _ info' where 'date _ str' = 20161120 group by date (time_str), hour (time_str), round (minute (time_str)/) * 10; -- 145 rowsselect concat (date (time_str), '', hour (time_str), ':', floor (minute (time_str)/10) * 10), count (*) from 'security '. 'COMMAND _ info' where 'date _ str' = 20161120 group by date (time_str), hour (time_str), floor (minute (time_str)/10) * 10; -- 127 rows (equivalent to date_format) select concat (date (time_str), '', hour (time_str), ':', ceil (minute (time_str)/10) * 10), count (*) from 'security '. 'cmd _ info' where 'date _ str' = 20161120 group by date (time_str), hour (time_str), ceil (minute (time_str)/10) * 10; -- 151 rows
&
DELIMITER //DROP PROCEDURE IF EXISTS `usp_cmd_info`;CREATE PROCEDURE `usp_cmd_info`(IN dates VARCHAR(12))BEGIN SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 00:00:00") AND CONCAT(dates, " 00:10:00") INTO @count_0; SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 00:10:00") AND CONCAT(dates, " 00:20:00") INTO @count_1; ... SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 23:40:00") AND CONCAT(dates, " 23:50:00") INTO @count_142; SELECT count(*) from `cmd_info` where `time_str` BETWEEN CONCAT(dates, " 23:50:00") AND CONCAT(dates, " 23:59:59") INTO @count_143; select @count_0, @count_1, @count_2, @count_3, @count_4, @count_5, @count_6, @count_7, @count_8, @count_9, @count_10, @count_11, @count_12, @count_13, @count_14, @count_15, @count_16, @count_17, @count_18, @count_19, @count_20, @count_21, @count_22, @count_23, @count_24, @count_25, @count_26, @count_27, @count_28, @count_29, @count_30, @count_31, @count_32, @count_33, @count_34, @count_35, @count_36, @count_37, @count_38, @count_39, @count_40, @count_41, @count_42, @count_43, @count_44, @count_45, @count_46, @count_47, @count_48, @count_49, @count_50, @count_51, @count_52, @count_53, @count_54, @count_55, @count_56, @count_57, @count_58, @count_59, @count_60, @count_61, @count_62, @count_63, @count_64, @count_65, @count_66, @count_67, @count_68, @count_69, @count_70, @count_71, @count_72, @count_73, @count_74, @count_75, @count_76, @count_77, @count_78, @count_79, @count_80, @count_81, @count_82, @count_83, @count_84, @count_85, @count_86, @count_87, @count_88, @count_89, @count_90, @count_91, @count_92, @count_93, @count_94, @count_95, @count_96, @count_97, @count_98, @count_99, @count_100, @count_101, @count_102, @count_103, @count_104, @count_105, @count_106, @count_107, @count_108, @count_109, @count_110, @count_111, @count_112, @count_113, @count_114, @count_115, @count_116, @count_117, @count_118, @count_119, @count_120, @count_121, @count_122, @count_123, @count_124, @count_125, @count_126, @count_127, @count_128, @count_129, @count_130, @count_131, @count_132, @count_133, @count_134, @count_135, @count_136, @count_137, @count_138, @count_139, @count_140, @count_141, @count_142, @count_143;END //DELIMITER ;show PROCEDURE status\GCALL usp_cmd_info("2016-10-20");
The above MySQL Stored Procedure statement is very long and cannot be input manually. You can use the following Python code to automatically generate at the required time interval:
Import datetimetoday = datetime. date. today () # Or convert from a given string to # today = datetime. datetime. strptime ('1970-11-21 ',' % Y-% m-% D') min_today_time = datetime. datetime. combine (today, datetime. time. min) #2016-11-21 00: 00: 00max_today_time = datetime. datetime. combine (today, datetime. time. max) #23: 59: 59sql_procedure_arr = [] SQL _procedure_arr2 = [] for x in xrange (0, 60*24/5, 1): start_datetime = min_today_time + datetime. timedelta (minutes = 5 * x) end_datetime = min_today_time + datetime. timedelta (minutes = 5 * (x + 1) # print x, start_datetime.strftime ("% Y-% m-% d % H: % M: % S "), end_datetime.strftime ("% Y-% m-% d % H: % M: % S") select_str = 'select count (*) from 'COMMAND _ info' where 'time _ str' BETWEEN "{0}" AND "{1}" INTO @ count _ {2 };'. format (start_datetime, end_datetime, x) # print select_str SQL _procedure_arr.append (select_str) SQL _procedure_arr2.append ('@ count _ {0 }'. format (x) print '\ n '. join (SQL _procedure_arr) print 'select {0 };'. format (','. join (SQL _procedure_arr2 ))
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.