青草久久影院-青草久久伊人-青草久久久-青草久久精品亚洲综合专区-SM双性精跪趴灌憋尿调教H-SM脚奴调教丨踩踏贱奴

17站長網

17站長網 首頁 數據庫 Mysql 查看內容

相關MYSQL DML UPDATE DELETE 中的子查詢問題

2023-3-16 14:21| 查看: 2302 |來源: 互聯網

從5.6開始MYSQL的子查詢進行了大量的優化,5.5中只有EXISTS strategy,在5.7中包含如下: IN(=ANY) --Semi-join --table pullout(最快的,子查詢條件為唯一 ...

從5.6開始MYSQL的子查詢進行了大量的優化,5.5中只有EXISTS strategy,在5.7中包含如下:

      IN(=ANY)

      --Semi-join

      --table pullout(最快的,子查詢條件為唯一鍵)

      --first match

      --semi-join materialization

      --loosescan

       --duplicateweedout

--Materialization

--EXISTS strategy(最慢的)

NOT IN( <>ALL)

--Materialization

--EXISTS strategy(最慢的)

 

 

而(not)exist卻沒有任何優化還是關聯子查詢的方式,這和ORACLE不一樣,ORACLE中in、exists

都可以使用半連接(semi)優化.所以MYSQL中盡量使用in不要用exists。not in不能使用semi-join

要小心使用,更不要用not exists,關于上面每一個含義可以參考官方手冊和mariadb手冊。

 

我們簡單的看一個列子,

 

 

使用semi-join materialization優化的

mysql> explain select * from testde1 where testde1.id in(select id from testde2);

+----+--------------+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+

| id | select_type  | table       | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                                              |

+----+--------------+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+

|  1 | SIMPLE       | | NULL       | ALL  | NULL          | NULL | NULL    | NULL | NULL |   100.00 | NULL                                               |

|  1 | SIMPLE       | testde1     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   15 |    10.00 | Using where; Using join buffer (Block Nested Loop) |

|  2 | MATERIALIZED | testde2     | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL                                               |

+----+--------------+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+

3 rows in set, 1 warning (0.00 sec)

  

禁用semi join使用Materialization優化

mysql> set optimizer_switch='semijoin=off';

Query OK, 0 rows affected (0.00 sec)

 

 

mysql> explain select * from testde1 where testde1.id in(select id from testde2);

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

|  1 | PRIMARY     | testde1 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   15 |   100.00 | Using where |

|  2 | SUBQUERY    | testde2 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |   100.00 | NULL        |

+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

2 rows in set, 1 warning (0.00 sec)

 

禁用join使用Materialization

ysql> set optimizer_switch='materialization=off';

Query OK, 0 rows affected (0.00 sec)

 

 

mysql> explain select * from testde1 where testde1.id in(select id from testde2);

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

| id | select_type        | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

|  1 | PRIMARY            | testde1 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   15 |   100.00 | Using where |

|  2 | DEPENDENT SUBQUERY | testde2 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |    50.00 | Using where |

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

2 rows in set, 1 warning (0.00 sec)

 

 

Note (Code 1003): /* select#1 */ select `test`.`testde1`.`id` AS `id` from `test`.`testde1` where (`test`.`testde1`.`id`,(/* select#2 */ select 1 from `test`.`testde2` where ((`test`.`testde1`.`id`) = `test`.`testde2`.`id`)))

 

 

使用DEPENDENT SUBQUERY 關聯子查詢優化,這也是最慢的。這和

select * from testde1 where exists (select * from testde2 where testde1.id=testde2.id);的執行計劃完全一致,

testde1大表必須作為驅動表

mysql> explain select * from testde1 where exists (select * from testde2 where testde1.id=testde2.id);

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

| id | select_type        | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

|  1 | PRIMARY            | testde1 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   15 |   100.00 | Using where |

|  2 | DEPENDENT SUBQUERY | testde2 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |    50.00 | Using where |

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

2 rows in set, 2 warnings (0.00 sec)

 

實際就是下面的執行計劃:

 

 

mysql> explain delete from testde1 where id in (select id from testde2);

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

| id | select_type        | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

|  1 | DELETE             | testde1 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |   15 |   100.00 | Using where |

|  2 | DEPENDENT SUBQUERY | testde2 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    2 |    50.00 | Using where |

+----+--------------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+

2 rows in set (0.00 sec)

 

這里我們看到小表testde2做了驅動表。

最后來說明一下這個報錯:

mysql> delete from testde1 where id in(select testde1.id from testde1,testde2 where testde1.id=testde2.id );

ERROR 1093 (HY000): You can't specify target table 'testde1' for update in FROM clause

我們先不管他有沒有意義,這個報錯再手冊上叫做ER_UPDATE_TABLE_USED,我們首先來分析一下這個報錯

這樣的delete會進行exists展開那么testde1既是修改條件的來源也是修改的對象,這樣是不允許的。那么如何修改呢?

實際上就需要select testde1.id from testde1,testde2 where testde1.id=testde2.id 的結果保存在一個臨時表中,

不要exists展開,手冊中給出的方法是

方法一、建立一個algorithm=temptable 的視圖

方法二、建立一個普通視圖同時修改SET optimizer_switch = 'derived_merge=off';

 

 

其目的都在于不展開選取第二種方式測試:

mysql> create view myt1

    -> as

    -> select testde1.id from testde1,testde2 where testde1.id=testde2.id;

Query OK, 0 rows affected (0.02 sec)

  

mysql> show status like '%tmp%';

+-------------------------+-------+

| Variable_name           | Value |

+-------------------------+-------+

| Created_tmp_disk_tables | 0     |

| Created_tmp_files       | 0     |

| Created_tmp_tables      | 2    |

+-------------------------+-------+

3 rows in set (0.01 sec)

 

看看執行計劃:

mysql> explain delete from testde1 where id in (select * from myt1);

+----+--------------------+------------+------------+----------------+---------------+-------------+---------+------+------+----------+----------------------------------------------------+

| id | select_type        | table      | partitions | type           | possible_keys | key         | key_len | ref  | rows | filtered | Extra                                              |

+----+--------------------+------------+------------+----------------+---------------+-------------+---------+------+------+----------+----------------------------------------------------+

|  1 | DELETE             | testde1    | NULL       | ALL            | NULL          | NULL        | NULL    | NULL |   13 |   100.00 | Using where                                        |

|  2 | DEPENDENT SUBQUERY | | NULL       | index_subquery |   | | 5       | func |    2 |   100.00 | Using index                                        |

|  3 | DERIVED            | testde2    | NULL       | ALL            | NULL          | NULL        | NULL    | NULL |    2 |   100.00 | NULL                                               |

|  3 | DERIVED            | testde1    | NULL       | ALL            | NULL          | NULL        | NULL    | NULL |   13 |    10.00 | Using where; Using join buffer (Block Nested Loop) |

+----+--------------------+------------+------------+----------------+---------------+-------------+---------+------+------+----------+----------------------------------------------------+

4 rows in set (0.00 sec)

  

先使用hash join將TESTDE2 和TESTDE1  建立為一個視圖VW_NSO_1,然后使用了HASH JOIN SEMI的優化方式,明顯用了到半連接優化

這也是為什么ORACLE比現在的MYSQL還是更加強勁的一個小例子,雖然都是作為一個整體,但是MYSQL已經用不到SEMI優化方式了,ORACLE

依然可以,但是可以預見不久的將來MYSQL肯定支持的。

本文最后更新于 2023-3-16 14:21,某些文章具有時效性,若有錯誤或已失效,請在網站留言或聯系站長:[email protected]
·END·
站長網微信號:w17tui,關注站長、創業、關注互聯網人 - 互聯網創業者營銷服務中心

免責聲明:本站部分文章和圖片均來自用戶投稿和網絡收集,旨在傳播知識,文章和圖片版權歸原作者及原出處所有,僅供學習與參考,請勿用于商業用途,如果損害了您的權利,請聯系我們及時修正或刪除。謝謝!

17站長網微信二維碼

始終以前瞻性的眼光聚焦站長、創業、互聯網等領域,為您提供最新最全的互聯網資訊,幫助站長轉型升級,為互聯網創業者提供更加優質的創業信息和品牌營銷服務,與站長一起進步!讓互聯網創業者不再孤獨!

掃一掃,關注站長網微信

大家都在看

    熱門排行

      最近更新

        返回頂部
        主站蜘蛛池模板: 在线自拍综合亚洲欧美 | 窝窝影院午夜看片毛片 | 中国大陆一级毛片免费 | 男女久久久国产一区二区三区 | 116美女写真成人午夜视频 | 在线视频a | 国产99视频在线观看 | 性盈盈剧场 | 伊人久久青草青青综合 | 亚洲欧洲日产国码中学 | 97久久无码精品AV | 国产毛A片啊久久久久久A | 日本乱子伦一区二区三区 | 免费人成在线观看视频不卡 | 欧美三级在线完整版免费 | 蜜桃人妻无码AV天堂三区 | 四虎影视永久无码精品 | 最近最新的日本字幕MV | 久久AV国产麻豆HD真实乱 | 亚洲午夜性春猛交XXXX | 亚洲欧美国产双大乳头 | 欧美激情视频二区 | 麒麟色欧美影院在线播放 | 2019精品国产品在线不卡 | 性欧美video 性欧美sexovideotv | 97一期涩涩97片久久久久久久 | 亚洲看片无码免费视频 | 久久这里都是精品 | 好吊妞在线成人免费 | 国产精品爽爽久久久久久蜜桃网站 | 精品无码久久久久久动漫 | 亚洲国产精品嫩草影院永久 | 特级淫片大乳女子高清视频 | 久久a在线视频观看 | 欧美日韩第一区 | 精品性影院一区二区三区内射 | 色综合久久综合网观看 | 欧洲馒头大肥p | 久久精品国产亚洲AV妓女不卡 | 国内卡一卡二卡三免费网站 | 三级黄色一级视频 |