多表查詢,採用匹配的方式要比聯接方式要快

2021-05-24 11:10:14 字數 671 閱讀 8748

我覺得,多表查詢,採用匹配的方式要比聯接方式要快。即

select a.*,b.* from a,b where a.id=b.id

要比 select a.*,b.* from a inner join b on a.id=b.id

快。 這一點,在外部聯接上尤其明顯。

所以,遇到需要用到外部聯接,如

select a.* from aleft outer join b on a.id=b.id where a.id=某值

我會這樣寫

select a.* from a,b where a.id=b.id and a.id=某值

union all

select a.* from a where a.id=某值 and not exists(select 1 from b where id=a.id)

這還不是最優的,還可以這樣寫

with w as (select * from a where a.id=某值)

select w.* from w,b where w.id=b.id

union all

select w.* from w where not exists(select 1 from b where id=w.id)

究竟是不是這樣子呢?存疑。

多表查詢,採用匹配的方式要比聯接方式要快

我覺得,多表查詢,採用匹配的方式要比聯接方式要快。即 select a.b.from a,b where a.id b.id 要比 select a.b.from a inner join b on a.id b.id 快。這一點,在外部聯接上尤其明顯。所以,遇到需要用到外部聯接,如 select ...

sql語句的多表查詢方式

例如 按照 department id 查詢 employees 員工表 和 departments 部門表 的資訊。方式一 通用型 select from where select e.last name,e.department id,d.department name from employe...

SQL 多表查詢的幾種連線方式

建立資料庫 create database goodssystem go 使用資料庫 usegoodssystem go 建立商品型別表 create table goodstype io intprimary keyidentity 1,1 typename varchar 10 not null...