查詢一個表的一列插入到另一個表
create table t as select * from all_objects;
create table t1 as select * from all_objects;
alter table t add id number;
現在要往t中的id列用object_id填充
不使用存貯過程:
1.使用update
update t a set a.id=(select object_id from t1 b where a.object_id=b.object_id);
已用時間: 00: 00: 00.26
SQL> update t a set a.id=(select object_id from t2 b where a.object_id=b.object_id);
已更新53842行。
已用時間: 00: 11: 40.23
執行計劃
----------------------------------------------------------
Plan hash value: 848927671
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 55929 | 1420K| 228 (1)| 00:00:03 |
| 1 | UPDATE | T | | | | |
| 2 | TABLE ACCESS FULL| T | 55929 | 1420K| 228 (1)| 00:00:03 |
|* 3 | TABLE ACCESS FULL| T2 | 621 | 8073 | 228 (1)| 00:00:03 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("B"."OBJECT_ID"=:B1)
Note
-----
- dynamic sampling used for this statement
統計資訊
----------------------------------------------------------
316 recursive calls
163844 db block gets
54634781 consistent gets
0 physical reads
28650136 redo size
687 bytes sent via SQL*Net to client
647 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
7 sorts (memory)
0 sorts (disk)
53842 rows processed
2.s使用Merge into
1 merge into t a using t2 b
2 on (a.object_id=b.object_id)
3 when matched then
4* update set a.id=b.object_id
SQL> /
53842 行已合併。
已用時間: 00: 00: 02.68
執行計劃
----------------------------------------------------------
Plan hash value: 68457001
--------------------------------------------------------------------------------
-----
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
-----
| 0 | MERGE STATEMENT | | 55929 | 1420K| | 1474 (1)| 00:00
:18 |
| 1 | MERGE | T | | | | |
|
| 2 | VIEW | | | | | |
|
|* 3 | HASH JOIN | | 55929 | 18M| 10M| 1474 (1)| 00:00
:18 |
| 4 | TABLE ACCESS FULL| T2 | 62109 | 9583K| | 228 (1)| 00:00
:03 |
| 5 | TABLE ACCESS FULL| T | 55929 | 9995K| | 228 (1)| 00
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."OBJECT_ID"="B"."OBJECT_ID")
Note
-----
- dynamic sampling used for this statement
統計資訊
----------------------------------------------------------
287 recursive calls
55737 db block gets
1999 consistent gets
0 physical reads
12575944 redo size
686 bytes sent via SQL*Net to client
669 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
9 sorts (memory)
0 sorts (disk)
53842 rows processed
這種情況下,merge into的效率比update 的效率高多了
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22418990/viewspace-1054728/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql 從一個表中查詢,插入到另一個表中MySql
- [MySQL] - 聯表查詢,查詢一個不在另一個表的記錄MySql
- SQL語句將查詢結果插入到另一張表中SQL
- Oracle中把一個查詢結果插入到一張表中Oracle
- 雙主鍵,,查詢不在另一個表中的記錄
- Oracle如何把一個表匯出匯入到另一個伺服器上的另一個表裡Oracle伺服器
- 50個查詢系列-建表和插入資料
- 查詢一個表插入資料的時間,按BLOCK時間BloC
- mysql從一張表中取出資料插入到另一張表MySql
- 查詢一個表的外來鍵
- db2 sql批量插入一張表插入另一張表DB2SQL
- Sql查詢 一個表中某欄位的資料在另一個表中某欄位中不存在的SQL
- 觸發器—一個表更新資料時同步欄位到另一個表中觸發器
- MySQL 合併查詢union 查詢出的行合併到一個表中MySql
- MySQL 拷貝一個InnoDB分割槽表到另一個例項MySql
- sql server 2000 一個表的資料插入到三個相關聯表中,一條一條的插入SQLServer
- 把一個使用者的所有表的查詢許可權 賦給另一個使用者
- MySQL 合併查詢join 查詢出的不同列合併到一個表中MySql
- 將一個使用者下所有表的查詢許可權賦給另一個使用者
- 查詢某個表的索引資訊索引
- efcore 跨表查詢,實現一個介面內查詢兩個不同資料庫裡各自的表資料資料庫
- sqlserver查詢一個庫所有表的記錄數SQLServer
- MySQL 更新一個表裡的欄位等於另一個表某欄位的值MySql
- 怎樣把分組查詢出來的結果插入到一張新表中?
- SQL 兩個表組合查詢SQL
- 查詢訪問同一表的兩個以上索引(一)索引
- 同一張表的兩個欄位比較查詢
- mysql 5.7.11查詢分割槽表的一個問題MySql
- SQL查詢一年的十二個月份,形成報表SQL
- 怎樣查詢一個表的最後更新時間?
- SQL裡3個表的連線查詢SQL
- SqlServer將資料庫中的表複製到另一個資料庫SQLServer資料庫
- python指令碼完成資料庫的一個表一個表的遷移到另一臺伺服器Python指令碼資料庫伺服器
- SQL Server一個表新增資料,引發另一個表更新資料 的一個簡單觸發器例子SQLServer觸發器
- 透過外部表改進一個繁瑣的大查詢
- Oracle查詢分割槽表的最後一個分割槽值Oracle
- 查詢訪問同一表的兩個以上索引(三)索引
- 查詢訪問同一表的兩個以上索引(二)索引