oracle之優化一中用group by優化distinct
SELECT DISTINCT 'AMEND_NEW', reporttitle, reportsubtitle, cab_cab_transactions.branchcode, cab_cab_transactions.prtfo_cd, cab_cab_transactions.sstm_scrty_id, cab_cab_transactions.sstm_trx_id, cab_cab_transactions.trde_dttm, cab_cab_transactions.efcte_dttm, cab_cab_transactions.due_stlmnt_dt, cab_cab_transactions.cncl_efcte_dttm, cab_cab_transactions.trde_sstm_id, cab_cab_transactions.trx_type_cd, cab_cab_transactions.trx_type_dscrn, cab_cab_transactions.trx_subtype_cd, cab_cab_transactions.trde_stat_flg, cab_cab_transactions.csh_cr_dr_indcr, cab_cab_transactions.long_shrt_indcr, cab_cab_transactions.lcl_crncy, cab_cab_transactions.stlmt_crncy, cab_cab_transactions.nomin_qty, cab_cab_transactions.price, cab_cab_transactions.lcl_cst, cab_cab_transactions.prtfo_cst, cab_cab_transactions.lcl_book_cst, cab_cab_transactions.prtfo_book_cst, cab_cab_transactions.lcl_sell_prcds, cab_cab_transactions.prtfo_sell_prcds, cab_cab_transactions.lcl_gnls, cab_cab_transactions.prtfo_gnls, cab_cab_transactions.lcl_acrd_intrt, cab_cab_transactions.prtfo_acrd_intrt, cab_cab_transactions.stlmt_crncy_stlmt_amt, cab_cab_transactions.lcl_net_amt, cab_cab_transactions.prtfo_net_amt, cab_cab_transactions.fx_bght_amt, cab_cab_transactions.fx_sold_amt, cab_cab_transactions.prtfo_crncy_stlmt_amt, cab_cab_transactions.prtfo_net_incme, cab_cab_transactions.dvnd_crncy_net_incme, cab_cab_transactions.dvnd_type_cd, cab_cab_transactions.lcl_intrt_pd_rec, cab_cab_transactions.prtfo_intrt_pd_rec, cab_cab_transactions.lcl_dvdnd_pd_rec, cab_cab_transactions.prtfo_dvdnd_pd_rec, cab_cab_transactions.lcl_sundry_inc_pd_rec, cab_cab_transactions.prtfo_sundry_inc_pd_rec, cab_cab_transactions.bnk_csh_cptl_secid, cab_cab_transactions.bnk_csh_inc_secid, cab_cab_transactions.reportdate, cab_cab_transactions.filename, sysdate, 'e483448' FROM cab_cfg_trx_type_mapping RIGHT JOIN(cab_cab_tran_adjustments INNER JOIN cab_cab_transactions ON(cab_cab_transactions.branchcode = cab_cab_tran_adjustments.branchcode ) AND(cab_cab_tran_adjustments.sstm_trx_id = cab_cab_transactions.sstm_trx_id)) ON(cab_cfg_trx_type_mapping.cab_trx_type_cd = cab_cab_transactions.trx_type_cd) AND(nvl(cab_cfg_trx_type_mapping.cab_trx_subtype_cd,' ') = nvl(cab_cab_transactions.trx_subtype_cd,' ') AND (cab_cfg_trx_type_mapping.branchcode=cab_cab_transactions.branchcode)) WHERE cab_cab_transactions.prtfo_cd IN (SELECT DISTINCT prtfo_cd FROM cab_cab_valuations_working WHERE created_by = 'e483448' AND branchcode='ISA') AND cab_cab_tran_adjustments.efcte_dttm > '2011-07-31' AND cab_cab_tran_adjustments.efcte_dttm <= '2011-08-31' AND eff_trde_stat_flg <> 'X' AND cab_cab_transactions.branchcode = 'ISA' AND cab_cab_tran_adjustments.branchcode = 'ISA' AND(cab_cfg_trx_type_mapping.cab_reportgroup = 'CABValuation' OR cab_cfg_trx_type_mapping.cab_reportgroup IS NULL)
問題在distinct上面,它會導致對全表掃描,而且會導致排序,然後刪除重複的記錄,所以速度很慢,因此需要優化distinct。查了不少資料,並逐一嘗試,最後發現了一個非常可觀的優化結果,用group by。語句如下:
SELECT 'AMEND_NEW', reporttitle, reportsubtitle, cab_cab_transactions.branchcode, cab_cab_transactions.prtfo_cd, cab_cab_transactions.sstm_scrty_id, cab_cab_transactions.sstm_trx_id, cab_cab_transactions.trde_dttm, cab_cab_transactions.efcte_dttm, cab_cab_transactions.due_stlmnt_dt, cab_cab_transactions.cncl_efcte_dttm, cab_cab_transactions.trde_sstm_id, cab_cab_transactions.trx_type_cd, cab_cab_transactions.trx_type_dscrn, cab_cab_transactions.trx_subtype_cd, cab_cab_transactions.trde_stat_flg, cab_cab_transactions.csh_cr_dr_indcr, cab_cab_transactions.long_shrt_indcr, cab_cab_transactions.lcl_crncy, cab_cab_transactions.stlmt_crncy, cab_cab_transactions.nomin_qty, cab_cab_transactions.price, cab_cab_transactions.lcl_cst, cab_cab_transactions.prtfo_cst, cab_cab_transactions.lcl_book_cst, cab_cab_transactions.prtfo_book_cst, cab_cab_transactions.lcl_sell_prcds, cab_cab_transactions.prtfo_sell_prcds, cab_cab_transactions.lcl_gnls, cab_cab_transactions.prtfo_gnls, cab_cab_transactions.lcl_acrd_intrt, cab_cab_transactions.prtfo_acrd_intrt, cab_cab_transactions.stlmt_crncy_stlmt_amt, cab_cab_transactions.lcl_net_amt, cab_cab_transactions.prtfo_net_amt, cab_cab_transactions.fx_bght_amt, cab_cab_transactions.fx_sold_amt, cab_cab_transactions.prtfo_crncy_stlmt_amt, cab_cab_transactions.prtfo_net_incme, cab_cab_transactions.dvnd_crncy_net_incme, cab_cab_transactions.dvnd_type_cd, cab_cab_transactions.lcl_intrt_pd_rec, cab_cab_transactions.prtfo_intrt_pd_rec, cab_cab_transactions.lcl_dvdnd_pd_rec, cab_cab_transactions.prtfo_dvdnd_pd_rec, cab_cab_transactions.lcl_sundry_inc_pd_rec, cab_cab_transactions.prtfo_sundry_inc_pd_rec, cab_cab_transactions.bnk_csh_cptl_secid, cab_cab_transactions.bnk_csh_inc_secid, cab_cab_transactions.reportdate, cab_cab_transactions.filename, sysdate, 'e483448' FROM cab_cfg_trx_type_mapping RIGHT JOIN(cab_cab_tran_adjustments INNER JOIN cab_cab_transactions ON(cab_cab_transactions.branchcode = cab_cab_tran_adjustments.branchcode ) AND(cab_cab_tran_adjustments.sstm_trx_id = cab_cab_transactions.sstm_trx_id)) ON(cab_cfg_trx_type_mapping.cab_trx_type_cd = cab_cab_transactions.trx_type_cd) AND(nvl(cab_cfg_trx_type_mapping.cab_trx_subtype_cd,' ') = nvl(cab_cab_transactions.trx_subtype_cd,' ') AND (cab_cfg_trx_type_mapping.branchcode=cab_cab_transactions.branchcode)) WHERE cab_cab_transactions.prtfo_cd IN (SELECT DISTINCT prtfo_cd FROM cab_cab_valuations_working WHERE created_by = 'e483448' AND branchcode='ISA') AND cab_cab_tran_adjustments.efcte_dttm > '2011-07-31' AND cab_cab_tran_adjustments.efcte_dttm <= '2011-08-31' AND eff_trde_stat_flg <> 'X' AND cab_cab_transactions.branchcode = 'ISA' AND cab_cab_tran_adjustments.branchcode = 'ISA' AND(cab_cfg_trx_type_mapping.cab_reportgroup = 'CABValuation' OR cab_cfg_trx_type_mapping.cab_reportgroup IS NULL) GROUP BY reporttitle, reportsubtitle, cab_cab_transactions.branchcode, cab_cab_transactions.prtfo_cd, cab_cab_transactions.sstm_scrty_id, cab_cab_transactions.sstm_trx_id, cab_cab_transactions.trde_dttm, cab_cab_transactions.efcte_dttm, cab_cab_transactions.due_stlmnt_dt, cab_cab_transactions.cncl_efcte_dttm, cab_cab_transactions.trde_sstm_id, cab_cab_transactions.trx_type_cd, cab_cab_transactions.trx_type_dscrn, cab_cab_transactions.trx_subtype_cd, cab_cab_transactions.trde_stat_flg, cab_cab_transactions.csh_cr_dr_indcr, cab_cab_transactions.long_shrt_indcr, cab_cab_transactions.lcl_crncy, cab_cab_transactions.stlmt_crncy, cab_cab_transactions.nomin_qty, cab_cab_transactions.price, cab_cab_transactions.lcl_cst, cab_cab_transactions.prtfo_cst, cab_cab_transactions.lcl_book_cst, cab_cab_transactions.prtfo_book_cst, cab_cab_transactions.lcl_sell_prcds, cab_cab_transactions.prtfo_sell_prcds, cab_cab_transactions.lcl_gnls, cab_cab_transactions.prtfo_gnls, cab_cab_transactions.lcl_acrd_intrt, cab_cab_transactions.prtfo_acrd_intrt, cab_cab_transactions.stlmt_crncy_stlmt_amt, cab_cab_transactions.lcl_net_amt, cab_cab_transactions.prtfo_net_amt, cab_cab_transactions.fx_bght_amt, cab_cab_transactions.fx_sold_amt, cab_cab_transactions.prtfo_crncy_stlmt_amt, cab_cab_transactions.prtfo_net_incme, cab_cab_transactions.dvnd_crncy_net_incme, cab_cab_transactions.dvnd_type_cd, cab_cab_transactions.lcl_intrt_pd_rec, cab_cab_transactions.prtfo_intrt_pd_rec, cab_cab_transactions.lcl_dvdnd_pd_rec, cab_cab_transactions.prtfo_dvdnd_pd_rec, cab_cab_transactions.lcl_sundry_inc_pd_rec, cab_cab_transactions.prtfo_sundry_inc_pd_rec, cab_cab_transactions.bnk_csh_cptl_secid, cab_cab_transactions.bnk_csh_inc_secid, cab_cab_transactions.reportdate, cab_cab_transactions.filename
最後執行時間只有15.1秒,快了60多倍,不得不說這優化效果還是很可觀的。不過查了很多資料,仍然沒有發現合理地解釋:為什麼distinct 和group by的效率會有這麼大差別。查的很多資料,講的基本都是兩者相差不大,實現也差不多。有待解決。
ps:這個兩個sql很複雜,僅看頭尾即可。
相關推薦
oracle之優化一中用group by優化distinct
今天mentor給了一個sql語句優化的任務。(環境是sql developer)有一個語句執行很慢,查詢出來的結果有17544條記錄,但需970秒,速度很慢。語句是這樣的:SELECT DISTINCT 'AMEND_NEW', rep
oracle之優化一用group by或exists優化distinct
今天mentor給了一個sql語句優化的任務。(環境是sql developer)有一個語句執行很慢,查詢出來的結果有17544條記錄,但需970秒,速度很慢。語句是這樣的: SELECT DISTINCT 'AMEND_NEW', r
資料庫優化SQL優化之SELECT優化 ——group by 優化
在資料庫查詢中,group by語句經常使用,而這個語句的使用是最耗效能的,按常理, 我們生活中要這樣做也很麻煩,有兩種情形: 1、有索引的情況 2、無索引的情況 對於第一種情況,如果在生活中要做這樣的事情,很惱火,我們正常的想法是,先把所有
mysql的order by,group by和distinct優化
order by,group by和distinct三類操作是在mysql中經常使用的,而且都涉及到排序,所以就把這三種操作放在一起介紹。 order by的實現與優化 order by的實現有兩種方式,主要就是按用沒用到索引來區分: 1. 根據索引欄位排序,利用索引取出的
Group by 優化
一個標準的 Group by 語句包含排序、分組、聚合函式,比如 select a,count(*) from t group by a ; 這個語句預設使用 a 進行排序。如果 a 列沒有索引,那麼就會建立臨時表來統計 a和 count(*),然後再通過
oracle:ORA-00979 不是 GROUP BY 表示式”
具體的解釋,見之前我看的那篇部落格: 摘錄: ORA-00979 不是 GROUP BY 表示式”這個錯誤,和我前面介紹的另外一個錯誤ORA-00937一樣使很多初學oracle的人愛犯的。 我在介紹使用聚合函式中用group by來分組資料時特別說明了selec
oracle中的having ,group by ,where之間聯絡與區別
having子句與where有相似之處但也有區別,都是設定條件的語句。 在查詢過程中聚合語句(sum,min,max,avg,count)要比having子句優先執行.而where子句在查詢過程中執行優先級別優先於聚合語句(sum,min,max,avg,count)。 簡
悲催的MSSQL 2 Oracle之旅(一)
好久沒有來這裡了,還就沒有發表文章了,這個月初,老闆突然決定要把整個專案轉為Ora平臺,可是苦於公司人手不夠,只能我自己來了,Ora以前只是接觸過皮毛,心裡還真是沒有底,現在把開發過程中碰到的問題記錄下來: 第一天:表結構的匯入,這算是骨架吧,剛開始嘗試過使用MSSQL的匯入
mysql ORDER BY,GROUP BY 和DISTINCT原理
如果 str reat 個數 需要 是我 如何 where子句 三種 前言 除了常規的Join語句之外,還有一類Query語句也是使用比較頻繁的,那就是ORDERBY,GROUP BY以及DISTINCT這三類查詢。考慮到這三類查詢都涉及到數據的排序等操作,所以我將他們放在
Group by 和distinct對比
** Group by 和distinct對比** CREATE TABLE sbtest1 (id int(11) NOT NULL AUTO_INCREMENT,k int(11) NOT NULL DEFAULT '0',c char(120) NOT NULL DEFAULT '',pad cha
mysql GROUP BY 代替DISTINCT 遇到的問題及解決
近日在做一個數據分析時,發現DISTINCT比較慢,想通過group by來替代。然而替代並非一帆風順,在替代過程中,發現對於重複資料,group by會取第一次出現的記錄。為得到我想要的統計資料,折騰了一番。下面用例項來描述我的問題及調整過程。 場景:有一張表,記錄手機使
Oracle使用row_number() over (partition order by)和DISTINCT去除重複記錄
最近做的一個模組涉及到8張表的聯合查詢,由於這8張表中有很多主從表的關聯,結果在使用模糊查詢的時候查詢結果集出現了重複記錄。如下:
資料庫查詢去重group by和distinct的理解
前言 在使用mysql時,有時需要查詢出某個欄位不重複的記錄,雖然mysql提供 有distinct這個關鍵字來過濾掉多餘的重複記錄只保留一條,但往往只用它來返回不重複記錄的條數,而不是用它來返回不重記錄的所有值。其原因是 distinct只能返回它的目標欄位
group by與distinct有何區別
他們的功能基本上是不一樣的。 distinct消除重複行。 group by是分組語句。 舉例來說可能方便一點。 A表id numa 1b 2c 3a 4c 7d 3e 5 如果只選出id列,用distinct和group by 一樣
group by與distinct的區別
舉例來說可能方便一點。A表id numa 1b 2c 3a 4c 7d 3e 5如果只選出id列,用distinct和group by 一樣的。select distinct(id) from A;i
MYSQL優化-之GROUP BY
plain 2種 borde ext emp 例如 般的 查詢條件 屬性。 轉載:https://my.oschina.net/heguangdong/blog/38567 在web應用中,提倡sql簡單,避免復雜度。所以在我們公司的應用中看不到jon,子查詢等語句的存在,
第十九篇:Mysql兩次Group by和ip轉數字作比較的一次優化
業務場景:兩張表,ipconfig_group表存了單位和 ip 起始段資訊visit_info表儲存了訪問次數,失敗次數,訪問流量,使用者ip等資訊兩張表的關係為: 一個部門下有若干ip段,對應的下面表的visitorip,每個visitorip有訪問次數,失敗
MySQL查詢優化之group by的優化
原文地址:https://dev.mysql.com/doc/refman/5.7/en/group-by-optimization.html 譯文: 8.2.1.15 GROUP BY優化 滿足GROUP BY子句的最常用方法是掃描整個表並建立一個新的臨時表,其中每個組的所有
mysql資料庫優化之GROUP BY(聚合) 函式
group by 功能:功能:分類彙總的時候使用,表示按欄位分組。該語句對查詢結果按group by後的值分組,所有具有相同值元組為一組。 例:sql語句 select id,name,sum(num) count from st grou
Oracle效能優化之高階SQL優化(一)
使用基於規則的優化器(CBO)時,Oracle解析器按照從右到左的順序處理FROM子句的表明,即FROM子句中最後的表(驅動表)會最先被處理。 當FROM子句包含多個表時,建議將記錄最少的表(一般是字典表)放在最後面。當Oracle處理多個表時,一般採用排序或合併的方式連線這些表,系統首先會掃描FR