具有WITH语句时,Postgres查询不会执行

时间:2017-04-02 07:45:15

标签: ruby-on-rails postgresql database-view scenic

我正在应用scenic gem来为Postgres数据库创建数据库视图。我有一个订单(ops_orders)表,其中有73k记录和订单行项目(ops_order_line_items)属于订单,有170k记录。

问题是查询执行的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语句性能不佳的文档。期待有用的解释。

0 个答案:

没有答案