国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

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

2024-07-24 12:31:21
字體:
來源:轉載
供稿:網友
      從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肯定支持的。

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿勒泰市| 永顺县| 武夷山市| 青阳县| 丰都县| 夏邑县| 灵川县| 淳安县| 多伦县| 石狮市| 丹凤县| 临猗县| 伊金霍洛旗| 池州市| 卫辉市| 镇巴县| 达日县| 房山区| 仁布县| 汕头市| 武夷山市| 策勒县| 沁阳市| 神池县| 新巴尔虎右旗| 垣曲县| 南昌市| 滨海县| 高要市| 景洪市| 息烽县| 家居| 类乌齐县| 江陵县| 通州市| 慈溪市| 调兵山市| 长沙县| 平顶山市| 德钦县| 南召县|