create table records(
id varchar,
updated_at bigint
);
create index index1 on records (updated_at, id);
查询。它迭代最近更新的记录。获取10条记录,记得最后一条,然后取下10条记录等等。
select * from objects
where updated_at > '1' or (updated_at = '1' and id > 'some-id')
order by updated_at, id
limit 10;
它采用了指数,但它并不明智地使用它,也适用于过滤和处理万吨的记录,请参阅下面的查询说明Rows Removed by Filter: 31575
。
奇怪的是,如果您删除or
并保留左侧或右侧状态 - 对两者都适用。但似乎如果不能找出如何正确应用索引,如果这两个条件与or
同时使用。
Limit (cost=0.42..19.03 rows=20 width=1336) (actual time=542.475..542.501 rows=20 loops=1)
-> Index Scan using index1 on records (cost=0.42..426791.29 rows=458760 width=1336) (actual time=542.473..542.494 rows=20 loops=1)
Filter: ((updated_at > '1'::bigint) OR ((updated_at = '1'::bigint) AND ((id)::text > 'some-id'::text)))
Rows Removed by Filter: 31575
Planning time: 0.180 ms
Execution time: 542.532 ms
(6 rows)
Postgres的版本是9.6
'...其中的updated_at> '1' ...'你不应该引用整数常量。 – wildplasser
@wildplasser我试过没有引号,同样的事情。 –
'width = 1336'这是一个*非常宽的表, – wildplasser