How to optimize 'group by' query and indexing on big table? My table has over 300+ million records. I created a key store_idx consisting of columns (store_region, store_format, store_code). This is my query: 

select store_region,
       store_format, 
       store_code,
       count(distinct basket_id), 
       min(shop_date), 
       max(shop_date) 
from basket 
group by store_region, 
         store_format, 
         store_code 
order by store_region, 
         store_format, 
         store_code;


When I ran explain command this was the output:

+----+-------------+--------+------------+-------+---------------+-----------+---------+------+-----------+----------+-------+
| id | select_type | table  | partitions | type  | possible_keys | key       | key_len | ref  | rows      | filtered | Extra |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+-----------+----------+-------+
|  1 | SIMPLE      | basket | NULL       | index | store_idx     | store_idx | 3069    | NULL | 305100051 |   100.00 | NULL  |
+----+-------------+--------+------------+-------+---------------+-----------+---------+------+-----------+----------+-------+ 


This query is taking so long to run. How can I optimize?
Calculating three aggregates on a 300 million row table without a filter is going to take some time, especially if the rows have to read from disk. Without knowing anything about types and indexes, it is difficult to give any specific advice, but you may try to replace store_idx with an index like:

CREATE INDEX store_idx ON basket
    (store_region, store_format, store_code, basket_id, shop_date);