从特定日期返回尚未完成的所有请求PostgreSQL

如何解决从特定日期返回尚未完成的所有请求PostgreSQL

我需要进行查询,以返回从录音开始到特定日期的所有未完成或取消的请求。我现在的操作方式需要花费大量时间并返回错误:“用户查询可能需要查看必须删除的行版本”(我猜这是由于缺少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(我不能只检索未结清的东西,它只适用于今天,而不是特定的过去日期-我想要的东西),介于记录开始和开始之间。可以说5月底。然后使用WHERE T1.ID NOT IN(在同一时间段中状态为“关闭”的T2.ID)。但是,正如我所说的,这将永远花费并返回错误。

我使用相同的代码来获取特定月份(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

谢谢

解决方法

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'))

但是我希望这只会给您带来一点点改善。您能否看到此更改有什么帮助?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res