【问题标题】:Return all requests from a specific date that are not finished postgresql从特定日期返回所有未完成 postgresql 的请求
【发布时间】:2020-08-18 14:05:15
【问题描述】:

我需要进行查询,它返回从录制开始到特定日期未完成或取消的所有请求。我现在正在做的方式,花费太多时间并返回错误:'用户查询可能需要查看必须删除的行版本'(我猜这是由于缺乏 RAM)。

以下是我正在使用的查询,以下是一些信息:

  • 保存每个新条目的 T1,其中包含多个表的 ID、创建日期、状态(打开、关闭)和其他键。

  • T2 保存每个请求中所做的每个更改(进行中、等待、拒绝和关闭)、更改日期和其他表的其他键。

    SELECT T1.id_request,
           T1.dt_created,
           T1.status
    FROM T1
    LEFT JOIN T2
        ON T1.id_request = T2.id_request
    WHERE (T1.dt_created >= '2012-01-01 00:00:00' AND T1.dt_created <= '2020-05-31 23:59:59')
        AND T1.id_request NOT IN (SELECT T2.di_request
                                  FROM T2
                                  WHERE ((T2.dt_change >= '2012-01-01 00:00:00' 
                                         AND T2.dt_change <= '2020-05-31 23:59:59')
                                         OR T2.dt_change IS NULL)
                                         AND T2.status IN ('Closed','Canceled','rejected'))
    

我的想法是获取所有收到的内容 - T1(我不能只检索打开的内容,它只适用于今天,而不是特定的过去日期 - 我想要的)在记录的开头和让我们说五月底。然后使用 WHERE T1.ID NOT IN (T2.ID with STATUS 'close', in the same period)。但正如我所说,它需要永远并返回一个错误。

我使用相同的代码来获取特定月份(1 日至 30 日)的开放内容,并且运行良好。

也许这种方法不是最好的方法,但我想不出任何其他方法(我不是 SQL 专家)。如果没有足够的信息来提供答案,请随意提问。

根据@MikeOrganek 的要求,这是分析器:

  Nested Loop Left Join  (cost=27985.55..949402.48 rows=227455 width=20) (actual time=2486.433..54832.280 rows=47726 loops=1)
   Buffers: shared hit=293242 read=260670
    Seq Scan on T1 (cost=27984.99..324236.82 rows=73753 width=20) (actual time=2467.499..6202.970 rows=16992 loops=1)
  Filter: ((dt_created >= '2020-05-01 00:00:00-03'::timestamp with time zone) AND (dt_created <= '2020-05-31 23:59:59-03'::timestamp with time zone) AND (NOT (hashed SubPlan 1)))
   Rows Removed by Filter: 6085779
    Buffers: shared hit=188489 read=250098
  SubPlan 1
   Nested Loop  (cost=7845.36..27983.13 rows=745 width=4) (actual time=129.379..1856.518 rows=168690 loops=1)
    Buffers: shared hit=60760
     Seq Scan on T3(cost=0.00..5.21 rows=3 width=8) (actual time=0.057..0.104 rows=3 loops=1)
     Filter: ((status_request)::text = ANY ('{Closed,Canceled,rejected}'::text[]))
     Rows Removed by Filter: 125
     Buffers: shared hit=7
     Bitmap Heap Scan on T2(cost=7845.36..9321.70 rows=427 width=8) (actual time=477.324..607.171 rows=56230 loops=3)
     Recheck Cond: ((dt_change >= '2020-05-01 00:00:00-03'::timestamp with time zone) AND (dt_change <= '2020-05-31 23:59:59-03'::timestamp with time zone) AND (T2.ID_status= T3.ID_status))
     Rows Removed by Index Recheck: 87203
     Heap Blocks: exact=36359
     Buffers: shared hit=60753
      BitmapAnd  (cost=7845.36..7845.36 rows=427 width=0) (actual time=473.864..473.864 rows=0 loops=3)
      Buffers: shared hit=24394
      Bitmap Index Scan on idx_ix_T2_dt_change (cost=0.00..941.81 rows=30775 width=0) (actual time=47.380..47.380 rows=306903 loops=3)
      Index Cond: ((dt_change >= '2020-05-01 00:00:00-03'::timestamp with time zone) AND (dt_change<= '2020-05-31 23:59:59-03'::timestamp with time zone))
      Buffers: shared hit=2523
      Bitmap Index Scan on idx_T2_ID_status  (cost=0.00..6895.49 rows=262724 width=0) (actual time=418.942..418.942 rows=2105165 loops=3)
      Index Cond: (ID_status = T3.ID_status )
      Buffers: shared hit=21871
    Index Only Scan using idx_ix_T2_id_request  on T2  (cost=0.56..8.30 rows=18 width=4) (actual time=0.369..2.859 rows=3 loops=16992)
    Index Cond: (id_request = t17.id_request )
    Heap Fetches: 44807
    Buffers: shared hit=104753 read=10572
    Planning time: 23.424 ms
    Execution time: 54841.261 ms

这是与dt_change IS NULL的主要区别:

  Planning time: 34.320 ms
  Execution time: 230683.865 ms

谢谢

【问题讨论】:

  • 请参阅这里以了解您遇到的错误的解释:stackoverflow.com/a/39450919/13808319 至于您的问题,您的T2 历史记录是否已更新(除了添加新的更改记录)?也就是说,历史是固定的吗?
  • @MikeOrganek 一旦记录有更改,该行就被修复了。每个新更改都是 T2 中带有新日期的新行。这是我的主要问题。要仅查找在该特定日期仍处于打开状态的内容,我需要获取该日期之前尚未关闭的请求的最后一次更改。
  • 如果是这样,则错误可能是由于查询运行时T1 发生的更改。从性能的角度来看,在子查询中看到 OR T2.dt_change is NULL 很麻烦。这是绝对必要的吗?您能否为您在一个月内运行的查询发布explain (analyze, buffers)
  • @MikeOrganek 关于OR IS NULL 我用于另一个问题,只是让它在那里。它可以被移除。 explain (analyze, buffers) 提取将作为答案发布。一个额外的问题:这个分析器可以与 psycopg2 一起使用吗?通常我用它来提取信息和熊猫的过程。
  • Seq Scan on T1 (cost=27984.99..324236.82 rows=73753 width=20) (实际时间=2467.499..6202.970 rows=16992 loops=1) Buffers: shared hit=104753 read=10572计划时间:23.424 毫秒执行时间:54841.261 毫秒 @MikeOrganek 对分析器的信息是否足够?

标签: postgresql subquery


【解决方案1】:

看起来OR T2.dt_change is NULL 的成本非常高,因为它将总执行时间增加了五倍。

我能看到的唯一选择是将not in 更改为not exists,如下所示。

SELECT T1.id_request,
       T1.dt_created,
       T1.status
  FROM T1
       LEFT JOIN T2
              ON T1.id_request = T2.id_request
 WHERE T1.dt_created >= '2012-01-01 00:00:00' 
   AND T1.dt_created <= '2020-05-31 23:59:59'
   AND NOT EXISTS (SELECT 1
                     FROM T2
                    WHERE id_request = T1.id_request
                      AND (   (    dt_change >= '2012-01-01 00:00:00' 
                               AND dt_change <= '2020-05-31 23:59:59')
                           OR dt_change IS NULL)
                      AND status IN ('Closed','Canceled','rejected'))

但我希望这只会给您带来一点点改进。你能看看这个改变有多大帮助吗?

【讨论】:

  • 这没有返回任何内容。为什么使用 SELECT 1(我尝试了它和 id_request)?即使是每月查询。 @MikeOrganek
  • @GustavoRottgering select 1 只是我很久以前接受的一个约定。这很奇怪,它没有返回任何行。您确定子查询包含id_request = T1.id_request 吗?如果只运行子查询会返回什么?
  • 抱歉拖了这么久(工作时的VPN真的很慢)。仅运行子查询它会返回一列。虽然无法准确检查有多少。任何想法为什么它没有在整个查询中返回任何内容?
  • @GustavoRottgering 如果子查询为每个T1.id_request 返回一行,它不会返回任何内容。这就是为什么我问你是否在子查询where 子句中有相关性。仅运行子查询需要多长时间?
  • 每月和整个期间的请求大约需要 8m。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
相关资源
最近更新 更多