相关疑难解决方法(0)

在大表上使用OFFSET优化查询

我有桌子

create table big_table (
id serial primary key,
-- other columns here
vote int
); 
Run Code Online (Sandbox Code Playgroud)

这个表非常大,大约有7000万行,我需要查询:

SELECT * FROM big_table
ORDER BY vote [ASC|DESC], id [ASC|DESC]
OFFSET x LIMIT n  -- I need this for pagination
Run Code Online (Sandbox Code Playgroud)

您可能知道,当x数字很​​大时,这样的查询非常慢.

为了性能优化,我添加了索引:

create index vote_order_asc on big_table (vote asc, id asc);
Run Code Online (Sandbox Code Playgroud)

create index vote_order_desc on big_table (vote desc, id desc);
Run Code Online (Sandbox Code Playgroud)

EXPLAIN显示上面的SELECT查询使用这些索引,但无论如何都有很大的偏移量.

如何OFFSET在大表中优化查询?也许PostgreSQL 9.5甚至更新版本都有一些功能?我搜索过但没找到任何东西.

sql postgresql pagination sql-order-by postgresql-9.5

9
推荐指数
1
解决办法
9042
查看次数