記一次exists語句優化過程

2022-05-29 12:15:09 字數 1777 閱讀 4350

我們經常會遇到帶有exists這種形式的語句,官方描述如下,一般對應於子查詢語句返回的布林值:

在例子中可以看到:

select department_id

from departments d

where exists

(select * from employees e

where d.department_id

= e.department_id)

order by department_id;

原始sql如下:

update rich_custlokprofitnormal n

set n.flag = 1

where n.state = 1

and n.flag = 0

and exists

(select 1

from rich_custinterest t,

financial_lockissueprofit f,

financial_issue i

where n.custinterestid = t.id

and t.issueid = i.issueid

and f.issueid = i.issueid

and n.issueprofitid = f.serialno

and f.state = 1

and i.usebenchmark = 0

and t.type in (0, 1)

and ((t.type = 0 and t.maturitydate = :1 and

t.paydate is not null) or (t.type = 1 and t.paydate = :2))

and t.issueid = :3)

由於sql語句為update語句,最終在生產環境改為如下方式:

select count(*) from  rich_custlokprofitnormal n

where n.flag = 0

and exists (select 1

from financial_lockissueprofit f

where f.issueid=n.issueid

and n.issueprofitid = f.serialno

and f.state=1)

and exists

(select 1

from rich_custinterest t,

financial_issue i

where n.custinterestid = t.id

and t.issueid = i.issueid

and i.usebenchmark = 0

and t.type in (0, 1)

and ((t.type = 0 and t.maturitydate = to_date('20180813','yyyymmdd') and

t.paydate is not null) or (t.type = 1 and t.paydate = to_date('20180813','yyyymmdd')))

and t.issueid = 'amhqrh63dgc')

and n.state=1;

記一次oracle 優化過程

可能很多大牛都知道這個方法,但我是頭回遇到,因為專案原因,要寫很多查詢sql,對速度有要求,所以很注重sql語句的優化。像使用left join 速度會快一些等等一些算是比較常見的方法吧。近兩天做自測時發現了乙個問題,同樣一條語句,加了乙個條件竟然速度慢了那麼多,本身是乙個求彙總的sql語句,查全部...

記一次SQL優化

問題發生在關聯主表a 4w資料量 和副表b 4w資料量 關聯欄位都是openid 當時用的是 left join 直接跑sql,卡死 伺服器也是差 優化1 改left join 為join,兩者區別就是left join查詢時已主表為依據,該是幾條就幾條 就算副表沒有關聯的資料 join如果副表沒有...

記一次新建操作(insert)的優化過程

mysql的 insert常用方法 插入後返回插入成功後的主鍵 insert paramtype object sql cdata insert into ucdp budget plan gmt create,gmt modified,biz date,status name,biz code,s...