MySQL - 如何将“使用连接缓冲区(块嵌套循环)”添加到查询中?

2024-04-06

当我在笔记本电脑上运行查询时,执行时间大约需要一秒钟,但在生产环境中,查询会持续 57 秒(并且它会导致应用程序崩溃 - 这是用 Ruby On Rails 编写的)。

我运行这个查询EXPLAIN并发现在我的笔记本电脑上是在执行的查询中的一张表上

使用连接缓冲区(块嵌套循环)

但这在生产中缺失了(对于这个表,表中没有任何内容)EXTRA柱子)。

这是为什么?如何添加使用连接缓冲区(块嵌套循环)还生产?

谢谢

EDIT:这是查询:

SELECT `shippers`.`company_name` 
FROM `shippers` 
LEFT OUTER JOIN `shipper_services` 
ON `shipper_services`.`shipper_id` = `shippers`.`id` 
LEFT OUTER JOIN `services` ON `services`.`id` = `shipper_services`.`service_id` 
LEFT OUTER JOIN `shipper_freight_flow_industries` 
ON `shipper_freight_flow_industries`.`shipper_id` = `shippers`.`id` 
LEFT OUTER JOIN `freight_flow_industries` 
ON `freight_flow_industries`.`id` = `shipper_freight_flow_industries`.`freight_flow_industry_id` 
WHERE `shippers`.`id` IN (189459, 337334, 149804, 36046, 158403, 165682, 153399, 51471, 211796, 164984, 61118, 56497, 340353, 66525, 225635, 250376, 33237, 69140, 151777, 169530, 245255, 142246, 259597, 57889, 262986, 167803, 33459, 40561, 65878, 44356, 169545, 210358, 25555, 4563, 82538, 157765, 232509, 213248, 37380, 258965, 340616, 340926, 143314, 195553, 60845, 161463, 255789, 38942, 192219, 811, 64672, 13530, 340809, 66030, 157223, 347671, 176886, 157812, 348255, 37357, 337003, 5588, 24257, 164799, 230717, 153801, 171835, 66595, 176780, 250184, 3262, 201519, 223904, 241992, 254167, 242449, 5368, 23903, 52571, 7198, 40135, 340494, 11851, 171285, 26810, 231003, 193961, 341161, 17122, 56660, 348103, 145539, 176912, 19716, 196617, 34803, 75444, 62418, 149606, 158879, 242439, 63291, 80548, 170778, 184871, 254549, 337109, 151159, 255077, 38939, 191926, 337720, 205999, 247471, 258106, 40225, 52599, 141637, 207246, 247541, 258876, 52629, 65936, 164884, 192238, 247588, 13669, 26875, 41763, 52700, 143009, 154515, 174092, 192869, 210753, 248501, 13835, 27222, 43017, 52995, 154640, 165368, 176390, 235034, 248829, 261492, 16610, 29589, 43109, 143661, 195373, 211866, 236874, 33148, 67629, 145474, 166592, 212358, 236937, 263276, 353, 18193, 44479, 68389, 196133, 241530, 251862, 331361, 770, 45861, 68982, 145652, 157945, 177022, 214534, 241659, 253705, 332487, 349455, 20303, 46852, 61001, 147373, 158198, 178036, 200139, 220189, 241765, 253987, 334046, 350465, 21532, 46970, 149478, 170761, 178187, 334167, 350466, 3285, 22934, 48334, 77067, 170770, 184809, 201905, 224892, 254293, 23063, 51366, 203181, 37607, 63370, 80720, 163426, 170798, 203424, 226486, 243809, 244904, 63388, 81420, 163553, 170800, 203819, 228767, 244936, 6057, 24664, 52584, 152229, 164080, 170812, 192096, 229759, 257935, 25300, 52592, 65635, 82575, 152798, 164171, 170817, 192179, 206647, 229772) 
AND ((
          (ACOS(least(1,COS(0.5194174327134307)*COS(-1.664517065837707)*COS(RADIANS(shippers.latitude))*COS(RADIANS(shippers.longitude))+
          COS(0.5194174327134307)*SIN(-1.664517065837707)*COS(RADIANS(shippers.latitude))*SIN(RADIANS(shippers.longitude))+
          SIN(0.5194174327134307)*SIN(RADIANS(shippers.latitude))))*3963.1899999999996)
          <= 5.0)) AND (store_location = "0" AND corporate_hq = "0" AND queued_item="0") 
GROUP BY company_name  ORDER BY count_of_facilities DESC)

生产:

+----+-------------+---------------------------------+--------+-------------------+-----------+---------+-----------------------------------------------------------------------+--------+----------------------------------------------+
| id | select_type | table                           | type   | possible_keys     | key       | key_len | ref                                                                   | rows   | Extra                                        |
+----+-------------+---------------------------------+--------+-------------------+-----------+---------+-----------------------------------------------------------------------+--------+----------------------------------------------+
|  1 | SIMPLE      | shippers                        | range  | PRIMARY,idx_store | PRIMARY   | 4       | NULL                                                                  |    245 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | shipper_services                | ref    | idx_combo         | idx_combo | 5       | db_production.shippers.id                                              |      2 | Using index                                  |
|  1 | SIMPLE      | services                        | eq_ref | PRIMARY           | PRIMARY   | 4       | db_production.shipper_services.service_id                              |      1 | Using index                                  |
|  1 | SIMPLE      | shipper_freight_flow_industries | ALL    | NULL              | NULL      | NULL    | NULL                                                                  | 241609 |                                              |
|  1 | SIMPLE      | freight_flow_industries         | eq_ref | PRIMARY           | PRIMARY   | 4       | db_production.shipper_freight_flow_industries.freight_flow_industry_id |      1 | Using index                                  |
+----+-------------+---------------------------------+--------+-------------------+-----------+---------+-----------------------------------------------------------------------+--------+----------------------------------------------+
5 rows in set (0.00 sec)

Indexes:

+---------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                           | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| shipper_freight_flow_industries |          0 | PRIMARY  |            1 | id          | A         |      241609 |     NULL | NULL   |      | BTREE      |         |               |
+---------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

本地主机:

+----+-------------+---------------------------------+--------+-------------------+-----------+---------+-----------------------------------------------------------------------------------+------+----------------------------------------------------+
| id | select_type | table                           | type   | possible_keys     | key       | key_len | ref                                                                               | rows | Extra                                              |
+----+-------------+---------------------------------+--------+-------------------+-----------+---------+-----------------------------------------------------------------------------------+------+----------------------------------------------------+
|  1 | SIMPLE      | shippers                        | range  | PRIMARY,idx_store | PRIMARY   | 4       | NULL                                                                              |  245 | Using where; Using temporary; Using filesort       |
|  1 | SIMPLE      | shipper_services                | ref    | idx_combo         | idx_combo | 5       | development.shippers.id                                              |    2 | Using index                                        |
|  1 | SIMPLE      | services                        | eq_ref | PRIMARY           | PRIMARY   | 4       | development.shipper_services.service_id                              |    1 | Using index                                        |
|  1 | SIMPLE      | shipper_freight_flow_industries | ALL    | NULL              | NULL      | NULL    | NULL                                                                              |   58 | Using where; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | freight_flow_industries         | eq_ref | PRIMARY           | PRIMARY   | 4       | development.shipper_freight_flow_industries.freight_flow_industry_id |    1 | Using index                                        |
+----+-------------+---------------------------------+--------+-------------------+-----------+---------+-----------------------------------------------------------------------------------+------+----------------------------------------------------+

Indexes:

+---------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                           | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| shipper_freight_flow_industries |          0 | PRIMARY  |            1 | id          | A         |          58 |     NULL | NULL   |      | BTREE      |         |               |
+---------------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

这是 Mysql 的一个错误https://bugs.mysql.com/bug.php?id=69721 https://bugs.mysql.com/bug.php?id=69721

您可以使用以下命令运行查询

set optimizer_switch='block_nested_loop=off'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MySQL - 如何将“使用连接缓冲区(块嵌套循环)”添加到查询中? 的相关文章

  • MYSQL从每个类别中随机选择一条记录

    我有一个数据库Items表看起来像这样 id name category int 有几十万条记录 每个item可以是 7 种不同的之一categories 对应于categories table id category 我想要一个从每个类别
  • mysql自动存储记录创建时间戳

    mysql 有什么方法可以在创建记录时自动将时间戳存储在记录行中 我试图使用时间戳 数据类型 和 current timestamp 作为默认值 但后来意识到每次更新记录时都会更新 我只需要一些可以存储创建时间戳的东西 Thanks Set
  • 如何使用 BigQuery 有效地选择另一个表中匹配子字符串的记录?

    我有一个包含数百万个字符串的表 我想将其与包含大约两万个字符串的表进行匹配 如下所示 standardSQL SELECT record FROM record JOIN fragment ON record name LIKE CONCA
  • 随机组合 MySQL 数据库中的两个单词

    我有一个包含名词和形容词的数据库 例如 id type word 1 noun apple 2 noun ball 3 adj clammy 4 noun keyboard 5 adj bloody ect 我想创建一个查询 它将抓取 10
  • PHP 与 MySQL 查询性能( if 、 函数 )

    我只看到这个artice http www onextrapixel com 2010 06 23 mysql has functions part 5 php vs mysql performance 我需要知道在这种情况下什么是最好的表
  • 如何使用 SQL Server 2008 将行复制到同一个表中

    A 到目前为止我的方式 sqlCommand CommandText INSERT Table1 column1 column2 column3 SELECT column1 column2 column3 FROM Table1 WHER
  • Mysql插入表后不显示右单引号(’)

    我有一个名为 测试 的表 我插入了一行 其中包含unicode字符右单引号 0x2019在名称字段中 SQL insert into Testing values Sno Name Address insert into Testing v
  • 在 android 中建立与 MySQL 的池连接

    我需要从我的 Android 应用程序访问 MySQL 数据库 现在所有的工作都通过 DriverManager getConnection url 等等 但我必须从多个线程访问数据库 所以我必须使用连接池 问题1 是 com mysql
  • 通过触发器应用表的列权限

    现在 我有一个名为 Members 的表 其中包含内容 分为联系人数据 银行数据 现在 管理员应该能够创建 更新 删除用户 这些用户保存在另一个表中 该表只能访问管理员 用户应该获得自己的 mysql 用户帐户 管理员还应该能够设置权限 例
  • 如何部署“SQL Server Express + EF”应用程序

    这是我第一次部署使用 SQL Server Express 数据库的应用程序 我首先使用实体 框架模型来联系数据库 我使用 Install Shield 创建了一个安装向导来安装应用程序 这些是我在目标计算机中安装应用程序所执行的步骤 安装
  • SQL 更新 - 更新选定的行

    我正在使用 SQL Server 2008 我有一个名为MYTABLE有两列 ID STATUS 我想编写一个存储过程来返回其记录STATUS是 0 但是这个存储过程必须更新STATUS返回行数为 1 如何在单个查询中执行此选择和更新操作
  • 如何从子查询 SQLite 接收两个值

    我是一个自学的 SQLite 新手 我有三个表 person pet person pet schema 是 CREATE TABLE person id INTEGER PRIMARY KEY first name TEXT last n
  • 从前端更改记录顺序

    我在编写下一个功能时遇到问题 我希望用户能够重新排列记录并更改 display order 值 我使用 Jquery UI 的可拖放功能来促进这一点 我可以看到如何简单地交换 display order 值 但我想为一条记录设置一个显示顺序
  • mysql 版本号排序

    我有这样的价值观 1 1 2 9 1 2 2 4 1 2 3 4 3 2 14 3 2 1 4 2 我需要使用 mysql 对这些值进行排序 该数据类型是 varbinary 300 所需的输出将类似于 1 1 2 1 2 3 4 2 2
  • SQL 按 IN 子句中的元素排序

    我有一个ITEM表 我想返回按 IN 子句中通知的相同顺序排序的结果 这些 ID 由用户告知 今天我有这个 SELECT FROM ITEM WHERE ITEM ID IN 45 2 671 6 ORDER BY CASE ITEM ID
  • 加载“mysql2”Active Record 适配器时出错

    我正在尝试升级我的应用程序 这是我在部署应用程序时遇到的错误 加载 mysql2 Active Record 适配器时出错 缺少它所依赖的宝石 无法激活mysql2 0 4 4 已经激活mysql2 0 3 21 确保所有依赖项都添加到 G
  • 使用默认路径中的文件创建数据库

    我想创建一个创建数据库的 SQL 脚本 现在 我有这个 CREATE DATABASE Documents ON PRIMARY NAME N Documents FILENAME N Documents mdf LOG ON NAME N
  • 如何将自托管 WordPress 和已安装的实时站点插件升级到最新可用版本而不遇到任何麻烦?

    我必须升级正在运行的 WordPress 网站的 WordPress CMS 和一些已安装的插件 并且我想要升级的一些插件之前已被修改以实现某些目标 http easycaptures com fs uploaded 288 9522279
  • 如何使用单个查询对从另一个表检索的表列表进行 UNION?

    我有一个表 其中包含 PostgreSQL 中的表列表 id table 1 table1 2 table2 3 table3 我想从所有这些表的联合中进行选择 例如 伪代码 select from union select table f
  • MySQL - 连接 a 或 b

    假设我有一个TABLE a其中一个COLUMN data是一个join其他 2 张桌子 TABLE b and TABLE c 因为我想得到一个COLUMN info in b or c 事情是a data将匹配only with b da

随机推荐