update語句的優化方式
有二張表,表中資料如下所示:
SQL> select * from a;
ID COL1 COL2
---------- --------------- ---------------
1 test1 remark1
2 test2 remark2
3 test3 remark3
4 test4 remark4
SQL> select * from b;
ID COLD1 COLD2
---------- --------------- ---------------
1 B2 remark3
2 C1 remark2
3 B4 remark3
現要通過欄位id關聯,將表a中的col2欄位更新為B中對應的cold2值。
SQL> set autotrace on;
方式一:傳統的update語句,a,b表直接通過id關聯
SQL> edit
已寫入 file afiedt.buf
1 update a set a.col2=(
2 select b.cold2 from b
3 where a.id=b.id)
4 where exists(
5 select 1 from b
6* where a.id=b.id)
SQL> /
已更新3行。
執行計劃
----------------------------------------------------------
Plan hash value: 2449172722
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 3 | 105 | 7 (15)| 00:00:01 |
| 1 | UPDATE | A | | | | |
|* 2 | HASH JOIN SEMI | | 3 | 105 | 7 (15)| 00:00:01 |
| 3 | TABLE ACCESS FULL| A | 4 | 88 | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| B | 3 | 39 | 3 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | B | 1 | 22 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("A"."ID"="B"."ID")
5 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
統計資訊
----------------------------------------------------------
10 recursive calls
5 db block gets
62 consistent gets
0 physical reads
0 redo size
909 bytes sent via SQL*Net to client
1025 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
3 rows processed
SQL> rollback;
回退已完成。
方式二:merge 更新方式,通過分析計劃資料可知,merge更新要快於方式一
SQL> merge into a
2 using b
3 on(a.id=b.id)
4 when matched then update set a.col2=b.cold2;
3 行已合併。
執行計劃
----------------------------------------------------------
Plan hash value: 1518874097
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | MERGE STATEMENT | | 3 | 54 | 7 (15)| 00:00:01 |
| 1 | MERGE | A | | | | |
| 2 | VIEW | | | | | |
|* 3 | HASH JOIN | | 3 | 222 | 7 (15)| 00:00:01 |
| 4 | TABLE ACCESS FULL| B | 3 | 93 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL| A | 4 | 172 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."ID"="B"."ID")
Note
-----
- dynamic sampling used for this statement
統計資訊
----------------------------------------------------------
36 recursive calls
5 db block gets
49 consistent gets
0 physical reads
0 redo size
910 bytes sent via SQL*Net to client
990 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
3 rows processed
方式三:inline view 更新方式
SQL> update
2 (select a.col2,b.cold2 from a,b where a.id=b.id)
3 set col2=cold2;
set col2=cold2
*
第 3 行出現錯誤:
ORA-01779: 無法修改與非鍵值儲存表對應的列
注:關聯表B的id必須是主鍵。
SQL> alter table b add constraint pk_b primary key(id);
表已更改。
SQL> edit
已寫入 file afiedt.buf
1 update
2 (select a.col2,b.cold2 from a,b where a.id=b.id)
3* set col2=cold2
SQL> /
已更新3行。
執行計劃
----------------------------------------------------------
Plan hash value: 996040790
--------------------------------------------------------------------------------
------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
------
| 0 | UPDATE STATEMENT | | 3 | 132 | 6 (17)| 00:0
0:01 |
| 1 | UPDATE | A | | | |
|
| 2 | MERGE JOIN | | 3 | 132 | 6 (17)| 00:0
0:01 |
| 3 | TABLE ACCESS BY INDEX ROWID| B | 3 | 66 | 2 (0)| 00:0
0:01 |
| 4 | INDEX FULL SCAN | PK_B | 3 | | 1 (0)| 00:0
0:01 |
|* 5 | SORT JOIN | | 4 | 88 | 4 (25)| 00:0
0:01 |
| 6 | TABLE ACCESS FULL | A | 4 | 88 | 3 (0)| 00:0
0:01 |
--------------------------------------------------------------------------------
------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("A"."ID"="B"."ID")
filter("A"."ID"="B"."ID")
Note
-----
- dynamic sampling used for this statement
統計資訊
----------------------------------------------------------
198 recursive calls
5 db block gets
57 consistent gets
0 physical reads
0 redo size
910 bytes sent via SQL*Net to client
982 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
10 sorts (memory)
0 sorts (disk)
3 rows processed
SQL> rollback;
回退已完成。
三、結論
標準update語法
單表更新或較簡單的語句採用使用此方案更優。
inline view更新法
兩表關聯且被更新表通過關聯表主鍵關聯的,採用此方案更優。
merge更新法
兩表關聯且被更新表不是通過關聯表主鍵關聯的,採用此方案更優。來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/21251711/viewspace-1102660/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL的update語句避坑MySql
- SQL語句優化SQL優化
- 【SQL】10 SQL UPDATE 語句SQL
- sql語句效能優化SQL優化
- MySQL——優化ORDER BY語句MySql優化
- MYSQL SQL語句優化MySql優化
- 優化 SQL 語句的步驟優化SQL
- MySQL -update語句流程總結MySql
- ORACLE多表關聯UPDATE語句Oracle
- 一個UPDATE語句引發的血案
- MySQL之SQL語句優化MySql優化
- [20201210]sql語句優化.txtSQL優化
- [20200320]SQL語句優化的困惑.txtSQL優化
- MySQL系列6 - join語句的優化MySql優化
- Oracle優化案例-單表分頁語句的優化(八)Oracle優化
- SQL優化案例-單表分頁語句的優化(八)SQL優化
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- [20181114]一條sql語句的優化.txtSQL優化
- 一條update SQL語句是如何執行的SQL
- DBeaver如何生成select,update,delete,insert語句delete
- SQL語句優化的原則與方法QOSQL優化
- [20200324]SQL語句優化的困惑2.txtSQL優化
- Sql語句本身的優化-定位慢查詢SQL優化
- 優化 JS 條件語句的 5 個技巧優化JS
- 《MySQL慢查詢優化》之SQL語句及索引優化MySql優化索引
- KunlunDB功能之insert/update/delete...returning語句delete
- 淺析MySQL語句優化中的explain引數MySql優化AI
- Mysql跨表更新 多表update sql語句總結MySql
- 比CRUD多一點兒(三):UPDATE、DELETE語句delete
- 輕量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 語句ORMSQLdelete
- [20211229]toad下優化sql語句注意的問題.txt優化SQL
- ThinkPHP 列印 sql 語句的幾種方式PHPSQL
- Oracle vs PostgreSQL,研發注意事項(13) - UPDATE語句OracleSQL
- 翻譯:update語句(已提交到MariaDB官方手冊)
- soar-PHP - SQL 語句優化器和重寫器的 PHP 擴充套件包、 方便框架中 SQL 語句調優PHPSQL優化套件框架
- C++ 反彙編:關於Switch語句的優化措施C++優化
- Mysql 52條SQL語句效能優化策略彙總MySql優化
- sql語句執行順序與效能優化(1)SQL優化
- MySql常用30種SQL查詢語句優化方法MySql優化