具有许多连接的Mysql Bad Query性能

时间:2013-10-01 12:45:38

标签: mysql performance

我有一个查询需要几分钟才能在一个小数据集上执行,问题是什么?这是解释输出:

id  select_type table           type    possible_keys               key                 key_len   ref             rows  Extra

1   SIMPLE      BT_CALL_CLASS.. const   PRIMARY                     PRIMARY             4         const           1     Using index; Using temporary; Using filesort
1   SIMPLE      BT_FLAGS_FLAGS  const   PRIMARY                     PRIMARY             4         const           1     Using index
1   SIMPLE      BT_DIRECTORY... range   PRIMARY                     PRIMARY             4         NULL            308   Using where; Using index
1   SIMPLE      BT_CALL_FLAGS.. ref     PRIMARY,FKDF68A2ED9F150002  FKDF68A2ED9F150002  4         const           49    Using where; Using index
1   SIMPLE      BT_DEPARTMENT.. index   PRIMARY                     FK1F3A276188DDBD3   5         NULL            27    Using where; Using index; Using join buffer
1   SIMPLE      BT_USERS_USER.. index   PRIMARY                     FK6A68E086C27A155   5         NULL            233   Using where; Using index; Using join buffer
1   SIMPLE      BT_FCT_CALLS..  eq_ref  PRIMARY,FKDF68A2ED4F28..    PRIMARY             8         ...call_id      1     Using where

查询

 desc SELECT DISTINCT 
      BT_FCT_CALLS_FCT_CALLS.start_time AS COL0
     ,BT_FCT_CALLS_FCT_CALLS.calling_number AS COL1
     ,BT_FCT_CALLS_FCT_CALLS.called_number AS COL2
     ,BT_FCT_CALLS_FCT_CALLS.response AS COL3

 FROM departments BT_DEPARTMENTS_DEPARTMENTS_3 LEFT OUTER JOIN 
 ( 
  directory_numbers BT_DIRECTORY_NUMBERS_DIRECTORY_NUMBERS LEFT OUTER JOIN 
  ( 
   users BT_USERS_USERS_2 JOIN 
   ( 
    call_classification_dim BT_CALL_CLASSIFICATION_DIM_CALL_CLASSIFICATION_DIM JOIN 
    ( 
     flags BT_FLAGS_FLAGS JOIN 
     ( 
      fct_calls BT_FCT_CALLS_FCT_CALLS JOIN call_flags BT_CALL_FLAGS_CALL_FLAGS
      ON ( BT_FCT_CALLS_FCT_CALLS.id = BT_CALL_FLAGS_CALL_FLAGS.call_id )
     ) 
     ON ( BT_CALL_FLAGS_CALL_FLAGS.flag = BT_FLAGS_FLAGS.id AND (  BT_FLAGS_FLAGS.id  = 1 )  )
    ) 
    ON ( BT_FCT_CALLS_FCT_CALLS.call_direction_id = BT_CALL_CLASSIFICATION_DIM_CALL_CLASSIFICATION_DIM.id AND (  BT_CALL_CLASSIFICATION_DIM_CALL_CLASSIFICATION_DIM.id  = 2 )  )
   ) 
   ON ( (( BT_FCT_CALLS_FCT_CALLS.on_network_called_user_id  =  BT_USERS_USERS_2.id ) OR ( BT_FCT_CALLS_FCT_CALLS.on_network_calling_user_id  =  BT_USERS_USERS_2.id )) AND TRUE   )
  ) 
  ON ( (( BT_FCT_CALLS_FCT_CALLS.on_network_called_ext_id  =  BT_DIRECTORY_NUMBERS_DIRECTORY_NUMBERS.id ) OR ( BT_FCT_CALLS_FCT_CALLS.on_network_calling_ext_id  =  BT_DIRECTORY_NUMBERS_DIRECTORY_NUMBERS.id )) AND TRUE )
 ) 
 ON ( (( BT_FCT_CALLS_FCT_CALLS.on_network_called_department_id  =  BT_DEPARTMENTS_DEPARTMENTS_3.id ) OR ( BT_FCT_CALLS_FCT_CALLS.on_network_calling_department_id  =  BT_DEPARTMENTS_DEPARTMENTS_3.id )) AND TRUE )

  WHERE 
    (
      (
          NOT( BT_DEPARTMENTS_DEPARTMENTS_3.id  IN ( 1 ) )
      )
  AND (
          NOT( BT_DIRECTORY_NUMBERS_DIRECTORY_NUMBERS.id  IN ( 1 ) )
      )

  AND (
          NOT( BT_USERS_USERS_2.id  IN ( 1 ) )
      )
    )
ORDER BY 
      COL0

3 个答案:

答案 0 :(得分:0)

首先将WHERE子句更改为:

 WHERE 
 BT_DEPARTMENTS_DEPARTMENTS_3.id  <> 1 AND 
 BT_DIRECTORY_NUMBERS_DIRECTORY_NUMBERS.id  <> 1 AND 
 BT_USERS_USERS_2.id <> 1

比使用NOT IN(..)

更快

删除不需要的AND TRUE

答案 1 :(得分:0)

首先,让我们从编写得非常糟糕的查询流程开始。通过简化直接关系与所有嵌套可以帮助清除一些事情。其次,在树上显示事物相关的关系。你正在全身反弹。为了便于阅读,我还缩短了“别名”。最后,您不需要所有表,因为您没有从所有表中获取使许多事情变得无用的值。无论如何,这是原始查询的清理版本。如您所见,更容易看到表A链接到B链接到D等的C链接。

SELECT DISTINCT 
      calls.start_time AS COL0,
      calls.calling_number AS COL1,
      calls.called_number AS COL2,
      calls.response AS COL3
   FROM 
      fct_calls calls 
         JOIN departments dpt3 
            ON calls.on_network_called_department_id = dpt3.id
            OR calls.on_network_calling_department_id = dpt3.id
         JOIN users u2 
            ON calls.on_network_called_user_id = u2.id
            OR calls.on_network_calling_user_id = u2.id
         JOIN directory_numbers dirNums 
            ON calls.on_network_called_ext_id = dirNums.id 
            OR calls.on_network_calling_ext_id = dirNums.id 
         JOIN call_classification_dim classDim 
            ON calls.call_direction_id = classDim.id 
            AND classDim.id = 2
         JOIN call_flags callFlags
            ON calls.id = callFlags.call_id
            JOIN flags f 
               ON callFlags.flag = f.id 
               AND f.id = 1
  WHERE
         dpt3.id <> 1
     AND dirNums.id <> 1
     AND  u2.id = 1
  ORDER BY 
     COL0

现在,这是我将要运行的内容。同样,甚至没有使用很多东西,例如调用标志和目录号。从通过呼叫或被叫位置获得您感兴趣的用户ID的所有呼叫开始。我会在调用表上有一个索引(on_network_called_user_id,on_network_calling_user_id)。我假设其他表在各自的主“ID”列上都有索引。

SELECT DISTINCT 
      calls.start_time AS COL0,
      calls.calling_number AS COL1,
      calls.called_number AS COL2,
      calls.response AS COL3
   FROM 
      fct_calls calls 
         JOIN departments dpt3 
            ON calls.on_network_called_department_id = dpt3.id
            OR calls.on_network_calling_department_id = dpt3.id
         JOIN directory_numbers dirNums 
            ON calls.on_network_called_ext_id = dirNums.id 
            OR calls.on_network_calling_ext_id = dirNums.id 
  WHERE
     (    calls.on_network_called_user_id = 1
       OR calls.on_network_calling_user_id = 1 )
     AND dpt3.id <> 1
     AND dirNums.id <> 1
  ORDER BY 
     COL0

更进一步,你可以通过考虑呼叫或被叫来做一个UNION来获得更好的性能,你可以尝试以下方法。因此,您正在寻找任何呼叫,其中进行呼叫的人是用户ID 1并且被呼叫的部门不是1且目录扩展名不是1 ...或者,用户ID 1正在被呼叫,但忽略来自部门1和扩展名1。

我会在调用表上有两个索引 这个是谁被呼叫,以及从哪里打电话 (on_network_called_user_id,on_network_calling_department_id,on_network_calling_ext_id) 反过来......他们正在打电话,他们打电话给部门/分机 (on_network_calling_user_id,on_network_called_department_id,on_network_called_ext_id)

SELECT
      calls.start_time AS COL0,
      calls.calling_number AS COL1,
      calls.called_number AS COL2,
      calls.response AS COL3
   FROM 
      fct_calls calls 
         LEFT JOIN departments dpt3 
            OR calls.on_network_calling_department_id = dpt3.id
         LEFT JOIN directory_numbers dirNums 
            OR calls.on_network_calling_ext_id = dirNums.id 
   WHERE
          calls.on_network_called_user_id = 1
      AND calls.on_network_calling_department_id <> 1
      AND calls.on_network_calling_ext_id <> 1
   ORDER BY 
      COL0
UNION
SELECT
      calls.start_time AS COL0,
      calls.calling_number AS COL1,
      calls.called_number AS COL2,
      calls.response AS COL3
   FROM 
      fct_calls calls 
         LEFT JOIN departments dpt3 
            OR calls.on_network_called_department_id = dpt3.id
         LEFT JOIN directory_numbers dirNums 
            OR calls.on_network_called_ext_id = dirNums.id 
   WHERE
          calls.on_network_calling_user_id = 1
      AND calls.on_network_called_department_id <> 1
      AND calls.on_network_called_ext_id <> 1

答案 2 :(得分:0)

正如我在评论中所说的那样,查询是自动生成的,无论如何,在升级到mysql 5.6.14后我无法修改它们,并且对查询本身没有任何改变,性能变为正常(与具有类似复杂性的其他查询相比)!