有没有办法提高mysql事件执行的性能?

时间:2015-06-24 15:19:44

标签: mysql performance events

我在一个名为flow的表中每天有一个拥有2百万个数据(行/天)的MySQL数据库。我编写了一个事务来读取和汇总此表中的数据,以写入临时表,这里称为top_tcp_ports和top_udp_ports。当我在Mysql控制台上运行以下事务查询时,通常需要30秒或更少时间来处理并将数据插入到这两个表(top_tcp_ports& top_udp_ports)。但相反,当MySQL事件调度程序运行此事务时,处理整个事务需要7分钟。此活动每1小时运行一次。这里的性能和时间正在考虑中。

DELIMITER |
    CREATE EVENT IF NOT EXISTS top_tcp_udp_ports_hourly
    ON SCHEDULE EVERY 1 HOUR starts CURRENT_TIMESTAMP  
    DO
    BEGIN
        INSERT INTO messages(message,created_at) VALUES('Start Event Execution Log ....',NOW());

        CREATE TABLE IF NOT EXISTS top_tcp_ports
        (
        time_stamp datetime,
        tcp_port mediumint(8) signed,
        octetsTotal bigint(20) unsigned,
        packetTotal bigint(20) unsigned
        )ENGINE=MyISAM;

        CREATE TABLE IF NOT EXISTS top_udp_ports
        (
        time_stamp datetime,
        udp_port mediumint(8) signed,
        octetsTotal bigint(20) unsigned,
        packetTotal bigint(20) unsigned
        )ENGINE=MyISAM;

        start transaction;

        SET @v1 :=  (select max(saved_max_id ) from saved_id);

        INSERT INTO top_tcp_ports (time_stamp, tcp_port, octetsTotal, packetTotal)
        SELECT (timestamp(res2.date, maketime(res2.hour,00,00))), res2.tcp_port, res2.octetsTotal, res2.packetTotal from
        (SELECT dt as date, hr as hour, tcp_port, sum(byt) as octetsTotal, sum(pkt) as packetTotal from
            (
                (SELECT -1 as tcp_port, sum(packetTotalCount+reversePacketTotalCount) as pkt, sum(octetTotalCount+reverseOctetTotalCount) as byt, date(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as dt, hour(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as hr FROM flows where protocol = 6 and CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')<DATE_FORMAT(now(),'%Y-%m-%d %H:00:00') and (SELECT @v1) < id group by dt, hr)
            union
                (SELECT srcport as tcp_port, sum(packetTotalCount+reversePacketTotalCount) as pkt, sum(octetTotalCount+reverseOctetTotalCount) as byt, date(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as dt, hour(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as hr FROM flows where protocol = 6 and CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')<DATE_FORMAT(now(),'%Y-%m-%d %H:00:00') and (SELECT @v1) < id group by srcport, dt, hr having byt > 1048576)
            union
                (SELECT  dstport as tcp_port, sum(packetTotalCount+reversePacketTotalCount) as pkt, sum(octetTotalCount+reverseOctetTotalCount) as byt, date(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as dt, hour(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as hr FROM flows where protocol = 6 and CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')<DATE_FORMAT(now(),'%Y-%m-%d %H:00:00') and (SELECT @v1) < id group by dstport, dt, hr having byt > 1048576)
            ) as res
        group by tcp_port, dt, hr) as res2;
        INSERT INTO messages(message,created_at) VALUES('Mid_1 Event Execution Log ....',NOW());

        INSERT INTO top_udp_ports (time_stamp, udp_port, octetsTotal, packetTotal)
        SELECT (timestamp(res3.date, maketime(res3.hour,00,00))), res3.udp_port, res3.octetsTotal, res3.packetTotal from
        (SELECT dt as date, hr as hour, udp_port, sum(byt) as octetsTotal, sum(pkt) as packetTotal from
            (
                (SELECT -1 as udp_port, sum(packetTotalCount+reversePacketTotalCount) as pkt, sum(octetTotalCount+reverseOctetTotalCount) as byt, date(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as dt, hour(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as hr FROM flows where protocol = 17 and CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')<DATE_FORMAT(now(),'%Y-%m-%d %H:00:00') and (SELECT @v1) < id group by dt, hr)
            union
                (SELECT srcport as udp_port, sum(packetTotalCount+reversePacketTotalCount) as pkt, sum(octetTotalCount+reverseOctetTotalCount) as byt, date(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as dt, hour(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as hr FROM flows where protocol = 17 and CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')<DATE_FORMAT(now(),'%Y-%m-%d %H:00:00') and (SELECT @v1) < id group by srcport,dt,hr having byt > 1048576)
            union
                (SELECT  dstport as udp_port, sum(packetTotalCount+reversePacketTotalCount) as pkt, sum(octetTotalCount+reverseOctetTotalCount) as byt, date(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as dt, hour(CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')) as hr FROM flows where protocol = 17 and CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')<DATE_FORMAT(now(),'%Y-%m-%d %H:00:00') and (SELECT @v1) < id group by dstport,dt,hr having byt > 1048576)
            ) as res
        group by udp_port, dt, hr) as res3;
        INSERT INTO messages(message,created_at) VALUES('Mid_2 Event Execution Log ....',NOW());

        SET SQL_SAFE_UPDATES = 0;
        update saved_id set saved_max_id=IFNULL((select max(id) from flows where id> (SELECT @v1) and CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM')<DATE_FORMAT(now(),'%Y-%m-%d %H:00:00')),(SELECT @v1)) where row_id=1;

        commit; 

        INSERT INTO messages(message,created_at) VALUES('End Event Execution Log ....',NOW());

    END |
    DELIMITER ;

此处的消息表用于跟踪事件级别的过程。

  

问题是如何提高MySQL事件的性能   安排或提升它?

     

有没有身体出现这个问题?

1 个答案:

答案 0 :(得分:1)

我怀疑@v1是问题所在。请在存储过程中添加EXPLAIN SELECT ... 以查看它是如何工作的那里。然后在外面 SP做同样的事情。我猜测EXPLAINs会有所不同,希望能提供丰富的信息。

这会加快速度吗?

CONVERT_TZ(flowEndMilliseconds,'UTC','SYSTEM') <
    DATE_FORMAT(now(), '%Y-%m-%d %H:00:00')
-->
flowEndMilliseconds < CONVERT_TZ(NOW(), 'SYSTEM', 'UTC')

AND ( SELECT  @v1) < id

加上

INDEX(protocol, flowEndMilliseconds)
-->
AND @v1 < id