postgres NOW函数花费的时间过长vs等效于字符串

如何解决postgres NOW函数花费的时间过长vs等效于字符串

这是我关于StackOverflow的第一个问题,因此请原谅,如果该问题的结构可能不正确。

我有一个带有t日期时间列d_datetime的表t_table,我需要过滤过去5天之间的数据。以下两项在本地我的数据较少的地方工作:

查询1。

SELECT * FROM t_table 
WHERE d_datetime 
BETWEEN '2020-08-28T00:00:00.024Z' AND '2020-09-02T00:00:00.024Z';

查询2。

SELECT * FROM t_table 
WHERE d_datetime 
BETWEEN (NOW() - INTERVAL '5 days') AND NOW();

查询3。

SELECT * FROM t_table 
WHERE d_datetime > NOW() - INTERVAL '5 days'; 

但是,当我移至实时数据库时,只有第一个查询在大约10秒钟内运行完毕。我不知道为什么,但是其他两个只是消耗了太多的处理能力,即使最后等待了5分钟,我也从未见过它们能完成。

我尝试使用以下方法自动生成用于第一个查询中显示的d_datetime的字符串:

查询4。

SELECT * FROM t_table 
WHERE d_datetime 
BETWEEN 
(TO_CHAR(NOW() - INTERVAL '5 days','YYYY-MM-ddThh:MI:SS.024Z'))
 AND
(TO_CHAR(NOW(),'YYYY-MM-ddThh:MI:SS.024Z'))

但是会引发以下错误:

operator does not exist: timestamp without time zone >= text

我的问题是:

  1. 查询1如此之快而其余的要花费大量时间才能在大型数据集上运行是否有任何特殊原因?
  2. 当查询4实际上生成与查询1相同的字符串格式时,为什么查询4失败('YYYY-MM-ddThh:mm:ss.024Z')?

以下是对第一个查询的解释结果的结果

EXPLAIN SELECT * FROM t_table 
WHERE d_datetime 
BETWEEN '2020-08-28T00:00:00.024Z' AND '2020-09-02T00:00:00.024Z';
                                                                                                                                              QUERY PLAN                                                                                                                                               
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize HashAggregate  (cost=31346.37..31788.13 rows=35341 width=22) (actual time=388.622..388.845 rows=6 loops=1)
   Output: count(_hyper_12_67688_chunk.octets),_hyper_12_67688_chunk.application,(date_trunc('day'::text,_hyper_12_67688_chunk.entry_time))
   Group Key: (date_trunc('day'::text,_hyper_12_67688_chunk.entry_time)),_hyper_12_67688_chunk.application
   Buffers: shared hit=17193
   ->  Gather  (cost=27105.45..31081.31 rows=35341 width=22) (actual time=377.109..398.285 rows=11 loops=1)
         Output: _hyper_12_67688_chunk.application,(PARTIAL count(_hyper_12_67688_chunk.octets))
         Workers Planned: 1
         Workers Launched: 1
         Buffers: shared hit=17193
         ->  Partial HashAggregate  (cost=26105.45..26547.21 rows=35341 width=22) (actual time=174.272..174.535 rows=6 loops=2)
               Output: _hyper_12_67688_chunk.application,PARTIAL count(_hyper_12_67688_chunk.octets)
               Group Key: date_trunc('day'::text,_hyper_12_67688_chunk.entry_time),_hyper_12_67688_chunk.application
               Buffers: shared hit=17193
               Worker 0: actual time=27.942..28.206 rows=5 loops=1
                 Buffers: shared hit=579
               ->  Result  (cost=1.73..25272.75 rows=111027 width=18) (actual time=0.805..141.094 rows=94662 loops=2)
                     Output: _hyper_12_67688_chunk.application,date_trunc('day'::text,_hyper_12_67688_chunk.octets
                     Buffers: shared hit=17193
                     Worker 0: actual time=1.576..23.928 rows=6667 loops=1
                       Buffers: shared hit=579
                     ->  Parallel Append  (cost=1.73..23884.91 rows=111027 width=18) (actual time=0.800..114.488 rows=94662 loops=2)
                           Buffers: shared hit=17193
                           Worker 0: actual time=1.572..20.204 rows=6667 loops=1
                             Buffers: shared hit=579
                           ->  Parallel Bitmap Heap Scan on _timescaledb_internal._hyper_12_67688_chunk  (cost=1.73..11.23 rows=8 width=17) (actual time=1.570..1.618 rows=16 loops=1)
                                 Output: _hyper_12_67688_chunk.octets,_hyper_12_67688_chunk.entry_time
                                 Recheck Cond: ((_hyper_12_67688_chunk.entry_time >= '2020-08-28 05:45:03.024'::timestamp without time zone) AND (_hyper_12_67688_chunk.entry_time <= '2020-09-02 11:45:03.024'::timestamp without time zone))
                                 Filter: ((_hyper_12_67688_chunk.application)::text = 'dns'::text)
                                 Rows Removed by Filter: 32
                                 Buffers: shared hit=11
                                 Worker 0: actual time=1.570..1.618 rows=16 loops=1
                                   Buffers: shared hit=11
                                 ->  Bitmap Index Scan on _hyper_12_67688_chunk_dpi_applications_entry_time_idx  (cost=0.00..1.73 rows=48 width=0) (actual time=1.538..1.538 rows=48 loops=1)
                                       Index Cond: ((_hyper_12_67688_chunk.entry_time >= '2020-08-28 05:45:03.024'::timestamp without time zone) AND (_hyper_12_67688_chunk.entry_time <= '2020-09-02 11:45:03.024'::timestamp without time zone))
                                       Buffers: shared hit=2
                                       Worker 0: actual time=1.538..1.538 rows=48 loops=1
                                         Buffers: shared hit=2
                           ->  Parallel Index Scan Backward using _hyper_12_64752_chunk_dpi_applications_entry_time_idx on _timescaledb_internal._hyper_12_64752_chunk  (cost=0.14..2.36 rows=1 width=44) (actual time=0.040..0.076 rows=52 loops=1)
                                 Output: _hyper_12_64752_chunk.octets,_hyper_12_64752_chunk.application,_hyper_12_64752_chunk.entry_time
                                 Index Cond: ((_hyper_12_64752_chunk.entry_time >= '2020-08-28 05:45:03.024'::timestamp without time zone) AND (_hyper_12_64752_chunk.entry_time <= '2020-09-02 11:45:03.024'::timestamp without time zone))
                                 Filter: ((_hyper_12_64752_chunk.application)::text = 'dns'::text)
                                 Rows Removed by Filter: 52
                                 Buffers: shared hit=

-- cut logs
                           ->  Parallel Seq Scan on _timescaledb_internal._hyper_12_64814_chunk  (cost=0.00..2.56 rows=14 width=17) (actual time=0.017..0.038 rows=32 loops=1)
                                 Output: _hyper_12_64814_chunk.octets,_hyper_12_64814_chunk.application,_hyper_12_64814_chunk.entry_time
                                 Filter: ((_hyper_12_64814_chunk.entry_time >= '2020-08-28 05:45:03.024'::timestamp without time zone) AND (_hyper_12_64814_chunk.entry_time <= '2020-09-02 11:45:03.024'::timestamp without time zone) AND ((_hyper_12_64814_chunk.application)::text = 'dns'::text))
                                 Rows Removed by Filter: 40
                                 Buffers: shared hit=2
                           ->  Parallel Seq Scan on _timescaledb_internal._hyper_12_62262_chunk  (cost=0.00..2.54 rows=9 width=19) (actual time=0.027..0.039 rows=15 loops=1)
                                 Output: _hyper_12_62262_chunk.octets,_hyper_12_62262_chunk.application,_hyper_12_62262_chunk.entry_time
                                 Filter: ((_hyper_12_62262_chunk.entry_time >= '2020-08-28 05:45:03.024'::timestamp without time zone) AND (_hyper_12_62262_chunk.entry_time <= '2020-09-02 11:45:03.024'::timestamp without time zone) AND ((_hyper_12_62262_chunk.application)::text = 'dns'::text))
                                 Rows Removed by Filter: 37
                                 Buffers: shared hit=2
 Planning Time: 3367.445 ms
 Execution Time: 417.245 ms
(7059 rows)

Parallel Index Scan Backward using...日志将继续处理表中的所有超表块。

对于前面提到的其他三个查询不成功,查询时它们仍未完成,最终最终会填满内存。因此,抱歉,我无法发布这些查询的EXPLAIN结果。

如果我的问题结构不正确,请告诉我。谢谢。

解决方法

您使用的分区表可能有很多分区,因为查询的计划时间为3秒。

您可能正在使用PostgreSQL v11或更早版本。第12版在执行时引入了分区修剪,而第11版只能在查询计划时排除分区。

在您的第一个查询中,WHERE条件包含常量,因此可以正常工作。在其他查询中,使用函数now(),其结果值仅在查询执行时才知道(它是STABLE,而不是IMMUTABLE),因此无法在查询时进行分区修剪计划时间。查询计划和执行不必同时进行-考虑准备好的语句。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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