简体   繁体   English

有人可以帮我优化这个查询吗?

[英]Can someone help me to optimizing this query?

I have the below query that takes so much time >> table size 64.31GB are there any rewrite possible for this?我有下面的查询需要这么多时间>>表大小 64.31GB 是否有任何重写可能?

SELECT MIN(ut.id) AS minUT, 
       MAX(ut.id) AS maxUT
FROM user_tasks ut
    JOIN user_tasks_metadata utm ON utm.user_task_id = ut.id
    JOIN ue_events_base ue ON ue.usr_task_id = ut.id
    JOIN batch_records br ON ut.id = br.user_task_id
WHERE ut.id > (
                SELECT IFNULL(MAX(assp.user_task_id_end) , 0) 
                FROM app_summary_snapshot_points assp
              )
AND br.created_at < (current_timestamp() - INTERVAL 10 MINUTE) 
AND br.is_subscription_updated = 1 
HAVING minUT > 0 and maxUT > 0;

the number of row scans行扫描次数

*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: ue
   partitions: NULL
         type: range
possible_keys: ue_events_user_task_id_events
          key: ue_events_user_task_id_events
      key_len: 8
          ref: NULL
         rows: 1722
     filtered: 100.00
        Extra: Using where; Using index
*************************** 2. row ***************************
           id: 1
  select_type: PRIMARY
        table: br
   partitions: NULL
         type: ref
possible_keys: batch_created_at,batch_user_task
          key: batch_user_task
      key_len: 9
          ref: ue_stage.ue.usr_task_id
         rows: 1
     filtered: 5.00
        Extra: Using where
*************************** 3. row ***************************
           id: 1
  select_type: PRIMARY
        table: ut
   partitions: NULL
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 8
          ref: ue_stage.ue.usr_task_id
         rows: 1
     filtered: 100.00
        Extra: Using index
*************************** 4. row ***************************
           id: 1
  select_type: PRIMARY
        table: utm
   partitions: NULL
         type: ref
possible_keys: usertask_fk_idx,id_asi
          key: id_asi
      key_len: 8
          ref: ue_stage.ue.usr_task_id
         rows: 1
     filtered: 100.00
        Extra: Using index

the br table does not have some combo index but is there any rewrite possible for this query? br 表没有一些组合索引,但是这个查询是否可以重写?

Your WHERE filter for your batch_records table looks for您的batch_records表的 WHERE 过滤器查找

AND br.created_at < (current_timestamp() - INTERVAL 10 MINUTE) 
AND br.is_subscription_updated = 1 

Therefore, a compound (multicolumn) covering index, like this, will help with performance.因此,像这样的复合(多列)覆盖索引将有助于提高性能。

ALTER TABLE batch_records ADD INDEX upd_cre_uti
        (is_subscription, created_at DESC, user_task_id);

MySQL can random-access that index to the first eligible row... the one with the correct value of is_subscription and the largest created_at . MySQL 可以将该索引随机访问到第一个符合条件的行......具有正确值is_subscription和最大created_at的那个。 It can then scan the index sequentially to the last eligible row.然后它可以按顺序扫描索引到最后一个符合条件的行。 While it is scanning it can fetch user_task_id from the index.在扫描时,它可以从索引中获取user_task_id

I don't think there's a useful query refactoring that can fix your performance problem: indexes are the canonical way to address this kind of problem.我认为没有有用的查询重构可以解决您的性能问题:索引是解决此类问题的规范方法。

br:  (is_subscription_updated, created_at, user_task_id)
assp:  (user_task_id_end)
utm:  INDEX(user_task_id)
ue:  INDEX(usr_task_id)

Do you really need utm and ue in the list of Joins?你真的需要 utm 和 ue 在 Joins 列表中吗?

What is the kludge with MIN, MAX and IFNULL? MIN、MAX 和 IFNULL 的组合是什么? (There may be a better way to achieve your goal.) (可能有更好的方法来实现您的目标。)

Please provide EXPLAIN SELECT... after making the changes.请在进行更改后提供EXPLAIN SELECT...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM