首頁 資料庫 mysql教程 彻底搞懂oracle的标量子查询

彻底搞懂oracle的标量子查询

Jun 07, 2016 pm 03:58 PM
oracle 徹底 查詢 量子

oracle标量子查询和自定义函数有时用起来比较方便,而且开发人员也经常使用,数据量小还无所谓,数据量大,往往存在性能问题。 以下测试帮助大家彻底搞懂标量子查询。 SQL create table a (id int,name varchar2(10)); Table created. SQL create table b (i

oracle标量子查询和自定义函数有时用起来比较方便,而且开发人员也经常使用,数据量小还无所谓,数据量大,往往存在性能问题。
以下测试帮助大家彻底搞懂标量子查询。

SQL> create table a (id int,name varchar2(10));
Table created.
SQL> create table b (id int,name varchar2(10));
Table created.
SQL> insert into a values (1,'a1');
1 row created.
SQL> insert into a values (2,'a2');
1 row created.
SQL> insert into b values (1,'b1');
1 row created.
SQL> insert into b values (2,'b2');
1 row created.
SQL> commit;
Commit complete.
SQL> @getlvall
Session altered.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b2
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 2 | 1 | 2 |00:00:00.01 | 14 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 2 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行2次,返回2行
SQL> insert into a values (3,'a3');
1 row created.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b2
3 a3
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 3 | 1 | 2 |00:00:00.01 | 21 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 3 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行3次,返回2行
SQL> insert into a values (4,'a4');
1 row created.
SQL> insert into a values (5,'a5');
1 row created.
SQL> insert into a values (6,'a6');
1 row created.
SQL> insert into a values (7,'a7');
1 row created.
SQL> insert into a values (8,'a8');
1 row created.
SQL> insert into a values (9,'a9');
1 row created.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b2
3 a3
4 a4
5 a5
6 a6
7 a7
8 a8
9 a9
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 9 | 1 | 2 |00:00:00.01 | 63 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行9次,返回2行
SQL> update b set name='b1';
2 rows updated.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b1
3 a3
4 a4
5 a5
6 a6
7 a7
8 a8
9 a9
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 9 | 1 | 2 |00:00:00.01 | 63 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
由上面的执行计划可以知道,b表执行2次,返回2行
SQL> insert into b values (3,'b1');
1 row created.
SQL> insert into b values (4,'b1');
1 row created.
SQL> insert into b values (5,'b1');
1 row created.
insert into b values (6,'b1');b1');
1 row created.
SQL> insert into b values (7,'b1');
1 row created.
SQL> insert into b values (8,'b1');
1 row created.
SQL> insert into b values (9,'b1');
1 row created.
SQL> commit;
Commit complete.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
2 a2 b1
3 a3 b1
4 a4 b1
5 a5 b1
6 a6 b1
7 a7 b1
8 a8 b1
9 a9 b1
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 9 | 1 | 9 |00:00:00.01 | 63 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
b.name字段全部为‘b1’,由上面的执行计划可以知道,b表执行9次,返回9行
SQL> update a set id=1;
9 rows updated.
SQL> commit;
Commit complete.
SQL> select * from a;
ID NAME
---------- --------------------
1 a1
1 a2
1 a3
1 a4
1 a5
1 a6
1 a7
1 a8
1 a9
9 rows selected.
SQL> select * from b;
ID NAME
---------- --------------------
1 b1
2 b1
3 b1
4 b1
5 b1
6 b1
7 b1
8 b1
9 b1
9 rows selected.
SQL> select a.*,(select name from b where b.id=a.id) from a;
ID NAME (SELECTNAMEFROMBWHER
---------- -------------------- --------------------
1 a1 b1
1 a2 b1
1 a3 b1
1 a4 b1
1 a5 b1
1 a6 b1
1 a7 b1
1 a8 b1
1 a9 b1
9 rows selected.
SQL> @getplanspe
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a
Plan hash value: 2657529235
------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
------------------------------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| B | 1 | 1 | 1 |00:00:00.01 | 7 |
| 2 | TABLE ACCESS FULL| A | 1 | 2 | 9 |00:00:00.01 | 8 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("B"."ID"=:B1)
Note
-----
- dynamic sampling used for this statement
22 rows selected.
SQL>
关联字段a.id全部为1,a表有9行,标量子查询相当于执行9次select name from b where b.id=1 ,oracle也不傻,starts=1,说明只执行了1次。
总结:
理想状态下,a.id为主键,没有重复值,那么a表返回多少行,b表就要被执行多少次。
特殊情况下,a.id的distinct值只有n个,那么b表只执行n次。


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

oracle如何查表空間大小 oracle如何查表空間大小 Apr 11, 2025 pm 08:15 PM

要查詢 Oracle 表空間大小,請遵循以下步驟:確定表空間名稱,方法是運行查詢:SELECT tablespace_name FROM dba_tablespaces;查詢表空間大小,方法是運行查詢:SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

oracle如何獲取時間 oracle如何獲取時間 Apr 11, 2025 pm 08:09 PM

在 Oracle 中獲取時間有以下方法:CURRENT_TIMESTAMP:返回當前系統時間,精確到秒。 SYSTIMESTAMP:比 CURRENT_TIMESTAMP 更準確,精確到納秒。 SYSDATE:返回當前系統日期,不含時間部分。 TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'): 將當前系統日期和時間轉換為特定格式。 EXTRACT:從時間值中提取特定部分,如年份、月份或小時。

oracle視圖如何加密 oracle視圖如何加密 Apr 11, 2025 pm 08:30 PM

Oracle 視圖加密允許您加密視圖中的數據,從而增強敏感信息安全性。步驟包括:1) 創建主加密密鑰 (MEk);2) 創建加密視圖,指定要加密的視圖和 MEk;3) 授權用戶訪問加密視圖。加密視圖工作原理:當用戶查詢加密視圖時,Oracle 使用 MEk 解密數據,確保只有授權用戶可以訪問可讀數據。

oracle如何查看實例名 oracle如何查看實例名 Apr 11, 2025 pm 08:18 PM

在 Oracle 中查看實例名的方法有三種:命令行中使用 "sqlplus" 和 "select instance_name from v$instance;" 命令。在 SQL*Plus 中使用 "show instance_name;" 命令。通過操作系統的任務管理器、Oracle Enterprise Manager 或檢查環境變量 (Linux 上的 ORACLE_SID)。

Oracle安裝失敗如何卸載 Oracle安裝失敗如何卸載 Apr 11, 2025 pm 08:24 PM

Oracle 安裝失敗的卸載方法:關閉 Oracle 服務,刪除 Oracle 程序文件和註冊表項,卸載 Oracle 環境變量,重新啟動計算機。若卸載失敗,可使用 Oracle 通用卸載工具手動卸載。

oracle數據庫如何導入 oracle數據庫如何導入 Apr 11, 2025 pm 08:06 PM

數據導入方法:1. 使用 SQLLoader 實用程序:準備數據文件、創建控製文件、運行 SQLLoader;2. 使用 IMP/EXP 工具:導出數據、導入數據。提示:1. 大數據集推薦 SQL*Loader;2. 目標表應存在,列定義匹配;3. 導入後需驗證數據完整性。

oracle無效數字如何排查 oracle無效數字如何排查 Apr 11, 2025 pm 08:27 PM

Oracle 無效數字錯誤可能由數據類型不匹配、數字溢出、數據轉換錯誤或數據損壞引起。排查步驟包括檢查數據類型、檢測數字溢出、檢查數據轉換、排查數據損壞,並探索其他可能的解決方案,如配置 NLS_NUMERIC_CHARACTERS 參數和啟用數據驗證日誌記錄。

oracle如何設置用戶 oracle如何設置用戶 Apr 11, 2025 pm 08:21 PM

在 Oracle 中創建用戶需遵循以下步驟:使用 CREATE USER 語句創建新用戶。使用 GRANT 語句授予必要權限。可選:使用 RESOURCE 語句設置配額。配置其他選項,如默認角色和臨時表空間。

See all articles