Postgresql 订单是否影响多个WHERE条件?

Postgresql 订单是否影响多个WHERE条件?,postgresql,Postgresql,我试图在名为posts的表上实现分页,并使用time_created和post_id列作为页面的分隔符 有可能在同一时间创建多个帖子,因此在检索下一页时,我需要在特定时间创建所有剩余的帖子,然后在该时间之前创建帖子 我担心的是,由于我有两个由OR分隔的条件,下面的查询可能只使用第二个条件来满足限制要求 我的问题是:ORDERBY子句是否保证我提供的第一个条件time_created=$1和post_id

我试图在名为posts的表上实现分页,并使用time_created和post_id列作为页面的分隔符

有可能在同一时间创建多个帖子,因此在检索下一页时,我需要在特定时间创建所有剩余的帖子,然后在该时间之前创建帖子

我担心的是,由于我有两个由OR分隔的条件,下面的查询可能只使用第二个条件来满足限制要求

我的问题是:ORDERBY子句是否保证我提供的第一个条件time_created=$1和post_id<$2将首先执行,因为time_created=$1在按降序排序时逻辑上应该在time_created<$1之前

SELECT * FROM posts 
WHERE (time_created = $1 AND post_id < $2) OR time_created < $1 
ORDER BY time_created DESC, post_id DESC
limit 50;

查询将分三步执行:

消除不满足WHERE子句中条件的行。 Order在Order by子句中按表达式重新生成行。 根据限制获取前50行。 因此,顺序不影响过滤

如果运行EXPLAIN Analysis,您将得到如下结果:

                                                                                        QUERY PLAN                                                                                         
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=430.71..430.84 rows=50 width=12) (actual time=1.727..1.730 rows=50 loops=1)
   ->  Sort  (cost=430.71..445.82 rows=6042 width=12) (actual time=1.726..1.727 rows=50 loops=1)
         Sort Key: time_created DESC, post_id DESC
         Sort Method: top-N heapsort  Memory: 27kB
         ->  Seq Scan on posts  (cost=0.00..230.00 rows=6042 width=12) (actual time=0.010..1.329 rows=6027 loops=1)
               Filter: (((time_created = '2018-07-01 00:00:00'::timestamp without time zone) AND (post_id < 1111)) OR (time_created < '2018-07-01 00:00:00'::timestamp without time zone))
               Rows Removed by Filter: 3973
 Planning time: 0.493 ms
 Execution time: 1.759 ms
(9 rows)
这可以解释为上述三个步骤