0

请帮我优化下面的查询,这个查询在ISNULL条件下执行需要超过60 分钟,但是如果我们将ISNULL替换为<=>条件,它需要15 分钟才能执行,但我们的期望是这个查询应该最多需要 2 分钟。

两个表都有:- 3880494 条记录。

SELECT hs.headendid FROM headendlineups_stagging hs LEFT JOIN headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid WHERE ISNULL(hlp.headendid)

将 ISNULL 替换为 <=> 运算符

SELECT hs.headendid,hlp.headendid AS productionheadend
FROM headendlineups_stagging hs  
LEFT JOIN  headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
-- WHERE ISNULL(hlp.headendid)
WHERE hlp.headendid <=> NULL;

解释两个查询的简单明了

**mysql> EXPLAIN
    ->  SELECT hs.headendid
    ->  FROM headendlineups_stagging hs
    ->  LEFT JOIN headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
    ->  WHERE ISNULL(hlp.headendid)\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: hs
   partitions: NULL
         type: index
possible_keys: NULL
          key: IX_lineups_headendid
      key_len: 198
          ref: NULL
         rows: 3854774
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: hlp
   partitions: NULL
         type: ref
possible_keys: IX_lineups_headendid,iDX_linups_NEW,New_headends_linup
          key: IX_lineups_headendid
      key_len: 153
          ref: onconnectdb.hs.HeadendId
         rows: 217
     filtered: 100.00
        Extra: Using where; Using index
2 rows in set, 1 warning (0.00 sec)**


mysql> EXPLAIN
    -> SELECT hs.headendid,hlp.headendid AS productionheadend
    -> FROM headendlineups_stagging hs
    -> LEFT JOIN  headendlineups_23march2017 hlp ON hs.headendid=hlp.headendid
    -> WHERE hlp.headendid <=> NULL\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: hs
   partitions: NULL
         type: index
possible_keys: NULL
          key: IX_lineups_headendid
      key_len: 198
          ref: NULL
         rows: 3854774
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: hlp
   partitions: NULL
         type: ref
possible_keys: IX_lineups_headendid,iDX_linups_NEW,New_headends_linup
          key: IX_lineups_headendid
      key_len: 153
          ref: onconnectdb.hs.HeadendId
         rows: 217
     filtered: 100.00
        Extra: Using where; Using index
2 rows in set, 1 warning (0.00 sec)

请帮助我了解两个查询是否扫描相同数量的记录,那么为什么两个查询的执行时间不同?并请建议编写上述查询的更好方法。

4

1 回答 1

0

时间上的差异很可能是缓存;再次运行每个。

更改LEFT JOINNOT EXISTS ( SELECT * FROM headendlineups_23march2017 hlp WHERE hs.headendid=hlp.headendid ) (它可能会或可能不会有帮助。

基于EXPLAINs,它正在尽其所能 - 即Using index在两个表上。面对现实,你必须扫描整个第一个表 380 万次,然后检查第二个表 380 万次。

多少内存?的价值是innodb_buffer_pool_size多少? SHOW TABLE SIZE(每个);我想比较缓冲池的大小。

于 2017-04-01T00:49:05.267 回答