sql效能問題start with

2021-08-30 06:21:49 字數 1387 閱讀 3747

監控資料庫後台發現疑似問題sql:

select distinct t1.gwbh, t1.gwmc, t1.p_gwbh, t1.reservation01, t1.kg_id

from t_grkh_khfa_gw t1

where t1.lrdw_id = :1

and t1.khfa_id = :2

start with t1.p_gwbh = 『0000』

connect by prior t1.gwbh = t1.p_gwbh

order by t1.reservation01

對應的變數值是不斷變化,推測是處理逐級載入樹形選單。

但實際測試傳入的變數值進行查詢,發現查詢速度極為差,平均高達85秒才出查詢結果,而查詢結果卻只有<=50條,而對應的t_grkh_khfa_gw表資料只有272,880條記錄.這個是有嚴重的效能問題,初步推斷此問題可能是導致載入樹形選單出現問題的原因。

發現問題的原因為where 條件啟用範圍是在start with的遞迴查詢中是將所有遞迴查詢出的結果中進行過濾。而不是過濾出資料在進行遞迴查詢。

而抽樣不加where條件的此遞迴的查詢資料結果集記錄數高達7,593,588條,耗時85秒。

select count(1)

from t_grkh_khfa_gw t1

start with t1.p_gwbh = 『0000』

connect by prior t1.gwbh = t1.p_gwbh

order by t1.reservation01

然後where 條件在從這700萬的中間結果集中過濾出幾十條資料,整個語句的效能消耗在這個700萬的中間結果集的生成上,從而導致效能問題。

針對排查出的這個疑似原因,優化如下,先按照where條件過濾出所需資料,然後進行遞迴查詢,0.047秒的毫秒級就可完成查詢結果。

修正sql如下:

select distinct t2.gwbh,

t2.gwmc,

t2.p_gwbh,

t2.gwbh,

t2.reservation01,

t2.kg_id

from (select t1.gwbh,

t1.gwmc,

t1.p_gwbh,

t1.reservation01,

t1.kg_id

from t_grkh_khfa_gw t1

where t1.lrdw_id = :1

and t1.khfa_id = :2) t2

start with t2.p_gwbh = 『0000』

connect by prior t2.gwbh = t2.p_gwbh

order by t2.reservation01

distinct:去重

SQL的效能問題

a.分析sql的執行計畫 explain,可以模擬sql優化器執行sql語句 b.mysql的查詢優化會干擾我們的優化 查詢優化計畫 explain sql語句 explain select from shine id select type table type possible keys key...

效能測試中SQL引起的效能問題

在做 系統效能測試的時候,100個併發對該業務進行查詢操作,平均響應時間都大於10s,經過排查,應用伺服器資源,資料庫資源,io,網路都正常,查詢 相關業務sql,其中一條sql執行耗時很長,該sql就是對應查詢業務的sql,具體sql為公司機密,不便透漏。sql大致為 select count f...

SQL效能調節

一 sql的執行過程 1.建立游標 create a cursor 2.分析語句 parse the statement 3.描述查詢結果 4.定義查詢的輸出資料 5.繫結變數 6.並行執行語句 7.執行語句 8.取出查詢的行 9.關閉游標 二 cbo規則 cbo i 0 cpu network 三...