I have a table in mysql like this
CREATE TABLE IF NOT EXISTS `connections` (
`src` int(10) unsigned NOT NULL,
`sport` smallint(5) unsigned NOT NULL,
`dst` int(10) unsigned NOT NULL,
`dport` smallint(5) unsigned NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`src`,`sport`,`dst`,`dport`,`time`),
KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.5 million records daily inserted in this table.
When i want to select records for a period of time like a day. it takes about 7 minutes. how can i improve it.
i`m using ruby on rails version 4.0.0
My selection is like this
connections = Connection.select('src, dst, UNIX_TIMESTAMP(time) as time')
.where(time: timeFrom..timeTo)
.order('time ASC')
After selection from database i have a loop like this :
connections.each do |con|
link = getServerID(con['src'])
link = getServerID(con['dst']) if link == 0
@total[link].append [con['time'] * 1000, con['dst']]
end
in this loop i have a bit process on src and dst then i add it to a hash this sections takes along and my computer crashed