簡體   English   中英

非常慢的oracle select語句

[英]very slow oracle select statement

我有一個select語句,如果數據包含數十萬,但執行時間非常慢,需要超過15分鍾。 是否可以改善此select語句的執行時間。

select a.levelP,
       a.code,
       a.descP,
       (select nvl(SUM(amount),0) from ca_glopen where code = a.code and acc_mth = '2016' ) ocf,
       (select nvl(SUM(amount),0) from ca_glmaintrx where code = a.code and to_char(doc_date,'yyyy') = '2016' and to_char(doc_date,'yyyymm') < '201601') bcf,
       (select nvl(SUM(amount),0) from ca_glmaintrx where jum_amaun > 0 and code = a.code and to_char(doc_date,'yyyymm') = '201601' ) debit,
       (select nvl(SUM(amount),0) from ca_glmaintrx where jum_amaun < 0 and code = a.code and to_char(doc_date,'yyyymm') = '201601' ) credit
from ca_chartAcc a
where a.code is not null
order by to_number(a.code), to_number(levelP)

請幫助我提高我的查詢和結果的速度.TQ

您的主要問題是大多數子查詢都使用搜索條件中的函數,包括日期中的一些笨拙函數。 通過提供實際日期(一個月的范圍通常占總行數的一小部分,因此很可能達到指數),可以更好地翻轉並明確限定預期范圍。

SELECT Chart.levelP, Chart.code, Chart.descP,
       COALESCE(GL_SUM.ocf, 0),
       COALESCE(Transactions.bcf, 0),
       COALESCE(Transactions.debit, 0),
       COALESCE(Transactions.credit, 0),
FROM ca_ChartAcc Chart
LEFT JOIN (SELECT code, SUM(amount) AS ocf
           FROM ca_GLOpen
           WHERE acc_mth = '2016') GL_Sum
       ON GL_Sum.code = Chart.code
LEFT JOIN (SELECT code, 
                  SUM(amount) AS bcf,
                  SUM(CASE WHEN amount > 0 THEN amount) AS debit,
                  SUM(CASE WHEN amount < 0 THEN amount) AS credit,
           FROM ca_GLMainTrx
           WHERE doc_date >= TO_DATE('2016-01-01')
                 AND doc_date < TO_DATE('2016-02-01')) Transactions
        ON Transactions.code = Chart.code
WHERE Chart.code IS NOT NULL
ORDER BY TO_NUMBER(Chart.code), TO_NUMBER(Chart.levelP)

如果您只需要幾個代碼,那么將這些值推送到子查詢中可能會產生更好的結果(盡管請注意優化器可能會為您執行此操作)。
可以從ORDER BY子句中刪除對TO_NUMBER(...)的調用; 但是,這取決於值的格式,因為它們的編碼方式可能會改變結果的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM