query挂起“创建排序索引”

q35jwt9p  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(232)

以前有一个运行一分钟的查询,现在在使用稍微不同的模式切换到新表后需要将近一个小时。
查询:

select 
    date(datetime_field)
    ,str_1
    ,str_2
    ,str_3
    ,str_4
    ,dayname(datetime_field)
    ,str_5
    ,date(convert_tz(datetime_field, 'Timezone', t.timezone))
    ,time(convert_tz(cpt_datetime, 'Timezone', t.timezone))
    ,sum_field_1
    ,sum_field_2 
from 
(select 
    concat_ws(' ', date_date, date_time) as datetime_field
    ,str_1
    ,str_2
    ,str_3
    ,str_4
    ,ceiling(sum(agg_field1)) as sum_field_1
    ,ceiling(sum(agg_field2)) as sum_field_2 
from raw_table f 
force index(raw_table_index) 
join lookup_table c on f.str_3 = c.str_3 and f.id_2 = c.id_2 
where str_3 = 'VALUE' 
and id_1 = 0 
and f.id_2 in ('val1','val2') 
and date_date >= '2018-04-12' 
group by concat_ws(' ', date_date, date_time)
,str_1
,str_2
,str_3
,str_4
,c.str_5) 
fcst left join timezones_table t on fcst.str_2 = t.str_2;

该表具有以下索引:

ALTER TABLE `raw_table` ADD INDEX raw_table_index (id_2, id_1, str_3, date_date);

explain显示它正在使用表上的索引:


***************************4. row***************************

           id: 2
  select_type: DERIVED
        table: f
         type: ref
possible_keys: raw_table_index
          key: raw_table_index
      key_len: 171
          ref: const,const,func
         rows: 414
        Extra: Using index condition

以及

id: 1
  select_type: PRIMARY
        table: <derived2>
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 38088
        Extra: NULL

所以在一张40米的table上,它过滤到38088+
查询处于“发送数据”状态约128秒,然后在“创建排序索引”状态下花费近2小时。我用了不同的指数,没有看到任何改善。当前索引与旧表上的索引非常相似。不知道我是不是在做傻事,只是错过了什么?有什么想法吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题