2

我正在应用风景宝石为 Postgres 数据库创建数据库视图。我有一个包含 73k 记录的订单 (ops_orders) 表和属于具有 170k 记录的订单的订单行项目 (ops_order_line_items)。

问题是查询的 WITH 语句执行时间过长,以至于它无限期地挂起:

WITH 
excluded_order_ids AS (
  SELECT ord.id
  FROM ops_orders ord
  WHERE NOT EXISTS (
    SELECT 1
    FROM ops_order_line_items oli
    WHERE oli.processing_status != 6 -- PENDING
      AND oli.order_id = ord.id
    )

  UNION ALL

  SELECT ord.id
  FROM ops_orders ord
  WHERE NOT EXISTS (
    SELECT 1
    FROM ops_order_line_items oli
    WHERE oli.processing_status != 1 -- FULFILLED
      AND oli.order_id = ord.id
    )

  UNION ALL

  SELECT ord.id
  FROM ops_orders ord
  WHERE NOT EXISTS (
    SELECT 1
    FROM ops_order_line_items oli
    WHERE oli.processing_status != 4 -- CANCELLED
      AND oli.order_id = ord.id
    )

  UNION ALL

  SELECT ord.id
  FROM ops_orders ord
  WHERE NOT EXISTS (
    SELECT 1
    FROM ops_order_line_items oli
    WHERE oli.line_item_type != 1 -- RESENT
      AND oli.order_id = ord.id
    )
),
included_order_ids AS (
  SELECT ord.id
  FROM ops_orders ord
  WHERE NOT EXISTS (
    SELECT 1
    FROM ops_order_line_items oli
    WHERE oli.processing_status != 3 -- PENDING
      AND oli.deadline > (current_date + 1)
      AND oli.order_id = ord.id
    )
)

SELECT oli.*
FROM ops_order_line_items oli
JOIN ops_purchase_line_items pli ON pli.id = oli.purchase_line_item_id
JOIN ops_purchase_orders po ON po.id = pli.purchase_order_id
JOIN ops_orders ord ON ord.id = oli.order_id
WHERE ((oli.processing_status IN (0, 2) AND oli.deadline <= (current_date + 3)) -- status: ?, ORDERED
  OR (oli.processing_status IN (5, 7) AND oli.deadline <= current_date) -- status: DROPSHIP, PACKING
    OR (oli.processing_status IN (2) AND po.countdown_date > po.created_at) -- status: FULFILLED
    OR (oli.order_id IN (SELECT id FROM included_order_ids)))
    AND oli.order_id NOT IN (SELECT id FROM excluded_order_ids)
ORDER BY ord.order_number DESC, oli.created_at ASC

但是,当我更改为:

WITH
included_order_ids AS (
  SELECT ord.id
  FROM ops_orders ord
  WHERE NOT EXISTS (
    SELECT 1
    FROM ops_order_line_items oli
    WHERE oli.processing_status != 3 -- PENDING
      AND oli.deadline > (current_date + 1)
      AND oli.order_id = ord.id
    )
)
SELECT oli.*
FROM ops_order_line_items oli
JOIN ops_purchase_line_items pli ON pli.id = oli.purchase_line_item_id
JOIN ops_purchase_orders po ON po.id = pli.purchase_order_id
JOIN ops_orders ord ON ord.id = oli.order_id
WHERE ((oli.processing_status IN (0, 2) AND oli.deadline <= (current_date + 3)) -- status: ?, ORDERED
  OR (oli.processing_status IN (5, 7) AND oli.deadline <= current_date) -- status: DROPSHIP, PACKING
  OR (oli.processing_status IN (2) AND po.countdown_date > po.created_at) -- status: FULFILLED    
  OR (oli.order_id IN (SELECT id FROM included_order_ids)))
  AND oli.order_id NOT IN (
    SELECT ord.id
    FROM ops_orders ord
    WHERE NOT EXISTS (
      SELECT 1
      FROM ops_order_line_items oli
      WHERE oli.processing_status != 6 -- PENDING
        AND oli.order_id = ord.id
      ))
  AND oli.order_id NOT IN (
    SELECT ord.id
    FROM ops_orders ord
    WHERE NOT EXISTS (
      SELECT 1
      FROM ops_order_line_items oli
      WHERE oli.processing_status != 1 -- FULFILLED
        AND oli.order_id = ord.id
      ))
  AND oli.order_id NOT IN (
    SELECT ord.id
    FROM ops_orders ord
    WHERE NOT EXISTS (
      SELECT 1
      FROM ops_order_line_items oli
      WHERE oli.processing_status != 4 -- CANCELLED
        AND oli.order_id = ord.id
      ))
  AND oli.order_id NOT IN (
    SELECT ord.id
    FROM ops_orders ord
    WHERE NOT EXISTS (
      SELECT 1
      FROM ops_order_line_items oli
      WHERE oli.line_item_type != 1 -- RESENT
        AND oli.order_id = ord.id
      ))
ORDER BY ord.order_number DESC, oli.created_at ASC

Postgres 中的 WITH 语句是否性能不佳?我进行了研究,但没有发现任何有关 WITH 语句性能不佳的文件。期待有用的解释。

4

0 回答 0