请帮我优化下面的查询,这个查询在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)
请帮助我了解两个查询是否扫描相同数量的记录,那么为什么两个查询的执行时间不同?并请建议编写上述查询的更好方法。