mysql - Calculate difference between 2 epoch times, large table, optimize having 3 indexes -
i have table represents packets traffic capture called packetsbydirection (showing relevant fields):
framenumber frametimeepoch flowid direction 288 1430221042.150789000 29 direction 289 1430221042.150922000 29 direction b
now, table has around 2 million rows (packets) , need calculate is, each packet, difference of time between him , previous packet with same direction , same flowid
i have done this query, plus adding indexes previous table make query faster.
select t1.framenumber, flowid, direction, frametimeepoch - ifnull(( select max(frametimeepoch) packetsbydirection flowid = t1.flowid , direction t1.direction , framenumber < t1.framenumber) ,frametimeepoch) timefromlastpacketfromsamedirection packetsbydirection t1
and result
framenumber flowid direction timefromlastpacketfromsamedirection 288 29 direction 0 289 29 direction b 0 290 29 direction 5.422 291 29 direction b 4.356 292 30 direction 0 293 30 direction 1.302
and on. now, query takes around 1 hour 600k rows, , i'm working millions of rows don't want try it. here "explain" output of query right (that's lot of iterations):
so question is, there more efficient way this?
thanks
edit: here definition of table
create table `packetsbydirection` ( `framenumber` int(11) not null default '0', `frametimeepoch` varchar(45) not null, `ipsrc` varchar(45) not null, `tcpsrcport` varchar(45) default null, `udpsrcport` varchar(45) default null, `ipdst` varchar(45) not null, `tcpdstport` varchar(45) default null, `udpdstport` varchar(45) default null, `iplength` varchar(45) not null, `flowid` int(11) not null, `direction` varchar(11) character set utf8 default null, key `index2` (`direction`), key `index3` (`flowid`), key `index4` (`framenumber`) ) engine=innodb default charset=latin1
not sure if work @ all, maybe running numbers faster?
select framenumber, case when flowid <> @currflow or direction <> @currdir @diff := 0 else @diff := frametimeepoch - @epoch end timefromlastpacketfromsamedirection , @currflow := flowid, @currdir := direction, @diff, @epoch := frametimeepoch packetsbydirection, (select @epoch := 0, @currflow :="", @currdir := "", @diff := 0) tmp order flowid, direction, frametimeepoch
Comments
Post a Comment