在这里/哪里有用?

Hai*_*ood 1 mysql

我的查询看起来像

SELECT to_number FROM sent_texts 
WHERE to_number NOT IN(SELECT mobile FROM action_6_members);
Run Code Online (Sandbox Code Playgroud)

查询完成,WHERE应用于结果集.
如果包含子查询,效果是什么(改进/降级)

WHERE mobile = to_number
Run Code Online (Sandbox Code Playgroud)

查询期间将HAVING应用于结果集.
如果包含子查询,效果是什么(改进/降级)

HAVING mobile = to_number
Run Code Online (Sandbox Code Playgroud)

使用原始查询有哪些优点/缺点?


更新
似乎我最初的想法是错误的,感谢Bill Karwin的回答.

所以我将用原始查询的解释来更新它.

这个查询导致我的服务器使用100%的cpu.

也许有人可以说出原因,以及如何解决这个问题?

id  select_type         table             type   possible_keys  key        key_len  ref  rows    Extra                     
2   DEPENDENT SUBQUERY  action_6_members  index                 mobile     42            179218  Using where; Using index  
1   PRIMARY             sent_txts         index                 to_number  123           256066  Using where; Using index   

这是基于联接的解释(经过一些更优化)

id  select_type  table             type   possible_keys  key        key_len  ref  rows    Extra                                 
1   SIMPLE       sent_txts         index                 to_number  78            256066  Using index                           
1   SIMPLE       action_6_members  index                 mobile     27            179218  Using where; Using index; Not exists  

Bil*_*win 5

只需使用原始查询.MySQL可以优化这种情况,特别是如果mobile是索引列.它运行一次非相关子查询,并合理有效地to_numbermobile数字集进行比较.

WHERE在查询HAVING期间应用查询和条件之后,我不知道您在哪里获得有关条件的应用的想法.这不准确.

想一想:

  • WHERE条件从结果集中消除.这是在查询期间完成的.

  • HAVING条件从结果集中消除.这也是在查询期间完成的,但是在GROUP BY将行收集到组中之后.

    你不应该使用HAVING,如果你不使用GROUP BY.