简体   繁体   English

Oracle 11g 查询需要将近 30 分钟才能完成,但在 SQL Server 中大约需要 5 分钟

[英]Oracle 11g query took almost 30min to finish but around 5 min in SQL Server

I need to produce a details report of user that using prepaid.我需要生成使用预付费用户的详细报告。

Basically I have 2 tables.基本上我有2张桌子。 TBL_USER and also TBL_PREPAID_DETAILS TBL_USER 和 TBL_PREPAID_DETAILS

tbl_user only list the users while tbl_prepaid_details will have all the prepaid information such as debit, credit, balance, total topup. tbl_user 只列出用户,而 tbl_prepaid_details 将包含所有预付费信息,如借方、贷方、余额、总充值。

The report will be produced something like this:-报告将是这样产生的:-

FULLNAME USER_ID NEW_BALANCE CREATED DEPOSIT DEBIT TOTAL_DEPOSIT
USER1 1 250 21/05/2011 500 250 1000
USER2 2 250 21/05/2011 500 250 1000
USER3 3 250 21/05/2011 500 250 1000
USER4 4 250 21/05/2011 500 250 1000

Before this I'm using SQL server for database but for the year 2012/2013 we need to migrate from SQL server to Oracle 11g.在此之前,我使用 SQL 服务器作为数据库,但在 2012/2013 年我们需要从 SQL 服务器迁移到 Oracle 11g。

From the previous sql query (SQL Server), I come out with below query (Oracle 11g).从之前的 sql 查询 (SQL Server),我得出了以下查询 (Oracle 11g)。 I can get the result but it took almost 30min to finish while in SQL server only took around 5min.我可以得到结果,但花了将近 30 分钟才完成,而在 SQL 服务器中只花了大约 5 分钟。 Its only 500+ users它只有 500+ 用户

Is it i'm missing something??是我错过了什么吗?? Is there any simplified version to speed up the process ?是否有任何简化版本可以加快流程?

SELECT DISTINCT A.FULLNAME AS NAME,
                A.USER_ID AS ID,
                B.NAME,

  (SELECT T.NEW_BAL
   FROM
     (SELECT NEW_BAL,
             USER_ID
      FROM TBL_PREPAID_DETAILS
      WHERE DATE_INSERT BETWEEN '01/01/2010' AND '14/11/2012'
      ORDER BY PREPAID_DETAIL_ID DESC) T
   WHERE T.USER_ID = A.USER_ID
     AND ROWNUM = 1) AS NEW_BALANCE,

  (SELECT T2.DATE_INSERT
   FROM
     (SELECT DATE_INSERT,
             USER_ID
      FROM TBL_PREPAID_DETAILS
      WHERE DATE_INSERT BETWEEN '01/01/2010' AND '14/11/2012'
      ORDER BY DATE_INSERT DESC)T2
   WHERE T2.USER_ID = A.USER_ID
     AND ROWNUM = 1) AS DATE_INSERT,

  (SELECT T3.PREV_BAL
   FROM
     (SELECT PREV_BAL,
             USER_ID
      FROM TBL_PREPAID_DETAILS
      WHERE DATE_INSERT BETWEEN '01/01/2010' AND '14/11/2012'
      ORDER BY PREPAID_DETAIL_ID ASC)T3
   WHERE T3.USER_ID = A.USER_ID
     AND ROWNUM = 1) AS DEPOSIT,

  (SELECT SUM(T4.CREDIT)
   FROM
     (SELECT CREDIT,
             USER_ID
      FROM TBL_PREPAID_DETAILS
      WHERE DATE_INSERT BETWEEN '01/01/2010' AND '14/11/2012'
      ORDER BY PREPAID_DETAIL_ID ASC)T4
   WHERE T4.USER_ID = A.USER_ID
     AND ROWNUM = 1) AS DEBIT,

  (SELECT SUM(T5.DEBIT + T5.PREV_BAL)
   FROM
     (SELECT DEBIT,
             PREV_BAL,
             USER_ID
      FROM TBL_PREPAID_DETAILS
      WHERE DATE_INSERT BETWEEN '01/01/2010' AND '14/11/2012'
      ORDER BY PREPAID_DETAIL_ID ASC)T5
   WHERE T5.USER_ID = A.USER_ID
     AND ROWNUM = 1) AS TOTAL_DEPOSIT
FROM TBL_USER A
LEFT JOIN TBL_PREPAID_DETAILS C ON A.USER_ID = C.USER_ID
LEFT JOIN TBL_ORGANIZATION_INFO B ON A.ORGANIZATION_ID = B.ORGANIZATION_ID
WHERE C.DATE_INSERT BETWEEN '01/01/2010' AND '14/11/2012'
  AND (A.USER_ID NOT IN ('xxx',
                         'xxx',
                         'xxx'))
GROUP BY A.FULLNAME,
         A.USER_ID,
         C.DATE_INSERT,
         B.NAME
ORDER BY A.FULLNAME;

Have a look at this page for general overview help on Oracle Tuning.请查看此页面以获取有关 Oracle Tuning 的一般概述帮助。 Overall Oracle Tuning Page整体 Oracle 调优页面

Oracle provides a great deal of tools to tune performance, including what indexes could be added to improve a particular query. Oracle 提供了大量工具来调整性能,包括可以添加哪些索引来改进特定查询。 It is one of the first things I do when I encounter a slow running query.当我遇到运行缓慢的查询时,这是我做的第一件事。 In your case, I would consider running an explain plan on your query and see what Oracle recommends.在您的情况下,我会考虑对您的查询运行解释计划并查看 Oracle 的建议。 Here is the documentation on running an explain plan.这是有关运行解释计划的文档。 Oracle Explain Plan Oracle 解释计划

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM