1

我需要生成使用预付费用户的详细信息报告。

基本上我有2张桌子。TBL_USER 和 TBL_PREPAID_DETAILS

tbl_user 仅列出用户,而 tbl_prepaid_details 将包含所有预付信息,例如借方、贷方、余额、总充值。

该报告将产生如下内容:-

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

在此之前,我将 SQL Server 用于数据库,但在 2012/2013 年,我们需要从 SQL Server 迁移到 Oracle 11g。

从之前的 sql 查询(SQL Server)中,我得出了以下查询(Oracle 11g)。我可以得到结果,但在 SQL Server 中只用了大约 5 分钟就完成了将近 30 分钟。它只有 500 多个用户

是我错过了什么吗?有没有简化版本来加快这个过程?

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;
4

1 回答 1

2

查看此页面以获取有关 Oracle 调优的一般概述帮助。 整体 Oracle 调优页面

Oracle 提供了大量工具来调整性能,包括可以添加哪些索引来改进特定查询。当我遇到运行缓慢的查询时,这是我做的第一件事。在您的情况下,我会考虑对您的查询运行解释计划并查看 Oracle 的建议。这是有关运行解释计划的文档。Oracle 解释计划

于 2012-11-14T17:06:08.317 回答