数据库版本:5.6.16

update in 修改数据,结果执行时间过慢,一直不出结果。

SQL语句及执行计划如下:

UPDATE erp_order_extra SET last_time=123123123123  
WHERE order_id IN (SELECT eo.order_id FROM jiuxianweb.erp_order eo WHERE eo.channel_ordersn='18161116045333705180');

+----+--------------------+-----------------+-----------------+-------------------------+---------+---------+------+---------+------------------------------+
| id | select_type        | table           | type            | possible_keys           | key     | key_len | ref  | rows    | Extra                        |
+----+--------------------+-----------------+-----------------+-------------------------+---------+---------+------+---------+------------------------------+
|  1 | PRIMARY            | erp_order_extra | index           | NULL                    | PRIMARY | 4       | NULL | 7528568 | Using where; Using temporary |
|  2 | DEPENDENT SUBQUERY | eo              | unique_subquery | PRIMARY,channel_ordersn | PRIMARY | 4       | func |       1 | Using where                  |
+----+--------------------+-----------------+-----------------+-------------------------+---------+---------+------+---------+------------------------------+

两张表的索引结构如下:
mysql> show index from erp_order_extra;
+-----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table           | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| erp_order_extra |          0 | PRIMARY    |            1 | extra_id    | A         |     7528568 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order_extra |          1 | order_id   |            1 | order_id    | A         |     7528568 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order_extra |          1 | action_id  |            1 | action_id   | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order_extra |          1 | split_type |            1 | split_type  | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

mysql> show index from erp_order;
+-----------+------------+-------------------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table     | Non_unique | Key_name                      | Seq_in_index | Column_name         | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+-------------------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| erp_order |          0 | PRIMARY                       |            1 | order_id            | A         |     8902636 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          0 | channel_ordersn               |            1 | channel_ordersn     | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          0 | channel_ordersn               |            2 | channel_id          | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | ordersn                       |            1 | order_sn            | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | printBatch                    |            1 | printBatch          | A         |     2225659 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | indexes_order_status          |            1 | order_status        | A         |        5324 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | indexes_order_status          |            2 | depart              | A         |       14198 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | indexes_order_status          |            3 | channel_id          | A         |       53309 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | orderstatus                   |            1 | order_status        | A         |          18 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | orderstatus                   |            2 | shipping_time       | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | addtime                       |            1 | add_time            | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | consignee                     |            1 | consignee           | A         |     2967545 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | consignee                     |            2 | verify_status       | A         |     2967545 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | consignee                     |            3 | add_time            | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | channeluser                   |            1 | channel_id          | A         |       14131 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | channeluser                   |            2 | verify_status       | A         |       34912 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | channeluser                   |            3 | user_id             | A         |     4451318 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | mobile                        |            1 | mobile              | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | extension_id                  |            1 | extension_id        | A         |       38707 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | self_id                       |            1 | self_id             | A         |         864 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | extension_code                |            1 | extension_code      | A         |        3487 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | tel                           |            1 | tel                 | A         |     2225659 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | order_amount                  |            1 | order_amount        | A         |       53955 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | invoice_no                    |            1 | invoice_no          | A         |     8902636 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | standard_time                 |            1 | standard_time       | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | shipping_time                 |            1 | shipping_time       | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | confirm_time                  |            1 | confirm_time        | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | pay_time                      |            1 | pay_time            | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | ware_id                       |            1 | ware_id             | A         |         110 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | admin_id                      |            1 | admin_id            | A         |        1936 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | userid                        |            1 | user_id             | A         |     2967545 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | shipping_id                   |            1 | channel_id          | A         |       18131 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | shipping_id                   |            2 | shipping_id         | A         |       74188 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | shipping_id                   |            3 | user_id             | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | shipping_id                   |            4 | verify_status       | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | shipping_id                   |            5 | add_time            | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | ind_channel_orderid_erp_order |            1 | channel_orderid     | A         |     4451318 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | index_erp_order_shipping_time |            1 | shipping_time       | A         |     8902636 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | ind_original_id_erp_order     |            1 | original_id         | A         |      890263 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | idx_ChangeTime                |            1 | ChangeTime          | A         |       45654 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | restorestock_status           |            1 | restorestock_status | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | index_temp                    |            1 | order_status        | A         |        3697 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | index_temp                    |            2 | is_range            | A         |        5915 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | index_temp                    |            3 | islock              | A         |        7394 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | index_temp                    |            4 | verify_status       | A         |        9614 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | index_temp                    |            5 | invoice_no          | A         |     8902636 |     NULL | NULL   |      | BTREE      |         |               |
| erp_order |          1 | index_ShopSn                  |            1 | ShopSn              | A         |          26 |     NULL | NULL   | YES  | BTREE      |         |               |
| erp_order |          1 | index_SellerSn                |            1 | SellerSn            | A         |          24 |     NULL | NULL   | YES  | BTREE      |         |               |
+-----------+------------+-------------------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

通过update join 去优化 update in的查询使用效率:

原SQL:
UPDATE erp_order_extra SET last_time=123123123123  
WHERE order_id IN (SELECT eo.order_id FROM jiuxianweb.erp_order eo WHERE eo.channel_ordersn='18161116045333705180');

JOIN SQL:
update erp_order_extra a inner join erp_order b on a.order_id=b.order_id set last_time=123123123123 where b.channel_ordersn='18161116045333705180';

执行计划:
+----+-------------+-------+------+-------------------------+-----------------+---------+-----------------------+------+--------------------------+
| id | select_type | table | type | possible_keys           | key             | key_len | ref                   | rows | Extra                    |
+----+-------------+-------+------+-------------------------+-----------------+---------+-----------------------+------+--------------------------+
|  1 | SIMPLE      | b     | ref  | PRIMARY,channel_ordersn | channel_ordersn | 153     | const                 |    1 | Using where; Using index |
|  1 | SIMPLE      | a     | ref  | order_id                | order_id        | 4       | jiuxianweb.b.order_id |    1 | NULL                     |
+----+-------------+-------+------+-------------------------+-----------------+---------+-----------------------+------+--------------------------+

mysql update join优化update in查询效率的更多相关文章

  1. MySQL 使用JOIN优化子查询

    1.数据准备 mysql> select * from student; +----+--------+----------+---------+-------------+ | id | na ...

  2. MYSQL通过索引优化数据库的查询

    #转载请联系 索引是什么? 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的位置信息. 更通俗的说,数据库索引好比是一本书前面的目录,能加快数据 ...

  3. [saiku] 优化多维度查询效率

    1.优化查询精度 优化原因:当维度过多时,查询很复杂,效率较慢.因此看能否通过优化sql的方式来提高查询效率. 代码跟踪查看(维度多余3时查询条件里面有in,优化不用in关键字) public abs ...

  4. MySQL时间设计 int timestamp datatime 查询效率性能比较

    在数据库设计的时候,我们经常会需要设计时间字段,在MYSQL中,时间字段可以使用int.timestamp.datetime三种类型来存储,那么这三种类型哪一种用来存储时间性能比较高,效率好呢?飘易就 ...

  5. mysql关于char和varchar的查询效率问题

    看了好多资料都说 varchar(size) 可变长度的字符值,节省空间,查询效率低 char(size) 固定长度的字符值,浪费空间,查询效率高 但是实际测试  char(100)   varcha ...

  6. mysql中存储字段类型的查询效率

    检索性能从快到慢的是(此处是听人说的): 第一:tinyint,smallint,mediumint,int,bigint第二:char,varchar第三:NULL 解释(转载): 整数类型1.TI ...

  7. 注意使用 BTREE 复合索引各字段的 ASC/DESC 以优化 order by 查询效率

    tbl_direct_pos_201506 表有 190 万数据.DDL: CREATE TABLE `tbl_direct_pos_201506` ( `acq_ins_code` char(13) ...

  8. MySql的join(连接)查询 (三表 left join 写法)

    1.内连接:将两个表中存在连结关系的字段符合连接条件的记录形成记录集 Select A.name,B.name from A inner join B on A.id=B.id和 Select A.n ...

  9. MySql采用range分区可提升查询效率

    简介: RANGE分区基于一个给定的连续区间范围,早期版本RANGE主要是基于整数的分区.在5.7版本中DATE.DATETIME列也可以使用RANGE分区,同时在5.5以上的版本提供了基于非整形的R ...

随机推荐

  1. 关于XML学习

    XML为知识构架存储语言: http://www.w3school.com.cn/xml/xml_tree.asp 语义网:为计算机构建一个系统的概念网络模型是计算机理解概念和机间信息交流的途径:XM ...

  2. java真实面试题(2)

    1,递归算法的实行过程,一般来说,可以分为()和()两个阶段,若一个问题的求解既可以用递归也可以用递推时,则往往用(),因为().贪婪法是一种()的算法. 答:递归算法分为递推和回归两个阶段,递推效率 ...

  3. 路飞学城Python-Day79

    27-url控制器之path方法 由于基于re_path的路径编写方式是重复性高的编写url,如果规则改变了,则对应的要去修改很多url,这样是很不方便的 所以采用Django2.0的path去编写u ...

  4. 数据结构(1) 第一天 算法时间复杂度、线性表介绍、动态数组搭建(仿Vector)、单向链表搭建、企业链表思路

    01 数据结构基本概念_大O表示法 无论n是多少都执行三个具体步骤 执行了12步 O(12)=>O(1) O(n) log 2 N = log c N / log c N (相当于两个对数进行了 ...

  5. Unicode编码,解释UCS、UTF、BMP、BOM等名词

    (转载 谈谈Unicode编码,简要解释UCS.UTF.BMP.BOM等名词 这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG游戏的升级 ...

  6. 【JavaScript框架封装】公共框架的封装

    /* * @Author: 我爱科技论坛 * @Time: 20180706 * @Desc: 实现一个类似于JQuery功能的框架 // 公共框架 // 种子模块:命名空间.对象扩展.数组化.类型的 ...

  7. Connection refused (SQL: select * from information_s chema.tables where table_schema = apidemo and table_name = migrations)

    LARAVEL 执行: $ php artisan migrate 报错 ,把 .env文件里的 DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306 换成 ...

  8. docker 下载镜像 ( 以 mysql为例 )

    一.官方镜像仓库 https://hub.docker.com/explore/ 二.常用操作 三.使用命令查看 mysql [root@localhost fw]# docker search my ...

  9. JavaScript实验一(添加节点,删除节点)

    静态html页面: <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...

  10. ZOJ 2316 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. O ...