postgresql 如何解决后十页查询慢的问题

ux6nzvsh  于 2023-03-17  发布在  PostgreSQL
关注(0)|答案(1)|浏览(188)

我在本地postgres数据库的nfts表中创建了1000万条数据,对数据进行分页,根据id对数据进行倒排,每页10条数据,查询前十页和后十页数据,每页查询十次,这里是平均所需时间。
以下是查询前十页数据所需的时间,单位为毫秒

20.5
12.9
15.6
13.6
13.9
12.9
12.5
13.1
12.2
13.7

以下为查询最后十页数据所需的时间,单位为秒

2.377
2.385
2.389
2.399
2.388
2.385
2.394
2.374
2.399
2.377

我的分页使用gem“kaminari”,用法如下NFT.order(:id).page(params[:page]).per(10)显示索引页,我给id添加了index,后端的查询语句是:

Started GET "/nfts?page=999992" for ::1 at 2023-03-16 09:10:03 +0800
  ActiveRecord::SchemaMigration Pluck (1.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by NftsController#index as JSON
  Parameters: {"page"=>"999992", "nft"=>{}}
  NFT Count (785.6ms)  SELECT COUNT(*) FROM (SELECT 1 AS one FROM "nfts" ORDER BY id desc LIMIT $1 OFFSET $2) subquery_for_count  [["LIMIT", 10], ["OFFSET", 9999910]]
  ↳ app/controllers/nfts_controller.rb:10:in `index'
  NFT Load (1670.2ms)  SELECT "nfts".* FROM "nfts" ORDER BY id desc LIMIT $1 OFFSET $2  [["LIMIT", 10], ["OFFSET", 9999910]]
  ↳ app/controllers/nfts_controller.rb:12:in `index'
Completed 200 OK in 2480ms (Views: 9.2ms | ActiveRecord: 2466.1ms | Allocations: 14400)

怎样解决最后十页查询慢的问题。
附上前十页和后十页每页所需的时间以下是前十页每页数据查询所需的时间,单位为毫秒

1:[77ms, 13ms, 10ms, 11ms, 17ms, 11ms, 17ms, 16ms, 12ms, 21ms]
2.[16ms, 11ms, 12ms, 10ms, 11ms, 11ms, 10ms, 18ms, 11ms, 19ms]
3.[23ms, 14ms, 16ms, 13ms, 23ms, 18ms, 12ms, 15ms, 12ms, 10ms]
4.[15ms, 16ms, 16ms, 11ms, 11ms, 12ms, 10ms, 10ms, 15ms, 20ms]
5.[22ms, 11ms, 11ms, 11ms, 12ms, 17ms, 12ms, 17ms, 16ms, 10ms]
6.[13ms, 19ms, 19ms, 10ms, 16ms, 11ms, 10ms, 10ms, 10ms, 11ms]
7.[11ms, 11ms, 20ms, 10ms, 16ms, 12ms, 9ms, 14ms, 11ms, 11ms]
8.[11ms, 16ms, 16ms, 11ms, 11ms, 9ms, 12ms, 12ms, 22ms, 11ms]
9.[12ms, 10ms, 15ms, 9ms, 14ms ,10ms, 12ms, 15ms, 11ms, 14ms]
10.[10ms, 23ms, 10ms, 11ms, 17ms, 11ms, 15ms, 10ms, 21ms, 9ms]

以下为查询最后10页中每页数据所需的时间,单位为秒

-1.[2.37s, 2.42s, 2.41s, 2.39s, 2.35s, 2.37s, 2.31s, 2.34s, 2.37s, 2.44s]
-2.[2.36s, 2.40s, 2.43s, 2.39s, 2.40s, 2.47s, 2.36s, 2.36s, 2.33s, 2.35s]
-3.[2.34s, 2.37s, 2.40s, 2.41s, 2.41s, 2.37s, 2.52s, 2.37s, 2.35s, 2.35s]
-4.[2.35s, 2.38s, 2.43s, 2.42s, 2.38s, 2.38s, 2.46s, 2.38s, 2.39s, 2.42s]
-5.[2.37s, 2.37s, 2.37s, 2.46s, 2.38s, 2.40s, 2.35s, 2.40s, 2.40s, 2.38s]
-6.[2.43s, 2.35s, 2.40s, 2.41s, 2.36s, 2.38s, 2.39s, 2.33s, 2.41s, 2.39s]
-7.[2.38s, 2.36s, 2.39s, 2.37s, 2.42s, 2.40s, 2.50s, 2.37s, 2.37s, 2.38s]
-8.[2.39s, 2.38s, 2.37s, 2.39s, 2.34s, 2.37s, 2.37s, 2.35s, 2.40s, 2.38s]
-9.[2.38s, 2.41s, 2.38s, 2.38s, 2.38s, 2.39s, 2.43s, 2.41s, 2.41s, 2.42s]
-10.[2.37s, 2.40s, 2.33s, 2.37s, 2.38s, 2.37s, 2.40s, 2.38s, 2.39s, 2.38s]
yyyllmsg

yyyllmsg1#

可以使用以下查询语句进行查询

select * from foo where ID > [huge] order by ID limit 100

相关问题