[SQL ] Oracle查詢語句效能

看板Database作者 (no anonymous)時間5年前 (2019/04/30 11:13), 編輯推噓3(303)
留言6則, 3人參與, 5年前最新討論串1/1
資料庫名稱: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

04/30 13:05, 5年前 , 1F
直接把1的*改成count(*)呢?
04/30 13:05, 1F

05/03 11:21, 5年前 , 2F
plan 有差很多?加paraller 試看看
05/03 11:21, 2F

05/08 15:16, 5年前 , 3F
建議不要用count(*),可以的話將*改成欄位名稱
05/08 15:16, 3F

05/08 15:19, 5年前 , 4F
另外注意一下是否有工具的問題,像在SQLDeveloper執行
05/08 15:19, 4F

05/08 15:20, 5年前 , 5F
你的第一種SQL的話,理論上會先吐50筆給你,但背後可能
05/08 15:20, 5F

05/08 15:20, 5年前 , 6F
還在執行,若可以的話可從EM去監控SQL執行狀況
05/08 15:20, 6F
文章代碼(AID): #1SnxrdU- (Database)