記一次解決oracle sql效能瓶頸的問題

2021-08-16 20:12:05 字數 3439 閱讀 3243

先上sql:

select

(select m.album_id from album_r_music am,album m where am.music_id = m.music_id and am.album_id = m.album_id and rownum = 1) album_id,

(select m.album_name from album_r_music am,album m where am.music_id = m.music_id and am.album_id = m.album_id and rownum = 1) album_name,

(select to_char(wm_concat(t.artist_id || ':' ||

t.artist_name))

from music_r_artist mr, artist t

where m.music_id = mr.music_id(+)

and mr.artist_id = t.artist_id

and mr.artist_type = 1) artist_id,

m.music_id,

m.length,

m.music_name,

m.singer_name,

m.rank_num,

m.publish_year,

m.status

from

"tagid != null">

tag ta,

object_r_tag o,

music_r_artist t,

artist_r_type y,

music m

t.artist_id = y

.artist_id

andy.type_id = '1'

and t.music_id = m.music_id

and m.status = '1'

and t.artist_type = 1

"tagid != null">

and m.music_id = o.object_id(+)

and ta.tag_id = o.tag_id

and ta.tag_id = #

and o.object_type = 1

這個sql有九百多萬條資料,分頁採用的mybatis框架的rowbounds類。會把整個結果集掃瞄一次,很耗時間,大概分一次頁要5s左右,到後面幾頁可能需要幾分鐘。。。

解決辦法是將要關聯的表寫成儲存過程,將music_r_artist t,artist_r_type y,music m這三張需要關聯的表融合成一張大的表,並且將rownum作為一列,加上索引。儲存過程會提前把需要的資料跑出來,這樣我們在查詢的時候去操作musicianas,如下:

create

orreplace

procedure migu_musician_songs

asbegin

execute

immediate

'drop table temp_musicianas';

execute immediate 'create

table temp_musicianas as

select m.*,rownum rn

from music m

where

exists

(select

1from (select music_id

from (select artist_id from artist_r_type where type_id = 1) y,

music_r_artist t

where y.artist_id = t.artist_id) t

where t.music_id = m.music_id

and m.status = 1)';

execute immediate 'rename musicianas to temp_musicianas1';

execute immediate 'rename temp_musicianas to musicianas';

execute immediate '

create index ind_musicianas_rn on musicianas(rn)';

exception

when others then

rollback;

end;

這樣我們可以通過行號,也就是rownum去做分頁,取得從startindex到endindex的資料,就是我們需要的這一頁的資料。sql如下:

select

(select m.album_id from album_r_music am,album m where am.music_id = m.music_id and am.album_id = m.album_id and rownum = 1) album_id,

(select m.album_name from album_r_music am,album m where am.music_id = m.music_id and am.album_id = m.album_id and rownum = 1) album_name,

(select to_char(wm_concat(t.artist_id || ':' ||

t.artist_name))

from music_r_artist mr, artist t

where m.music_id = mr.music_id(+)

and mr.artist_id = t.artist_id

and mr.artist_type = 1) artist_id,

m.music_id,

m.length,

m.music_name,

m.singer_name,

m.rank_num,

m.publish_year,

m.status

from

"tagid != null">

object_r_tag o,

musicianas m

1= 1

"tagid != null">

and m.music_id = o.object_id(+)

and o.tag_id = #

and o.object_type = 1

and m.rn between $ and $

極大提公升了效率:

記一次解決MQ不消費問題

mq不消費,mq執行緒全部處於等待狀態 專案重啟之後問題解決 jvm記憶體狀態正常 fullgc時間持續加長 cpu 100 查詢專案程序中所有的執行緒資訊,檢視是否有死鎖 檢視例項程序 tomcat的話bootstrap那個就是,springboot專案看啟動類名 jps 檢視程序狀態 產看是否有...

nginx記一次解決跨域問題的記錄

在nginx中配置proxy pass時,當在後面的url加上了 相當於是絕對根路徑,則nginx不會把location中匹配的路徑部分 走 如果沒有 則會把匹配的路徑部分也給 走。下面四種情況分別用http 192.168 1.4 proxy test.html 進行訪問。第一種 location...

記一次oracle sql調優過程

這裡兩天都在對一條sql進行調優。該sql並不複雜,類似於 select from some view union all select from some table where datetime d1 and datetime d2 and 底層使用ibatis2.1.6 oracle 10g。...