The optimization problem of group by sentence in mysql and how to find the train of thought

I want to optimize a sentence about group by. I don"t know where to start. I want to find a train of thought

.

SELECT

adv_id,
network_id,
SUM(re_click) AS clk,
SUM(deny_click) AS dny_clk,
SUM(re_conver) AS re_ins,
SUM(mat_conver) AS mat_ins,
SUM(def_conver) AS def,
SUM(cost) AS cost,
SUM(earn) AS earn 

FROM ams_data_hourly_2018_03 WHERE start_time BETWEEN 1519862400 AND 1519948799
GROUP BY

adv_id,
network_id

explain:

clipboard.png

:

clipboard.png

ask for advice

Feb.27,2021

try adding FORCE INDEX start_time_adv_id_networkid

define start_time_adv_id_networkid as (start_time, adv_id, network_id)
adv_id comes before network_id, because adv_id precedes when you group.


build a composite index for adv_id,network_id, do not start with start_time, which does not meet the principle of "leftmost match".


1. If the time search result set is small, just one start_time single-column index is enough. Force the index, because group by will guide mysql to follow the index of the group by field or scan all of it directly.
2. Look at the name of the table. You should be a sub-table. If the time range directly covers most of the table data, you don't need any index. Scan the whole table. It is useless to have three fields and two fields plus index. The composite index of time range plus group by field only uses the time field, and only the index of group by field scans the whole table. Unless you override


adv_id and network_id federated index

with an index
Menu