mysql update join優化update in查詢效率
資料庫版本: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 |
+----+-------------+-------+------+-------------------------+-----------------+---------+-----------------------+------+--------------------------+
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 |
+----+-------------+-------+------+-------------------------+-----------------+---------+-----------------------+------+--------------------------+
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-2128599/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle update left join查詢Oracle
- join 查詢優化優化
- mysql update join,insert select 語法MySql
- sql查詢更新update selectSQL
- mysql 各種級聯查詢後更新(update select).MySql
- 用LEFT JOIN優化標量子查詢優化
- update語句的優化方式優化
- MySQL查詢優化MySql優化
- MySQL 優化六(InnoDB 下 update 資料出現表鎖之優化)MySql優化
- mysql的left join和inner join的效率對比,以及如何優化MySql優化
- MySQL 的查詢優化MySql優化
- MySQL 慢查詢優化MySql優化
- MySQL優化COUNT()查詢MySql優化
- OB_MYSQL UPDATE 最佳化案例MySql
- MySQL 優化五(關聯查詢子查詢以及 in 的效率問題)(高階篇)MySql優化
- mysql查詢優化檢查 explainMySql優化AI
- MySQL InnoDB update流程MySql
- MySQL調優之查詢優化MySql優化
- oracle update操作的優化一例Oracle優化
- 一條update語句的優化探索優化
- MySQL查詢優化利刃-EXPLAINMySql優化AI
- MySQL索引與查詢優化MySql索引優化
- MySQL分頁查詢優化MySql優化
- mysql關聯查詢優化MySql優化
- UPDATE查詢結果範圍內的資料
- UPDATE子查詢的ORA-1779錯誤
- MySQL join連表查詢示例MySql
- MySQL 常用的UPDATE操作MySql
- MySQL-效能優化-索引和查詢優化MySql優化索引
- MySQL——優化巢狀查詢和分頁查詢MySql優化巢狀
- 【資料庫】MySQL查詢優化資料庫MySql優化
- Mysql 慢查詢優化實踐MySql優化
- 十七、Mysql之SQL優化查詢MySql優化
- MySQL: 使用explain 優化查詢效能MySqlAI優化
- mysql查詢效能優化總結MySql優化
- MySQL子查詢的優化薦MySql優化
- 關於資料字典的查詢效率優化優化
- Windows Update與Microsoft UpdateWindowsROS