作者lueichun (no anonymous)
看板Database
標題[SQL ] Oracle查詢語句效能
時間Tue Apr 30 11:13:41 2019
資料庫名稱:Oracle
資料庫版本:12c
內容/問題描述:我有一段同時join好幾張表格的查詢sql:
select * from t1,t2,t3,t4
where tl.t1a=t2.t2a
and t1.t1b=t2.t2b
and t1.t1c=t2.t2c
and t1.t1d=t2.t2d
and t3.t3a=t1.t1c
and t3.t3b='AAA'
and t2.t2e='BBB'
and t1.t1e=t4.t4a
這樣的語法,在查詢時的效率尚可,但是如果將上面的語法變成子查詢,
然後查出共有多少筆資料:
slect count(*) from
(
select * from t1,t2,t3,t4
where tl.t1a=t2.t2a
and t1.t1b=t2.t2b
and t1.t1c=t2.t2c
and t1.t1d=t2.t2d
and t3.t3a=t1.t1c
and t3.t3b='AAA'
and t2.t2e='BBB'
and t1.t1e=t4.t4a
)
寫成以上這樣,要去取得資料的總筆數,速度卻慢了將近10倍!!
第一筆SQL花3秒鐘,第二筆SQL卻花了整整30秒!!
請問這樣的話,有辦法只修改SQL本身,讓查詢總筆數的SQL可以加速嗎
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.198.51
※ 文章網址: https://www.ptt.cc/bbs/Database/M.1556594023.A.7BE.html
推 criky: 直接把1的*改成count(*)呢? 04/30 13:05
推 bij831: plan 有差很多?加paraller 試看看 05/03 11:21
推 terader: 建議不要用count(*),可以的話將*改成欄位名稱 05/08 15:16
→ terader: 另外注意一下是否有工具的問題,像在SQLDeveloper執行 05/08 15:19
→ terader: 你的第一種SQL的話,理論上會先吐50筆給你,但背後可能 05/08 15:20
→ terader: 還在執行,若可以的話可從EM去監控SQL執行狀況 05/08 15:20