postgresql数据库怎样对查询出的结果按日期时间排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgresql数据库怎样对查询出的结果按日期时间排序相关的知识,希望对你有一定的参考价值。
select * from (select 除了时间所有字段写出来,to_date('COMMENTTIME','yyyy-mm-dd hh24:mi:ss') time from T_SCHOOL_RECORDCOMMENT where RECORDID = :p_cid) order by time desc;这样试试,但是你的时间字段用该保存为date类型,这样好像很麻烦。。 参考技术A select * from 表 order by 日期字段
按日期范围分区 PostgreSQL 扫描所有分区
【中文标题】按日期范围分区 PostgreSQL 扫描所有分区【英文标题】:Partition by date range PostgreSQL scans all partitions 【发布时间】:2019-04-11 14:10:54 【问题描述】:我有一个每月分区的表(时间戳列)。
查询数据时,explain 显示当我使用日期函数构造日期时正在查询所有分区,而当我使用硬编码日期时,仅扫描目标分区。
所以当这样查询时:
SELECT * FROM vw_comments
WHERE created >= '2019-4-1'
AND created <= '2019-4-30'
limit 100;
它只扫描 1 个分区(1 个月,很好!) 但是为了让它更有活力,我传递了这样的东西(简化)
SELECT * FROM vw_comments
WHERE created >= (date_trunc('month', now()))::timestamp
AND created <= (date_trunc('month', now() + interval '1 month') - interval '1 day') ::timestamp
limit 100;
上述日期方法的日期与第一个查询完全相同,但 EXPLAIN 显示所有分区都已扫描。
如何让它发挥作用?
编辑:添加表定义并解释
应@a_horse_with_no_name 的要求,我添加了实际表格并进行了解释。这样做后,我想出了更多的东西:动态日期在加入时不起作用。因此,在下面的查询中省略“用户”表可以使动态日期起作用。
CREATE TABLE public.comments
(
comment_id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
comment_id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
from_user_id integer NOT NULL,
fk_topic_id integer NOT NULL,
comment_text text COLLATE pg_catalog."default",
parent_comment_id integer,
created timestamp without time zone NOT NULL,
comment_type integer NOT NULL DEFAULT 0,
CONSTRAINT comments_pkey PRIMARY KEY (comment_id, created)
) PARTITION BY RANGE (created)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.comments
OWNER to soichat;
CREATE INDEX ix_comments_comment_id
ON public.comments USING btree
(comment_id DESC)
TABLESPACE pg_default;
CREATE INDEX ix_comments_created
ON public.comments USING btree
(created DESC)
TABLESPACE pg_default;
CREATE INDEX ix_comments_fk_topic_id
ON public.comments USING btree
(fk_topic_id)
TABLESPACE pg_default;
CREATE INDEX ix_comments_from_user_id
ON public.comments USING btree
(from_user_id)
TABLESPACE pg_default;
CREATE INDEX ix_comments_parent_comment_id
ON public.comments USING btree
(parent_comment_id)
TABLESPACE pg_default;
-- Partitions SQL
CREATE TABLE public.comments_2019_2 PARTITION OF public.ix_comments_parent_comment_id
FOR VALUES FROM ('2019-02-01 00:00:00') TO ('2019-03-01 00:00:00');
CREATE TABLE public.comments_2019_3 PARTITION OF public.ix_comments_parent_comment_id
FOR VALUES FROM ('2019-03-01 00:00:00') TO ('2019-04-01 00:00:00');
CREATE TABLE public.comments_2019_4 PARTITION OF public.ix_comments_parent_comment_id
FOR VALUES FROM ('2019-04-01 00:00:00') TO ('2019-05-01 00:00:00');
CREATE TABLE public.comments_2019_5 PARTITION OF public.ix_comments_parent_comment_id
FOR VALUES FROM ('2019-05-01 00:00:00') TO ('2019-06-01 00:00:00');
查询:
explain (analyse, buffers)
SELECT comments.comment_id,
comments.from_user_id,
comments.fk_topic_id,
comments.comment_text,
comments.parent_comment_id,
comments.created,
users.user_name,
users.picture_path
FROM comments
LEFT JOIN users ON comments.from_user_id = users.user_id
WHERE comments.created >= (date_trunc('month', now()))::timestamp
AND comments.created <= (date_trunc('month', now() + interval '1 month') - interval '1 day') ::timestamp
limit 100;
explain (analyze, buffers)
Limit (cost=1.20..11.93 rows=100 width=126) (actual time=1.441..1.865 rows=100 loops=1)
Buffers: shared hit=499
-> Merge Left Join (cost=1.20..753901.07 rows=7028011 width=126) (actual time=1.440..1.778 rows=100 loops=1)
Merge Cond: (comments_2019_2.from_user_id = users.user_id)
Buffers: shared hit=499
-> Merge Append (cost=0.92..665812.08 rows=7028011 width=51) (actual time=0.017..0.259 rows=100 loops=1)
Sort Key: comments_2019_2.from_user_id
Buffers: shared hit=15
-> Index Scan using comments_2019_2_from_user_id_idx on comments_2019_2 (cost=0.15..58.95 rows=5 width=56) (actual time=0.002..0.003 rows=0 loops=1)
Filter: ((created >= (date_trunc('month'::text, now()))::timestamp without time zone) AND (created <= ((date_trunc('month'::text, (now() + '1 mon'::interval)) - '1 day'::interval))::timestamp without time zone))
Buffers: shared hit=1
-> Index Scan using comments_2019_3_from_user_id_idx on comments_2019_3 (cost=0.15..9790.24 rows=1 width=51) (actual time=0.002..0.003 rows=0 loops=1)
Filter: ((created >= (date_trunc('month'::text, now()))::timestamp without time zone) AND (created <= ((date_trunc('month'::text, (now() + '1 mon'::interval)) - '1 day'::interval))::timestamp without time zone))
Buffers: shared hit=1
-> Index Scan using comments_2019_4_from_user_id_idx on comments_2019_4 (cost=0.43..550483.74 rows=7028000 width=51) (actual time=0.010..0.162 rows=100 loops=1)
Filter: ((created >= (date_trunc('month'::text, now()))::timestamp without time zone) AND (created <= ((date_trunc('month'::text, (now() + '1 mon'::interval)) - '1 day'::interval))::timestamp without time zone))
Buffers: shared hit=12
-> Index Scan using comments_2019_5_from_user_id_idx on comments_2019_5 (cost=0.15..58.95 rows=5 width=56) (actual time=0.001..0.002 rows=0 loops=1)
Filter: ((created >= (date_trunc('month'::text, now()))::timestamp without time zone) AND (created <= ((date_trunc('month'::text, (now() + '1 mon'::interval)) - '1 day'::interval))::timestamp without time zone))
Buffers: shared hit=1
-> Index Scan using pk_users on users (cost=0.28..234.83 rows=1606 width=79) (actual time=0.005..0.870 rows=1395 loops=1)
Buffers: shared hit=484
Planning Time: 0.360 ms
Execution Time: 1.942 ms
【问题讨论】:
@a_horse_with_no_name 我添加了实际查询和解释。这样做后,我想出了更多的东西:动态日期在加入时不起作用。因此,在下面的查询中省略“用户”表可以使动态日期起作用。 @a_horse_with_no_name:嗯,我使用 dbeaver 来执行创建脚本。我使用来自 pgadmin 的正确创建脚本更新了问题。对此感到抱歉。 看起来这种情况对于PostgreSQL来说太复杂了,无法进行分区修剪;我不知道确切的原因。但是其他分区上的索引扫描并没有真正受到伤害,不是吗? @LaurenzAlbe 看起来是这样,但是您看到的是测试数据库的结果(其他月份几乎没有记录),通常每月有数百万个 cmets,我们正在删除它们一年后,所以会有点疼。分区目前尚未在生产中实现。 【参考方案1】:找到(一个很好的)答案here
因为规划器不知道 now() 将在运行时产生什么时间,所以它选择安全选项并扫描所有分区。因为我不想为每个分区配置新函数,所以我使用了一个创建日期的不可变函数:
CREATE FUNCTION now_immutable()
RETURNS timestamp AS
$func$
SELECT now() AT TIME ZONE current_setting('TimeZone')
$func$ LANGUAGE sql IMMUTABLE;
所以现在我不使用 now(),而是将此函数用于事务中日期不变的函数:
explain (analyse, buffers)
SELECT comments.comment_id,
comments.from_user_id,
comments.fk_topic_id,
comments.comment_text,
comments.parent_comment_id,
comments.created,
users.user_name,
users.picture_path
FROM comments
LEFT JOIN users ON comments.from_user_id = users.user_id
WHERE comments.created >= (date_trunc('month', now_immutable()))
AND comments.created <= (date_trunc('month', now_immutable() + interval '1 month') - interval '1 day')
limit 100;
我还创建了另一个方便的函数来从代码months_back调用:
CREATE OR REPLACE FUNCTION public.months_back(months_back integer)
RETURNS timestamp without time zone
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT cast((date_trunc('month', now()) - (months_back || ' month')::interval)::timestamp AT TIME ZONE current_setting('TimeZone') as timestamp)
$function$;
这在每月分区时很方便,因为如果您知道第一个评论是 3 个月前,您可以调用months_back(3),并且 postgres 将仅搜索 3 个分区,传递 0 将为您提供当前月份的开始。
希望这对某人有所帮助。
【讨论】:
以上是关于postgresql数据库怎样对查询出的结果按日期时间排序的主要内容,如果未能解决你的问题,请参考以下文章
关于Postgresql数据库查询结果不足位数前面补位的操作