巢狀SQL的查詢速度比較分析

2021-08-26 12:48:10 字數 3585 閱讀 3336

巢狀sql的查詢速度比較分析

文章中使用oracle自帶的hr資料庫,故**可以直接進行測試。

**一:

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'a%'

or t.first_name like 'b%'

or t.first_name like 'h%'

or t.first_name like 'k%'

or t.first_name like 'm%'

or t.first_name like 'j%'

or t.first_name like 'n%'

執行計畫:

**二:

select *

from (

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'a%'

or t.first_name like 'b%'

or t.first_name like 'h%'

or t.first_name like 'k%'

or t.first_name like 'm%'

or t.first_name like 'j%'

or t.first_name like 'n%'

)執行計畫:

對比:**1與**2的執行計畫相同

**三:

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'a%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'b%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'h%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'k%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'm%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'j%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'n%'

執行計畫:

**四:

select * from

(select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'a%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'b%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'h%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'k%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'm%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'j%'

union

select t.employee_id, t.first_name, t.phone_number from hr.employees t where t.first_name like 'n%'

)執行計畫:

其他:在乙個sql中,使用「or」語句比使用多個union會花費更短的時間。

**五:

**5-1:

select *

from

(select * from hr.employees tx where tx.department_id = 50) t1,

(select * from hr.departments ty where ty.department_id < 150) t2

where t1.department_id = t2.department_id

**5-2:

select *

from

hr.employees t1,

hr.departments t2

where t1.department_id = t2.department_id and t1.department_id = 50 and t2.department_id < 150

**5-3:

select *

from

hr.employees t1,

hr.departments t2

where t1.department_id = t2.department_id(+) and t1.department_id = 50 and t2.department_id <150

**5-4:

select *

from

hr.employees t1,

hr.departments t2

where t1.department_id(+) = t2.department_id and t1.department_id = 50 and t2.department_id <150

**5-1到**5-4的oracle執行計畫分析結果相同:

對比:**5-1到**5-4的執行計畫相同。oracle是先對t1和t2中資料進行過濾後,再對結果集進行關聯查詢。且oracle對錶過濾內容進行了優化,對錶departments的查詢優化為 ty.department_id=50 而不是 ty.department_id<150

[url]

SQL巢狀查詢

訂單表orders 顧客表 customers 訂單商品表orderitems 列出訂購物品rgan01的所有顧客 select cust name,cust contact from customers where cust id in select cust id from orders whe...

sql查詢之巢狀查詢

巢狀查詢也叫子查詢,乙個select語句的查詢結果能夠作為另外乙個語句的輸入值。子查詢不但可以出現在where子句中,也能出現在from中作為乙個臨時表使用,而且還可以出現在select list中,作為乙個欄位值來返回。1 單行子查詢 單行子查詢的返回值只有一行資料。可以再主查詢語句的條件語句中引...

SQL 多表查詢 巢狀查詢

多表查詢 1 結構 select from where 2 笛卡爾積查詢 select from student,course select from student,sdept 3 查詢每個學院的學生情況 select from student,sdept where student.deptno...