Count太慢了。我怎么能重构这个?

时间:2017-11-27 04:02:56

标签: sql oracle

我已经做了一段时间了......

我已经让内部查询运行得更快,但计数似乎正在减慢这一点..

我只需要在" type_id"发生> 13次,但这个数量似乎大大减缓了这一次......已经运行了一个多小时..

我怎么能重构这个以提高速度?它运行了很多数据..

我试图保持干燥以避免同一个表的多个子查询..但是,最终,我需要这个更快..

<select class="custom-select mb-2 col-md-4 col-12 mr-sm-2 mb-sm-0">

    <option ng-repeat="let server of servers" value="{{ server.display_name }}" ng-model="serverSelected">
<span ng-change="onServerSelect(serverSelected)">
{{server.display_name}}
</span>
</option>
    </select>

解释计划:

SELECT
    *
FROM
    (
        SELECT
            COUNT(
                p.type_id
            ) OVER(PARTITION BY
                p.type_id
            ) cnt,
            p.pay_key puk, a.pay_key auk, a.pay_code
FROM payment p INNER JOIN
     (SELECT a.*,
             ROW_NUMBER() OVER (PARTITION BY pay_key
                                ORDER BY allotment_uid
                               ) as seqnum
      FROM allotment a
      WHERE pay_code = 101 AND deleted = 'N'
     ) a
     ON a.pay_key = p.pay_key AND
        seqnum = 1

                    INNER JOIN submission submission ON
                submission.event_num = a.event_num
            AND
                submission.client_id = a.client_id
            AND
                submission.submitter_num = a.submitter_num
            AND
                submission.unit = a.unit
            AND
                submission.submission_num = a.submission_num
            INNER JOIN submitter submitter ON
                submitter.event_num = submission.event_num
            AND
                submitter.submitter_num = submission.submitter_num
            AND
                submitter.client_id = submission.client_id
        WHERE
                p.type_id <> 0
            AND
                p.type_id IS NOT NULL
            AND
                p.deleted = 'N'
            AND
                p.date_paid >= TO_DATE('2017-07-01','yyyy-mm-dd')
            AND
                p.date_paid < TO_DATE('2017-10-01','yyyy-mm-dd') + 1) a

                where a.cnt > 13;

1 个答案:

答案 0 :(得分:0)

尝试在连接之前计算过滤的行,例如

SELECT
      p.cnt, p.puk, a.pay_key auk, a.pay_code
FROM (
      SELECT
            COUNT(p.type_id) OVER (PARTITION BY p.type_id) cnt
          , p.pay_key                                      puk
      FROM payment
      WHERE p.type_id <> 0
      AND p.type_id IS NOT NULL
      AND p.deleted = 'N'
      AND p.date_paid >= TO_DATE('2017-07-01', 'yyyy-mm-dd')
      AND p.date_paid < TO_DATE('2017-10-01', 'yyyy-mm-dd') + 1
     ) P
      INNER JOIN (
            SELECT
                  a.*
                , ROW_NUMBER() OVER (PARTITION BY pay_key
                  ORDER BY allotment_uid
                  ) AS seqnum
            FROM allotment a
            WHERE pay_code = 101
            AND deleted = 'N'
      ) a ON a.pay_key = p.pay_key
            AND seqnum = 1
      INNER JOIN submission submission ON submission.event_num = a.event_num
            AND submission.client_id = a.client_id
            AND submission.submitter_num = a.submitter_num
            AND submission.unit = a.unit
            AND submission.submission_num = a.submission_num
      INNER JOIN submitter submitter ON submitter.event_num = submission.event_num
            AND submitter.submitter_num = submission.submitter_num
            AND submitter.client_id = submission.client_id
) d
WHERE d.cnt > 13;