1. <small id='cHiNU'></small><noframes id='cHiNU'>

      <i id='cHiNU'><tr id='cHiNU'><dt id='cHiNU'><q id='cHiNU'><span id='cHiNU'><b id='cHiNU'><form id='cHiNU'><ins id='cHiNU'></ins><ul id='cHiNU'></ul><sub id='cHiNU'></sub></form><legend id='cHiNU'></legend><bdo id='cHiNU'><pre id='cHiNU'><center id='cHiNU'></center></pre></bdo></b><th id='cHiNU'></th></span></q></dt></tr></i><div id='cHiNU'><tfoot id='cHiNU'></tfoot><dl id='cHiNU'><fieldset id='cHiNU'></fieldset></dl></div>
        <bdo id='cHiNU'></bdo><ul id='cHiNU'></ul>

      <legend id='cHiNU'><style id='cHiNU'><dir id='cHiNU'><q id='cHiNU'></q></dir></style></legend>
      <tfoot id='cHiNU'></tfoot>

      1. 子查询与连接

        时间:2023-08-18
        <legend id='XFiF5'><style id='XFiF5'><dir id='XFiF5'><q id='XFiF5'></q></dir></style></legend>

          <small id='XFiF5'></small><noframes id='XFiF5'>

          <tfoot id='XFiF5'></tfoot>
            <bdo id='XFiF5'></bdo><ul id='XFiF5'></ul>

                  <i id='XFiF5'><tr id='XFiF5'><dt id='XFiF5'><q id='XFiF5'><span id='XFiF5'><b id='XFiF5'><form id='XFiF5'><ins id='XFiF5'></ins><ul id='XFiF5'></ul><sub id='XFiF5'></sub></form><legend id='XFiF5'></legend><bdo id='XFiF5'><pre id='XFiF5'><center id='XFiF5'></center></pre></bdo></b><th id='XFiF5'></th></span></q></dt></tr></i><div id='XFiF5'><tfoot id='XFiF5'></tfoot><dl id='XFiF5'><fieldset id='XFiF5'></fieldset></dl></div>
                    <tbody id='XFiF5'></tbody>
                  本文介绍了子查询与连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我重构了我们从另一家公司继承的应用程序的一个缓慢部分,以使用内部联接而不是像这样的子查询:

                  I refactored a slow section of an application we inherited from another company to use an inner join instead of a subquery like:

                  WHERE id IN (SELECT id FROM ...)
                  

                  重构后的查询运行速度提高了大约 100 倍.(约 50 秒到约 0.3 秒)我期待改进,但谁能解释为什么它如此激烈?where 子句中使用的列都已编入索引.SQL 是否每行执行一次 where 子句中的查询?

                  The refactored query runs about 100x faster. (~50 seconds to ~0.3) I expected an improvement, but can anyone explain why it was so drastic? The columns used in the where clause were all indexed. Does SQL execute the query in the where clause once per row or something?

                  更新 - 解释结果:

                  区别在于where id in()"查询的第二部分——

                  The difference is in the second part of the "where id in ()" query -

                  2   DEPENDENT SUBQUERY  submission_tags ref st_tag_id   st_tag_id   4   const   2966    Using where
                  

                  vs 1 个带连接的索引行:

                  vs 1 indexed row with the join:

                      SIMPLE  s   eq_ref  PRIMARY PRIMARY 4   newsladder_production.st.submission_id  1   Using index
                  

                  推荐答案

                  相关子查询"(即,其中 where 条件取决于从包含查询的行中获得的值的查询)将对每一行执行一次.不相关的子查询(其中 where 条件独立于包含查询的子查询)将在开始时执行一次.SQL 引擎会自动做出这种区分.

                  A "correlated subquery" (i.e., one in which the where condition depends on values obtained from the rows of the containing query) will execute once for each row. A non-correlated subquery (one in which the where condition is independent of the containing query) will execute once at the beginning. The SQL engine makes this distinction automatically.

                  但是,是的,解释计划会给你一些肮脏的细节.

                  But, yeah, explain-plan will give you the dirty details.

                  这篇关于子查询与连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从 MySQL 插入查询中获取新记录主键 ID? 下一篇:在 MySQL 中生成一系列数字

                  相关文章

                  最新文章

                    <small id='m2V5t'></small><noframes id='m2V5t'>

                    <tfoot id='m2V5t'></tfoot>

                  1. <i id='m2V5t'><tr id='m2V5t'><dt id='m2V5t'><q id='m2V5t'><span id='m2V5t'><b id='m2V5t'><form id='m2V5t'><ins id='m2V5t'></ins><ul id='m2V5t'></ul><sub id='m2V5t'></sub></form><legend id='m2V5t'></legend><bdo id='m2V5t'><pre id='m2V5t'><center id='m2V5t'></center></pre></bdo></b><th id='m2V5t'></th></span></q></dt></tr></i><div id='m2V5t'><tfoot id='m2V5t'></tfoot><dl id='m2V5t'><fieldset id='m2V5t'></fieldset></dl></div>

                      <bdo id='m2V5t'></bdo><ul id='m2V5t'></ul>
                    <legend id='m2V5t'><style id='m2V5t'><dir id='m2V5t'><q id='m2V5t'></q></dir></style></legend>