sql - Mysql Group by time interval optimization -
i have large table (several hundred millions of rows) stores test results along datetime , foreign key related entity called 'link', need to group rows time intervals of 10,15,20,30 , 60 minutes filter time , 'link_id' know can done query explained [here][1]:
select time,avg(rtt),min(rtt),max(rtt),count(*) trace link_id=1 , time>='2015-01-01' , time <= '2015-01-30' group unix_timestamp(time) div 600;
this solution worked extremely slow (about 10 on average) tried adding datetime column each 'group interval' example row:
id | time | rtt | link_id 1 | 2014-01-01 12:34:55.4034 | 154.3 | 2
became:
id | time | rtt | link_id | time_60 |time_30 ... 1 | 2014-01-01 12:34:55.4034 | 154.3 | 2 | 2014-01-01 12:00:00.00 | 2014-01-01 12:30:00.00 ...
and intervals following query:
select time_10,avg(rtt),min(rtt),max(rtt),count(*) trace link_id=1 , time>='2015-01-01' , time <= '2015-01-30' group time_10;
this query @ least 50% faster (about 5 seconds on average) still pretty slow, how can optimize query faster?
explain query outputs this:
+----+-------------+------------+------+------------------------------------------------------------------------+----------------------------------------------------+---------+-------+---------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | | +----+-------------+------------+------+------------------------------------------------------------------------+----------------------------------------------------+---------+-------+---------+----------------------------------------------+ | 1 | simple | main_trace | ref | main_trace_link_id_c6febb11f84677f_fk_main_link_id,main_trace_e7549e3e | main_trace_link_id_c6febb11f84677f_fk_main_link_id | 4 | const | 1478359 | using where; using temporary; using filesort | +----+-------------+------------+------+------------------------------------------------------------------------+----------------------------------------------------+---------+-------+---------+----------------------------------------------+
and these table indexes:
+------------+------------+----------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | table | non_unique | key_name | seq_in_index | column_name | collation | cardinality | sub_part | packed | null | index_type | comment | index_comment | +------------+------------+----------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | main_trace | 0 | primary | 1 | id | | 2956718 | null | null | | btree | | | | main_trace | 1 | main_trace_link_id_c6febb11f84677f_fk_main_link_id | 1 | link_id | | 2 | null | null | | btree | | | | main_trace | 1 | main_trace_07cc694b | 1 | time | | 2956718 | null | null | | btree | | | | main_trace | 1 | main_trace_e7549e3e | 1 | time_10 | | 22230 | null | null | yes | btree | | | | main_trace | 1 | main_trace_01af8333 | 1 | time_15 | | 14783 | null | null | yes | btree | | | | main_trace | 1 | main_trace_1681ff94 | 1 | time_20 | | 10870 | null | null | yes | btree | | | | main_trace | 1 | main_trace_f7c28c93 | 1 | time_30 | | 6399 | null | null | yes | btree | | | | main_trace | 1 | main_trace_0f29fcc5 | 1 | time_60 | | 3390 | null | null | yes | btree | | | +------------+------------+----------------------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
for query:
select time_10, avg(rtt), min(rtt), max(rtt), count(*) trace link_id = 1 , time >= '2015-01-01' , time <= '2015-01-30' group time_10;
the best index covering index: trace(link_id, time, time_10, rtt)
.
Comments
Post a Comment