• <bdo id='SqrNS'></bdo><ul id='SqrNS'></ul>

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

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

      1. MySQL - SELECT WHERE field IN(子查询) - 为什么非常慢?

        MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?(MySQL - SELECT WHERE field IN(子查询) - 为什么非常慢?)
          <tbody id='6F1Tf'></tbody>

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

              <bdo id='6F1Tf'></bdo><ul id='6F1Tf'></ul>

              <small id='6F1Tf'></small><noframes id='6F1Tf'>

              <tfoot id='6F1Tf'></tfoot>

                1. 本文介绍了MySQL - SELECT WHERE field IN(子查询) - 为什么非常慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我想检查数据库中有几个重复项,所以我做了什么来查看哪些是重复项,我这样做了:

                  I've got a couple of duplicates in a database that I want to inspect, so what I did to see which are duplicates, I did this:

                  SELECT relevant_field
                  FROM some_table
                  GROUP BY relevant_field
                  HAVING COUNT(*) > 1
                  

                  这样,我将获得相关字段出现不止一次的所有行.此查询需要几毫秒才能执行.

                  This way, I will get all rows with relevant_field occuring more than once. This query takes milliseconds to execute.

                  现在,我想检查每个重复项,所以我想我可以在上面的查询中使用相关字段选择 some_table 中的每一行,所以我这样做了:

                  Now, I wanted to inspect each of the duplicates, so I thought I could SELECT each row in some_table with a relevant_field in the above query, so I did like this:

                  SELECT *
                  FROM some_table 
                  WHERE relevant_field IN
                  (
                      SELECT relevant_field
                      FROM some_table
                      GROUP BY relevant_field
                      HAVING COUNT(*) > 1
                  )
                  

                  由于某种原因,这变得非常缓慢(需要几分钟).这里到底发生了什么让它变得这么慢?相关字段已编入索引.

                  This turns out to be extreeeemely slow for some reason (it takes minutes). What exactly is going on here to make it that slow? relevant_field is indexed.

                  最终我尝试从第一个查询(SELECT related_field FROM some_table GROUP BY related_field HAVING COUNT(*) > 1)创建一个视图temp_view",然后像这样进行我的第二个查询:

                  Eventually I tried creating a view "temp_view" from the first query (SELECT relevant_field FROM some_table GROUP BY relevant_field HAVING COUNT(*) > 1), and then making my second query like this instead:

                  SELECT *
                  FROM some_table
                  WHERE relevant_field IN
                  (
                      SELECT relevant_field
                      FROM temp_view
                  )
                  

                  而且效果很好.MySQL 会在几毫秒内完成此操作.

                  And that works just fine. MySQL does this in some milliseconds.

                  这里有没有 SQL 专家可以解释发生了什么?

                  Any SQL experts here who can explain what's going on?

                  推荐答案

                  正在为每一行运行子查询,因为它是一个相关查询.通过从子查询中选择所有内容,可以将相关查询变成非相关查询,如下所示:

                  The subquery is being run for each row because it is a correlated query. One can make a correlated query into a non-correlated query by selecting everything from the subquery, like so:

                  SELECT * FROM
                  (
                      SELECT relevant_field
                      FROM some_table
                      GROUP BY relevant_field
                      HAVING COUNT(*) > 1
                  ) AS subquery
                  

                  最终查询将如下所示:

                  SELECT *
                  FROM some_table
                  WHERE relevant_field IN
                  (
                      SELECT * FROM
                      (
                          SELECT relevant_field
                          FROM some_table
                          GROUP BY relevant_field
                          HAVING COUNT(*) > 1
                      ) AS subquery
                  )
                  

                  这篇关于MySQL - SELECT WHERE field IN(子查询) - 为什么非常慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                  MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                  Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                  How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                  Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                  Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

                    <tbody id='rkRy3'></tbody>

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

                  • <bdo id='rkRy3'></bdo><ul id='rkRy3'></ul>
                    <tfoot id='rkRy3'></tfoot>

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

                    • <legend id='rkRy3'><style id='rkRy3'><dir id='rkRy3'><q id='rkRy3'></q></dir></style></legend>