我正在建立一个博客,但我遇到了问题。应该有一些粘性帖子。我想要的只是首先获得粘性帖子,然后是其余的。
一个有效的查询是
select * from
(
(select *,true as st from blog where "stickyUntil" > current_timestamp)
UNION all
(SELECT *,false as st from blog where "stickyUntil" < current_timestamp or "stickyUntil" is null )
) q
order by st desc, "stickyUntil" DESC ,publish DESC OFFEST x LIMIT z
workz 的另一个更简单的查询是
select * from blog order by case when "stickyUntil" > current_timestamp then "stickyUntil" end desc nulls last, publish desc;
但这将迫使 200.000 行在内存中排序不是很快..
有办法优化吗??
使用两个单独的查询会更好吗?谢谢