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
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e776ee-65c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e776ee-65c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?