【发布时间】:2017-05-25 17:30:34
【问题描述】:
我有一个返回 SETOF records 的 PostgreSQL 函数 find_all_records_with_params()。记录表有以下列:
table records(
id integer,
type integer,
created date,
content text
);
我想选择 current_date - created 大于 90 天的所有记录。但是,如果此查询不存在,我想选择 1 条最早 created 日期的记录。
我尝试使用递归 cte(如下所示)来执行此操作,但它不允许 if/else 逻辑。我试图避免调用find_all_records_with_params() 函数两次,如果可能的话,我不希望创建临时表。
with most_records as (select * from find_all_records_with_params()),
filtered_records as (select * from most_records where current_date - created > 90)
if exists(select * from filtered_records) then
select * from filtered_records;
else
select * from most_records order by created asc limit 1;
end if;
【问题讨论】:
-
current_date - created > 90-- 这很丑。为什么不created < current_date - 90? -
@DavidAldridge 执行
current_date - 90是否比current_date - created有性能优势?它会缓存该值吗? -
它允许优化器使用索引,但更容易阅读——我写 SQL 已经 25 年了,但你把
current_date - created > 90放在我面前,我必须好好想想这意味着什么。
标签: sql postgresql common-table-expression