explain (analyze, buffers ) select * from door_event_info d
where
d.door_id in (select a.door_id from door_device_info a) and
d.event_time > '2017-11-12 12:12:12' and d.event_time < '2019-12-12 12:12:12'
order by d.event_time offset 1295980 limit 20;
1. 不能使用位點來優化,因為帶了條件之后位點不連續。
2. door_id 的值大概有兩千個,均勻分布在這五百萬資料中
當前條件下有五百萬的資料,執行計劃如下:
Limit (cost=246144.59..246144.60 rows=1 width=6) (actual time=15787.731..15787.744 rows=20 loops=1)
Buffers: shared hit=26330 read=111413, temp read=3264 written=11441
-> Sort (cost=245854.63..246144.59 rows=115984 width=6) (actual time=14965.301..15733.105 rows=1296000 loops=1)
Sort Key: d.id
Sort Method: external merge Disk: 91376kB
Buffers: shared hit=26330 read=111413, temp read=3264 written=11441
-> Hash Semi Join (cost=43.97..236098.31 rows=115984 width=6) (actual time=0.987..5382.321 rows=5502007 loops=1)
Hash Cond: ((d.door_id)::text = (a.door_id)::text)
Buffers: shared hit=26330 read=111413
-> Seq Scan on door_event_info d (cost=0.00..220320.00 rows=5502484 width=28) (actual time=0.015..3632.381 rows=5502007 loops=1)
Filter: ((event_time > '2017-11-12 12:12:12'::timestamp without time zone) AND (event_time < '2019-12-12 12:12:12'::timestamp without time zone))
Rows Removed by Filter: 5993
Buffers: shared hit=26287 read=111413
-> Hash (cost=43.43..43.43 rows=43 width=516) (actual time=0.957..0.957 rows=2040 loops=1)
Buckets: 2048 (originally 1024) Batches: 1 (originally 1) Memory Usage: 126kB
Buffers: shared hit=43
-> Seq Scan on door_device_info a (cost=0.00..43.43 rows=43 width=516) (actual time=0.009..0.458 rows=2040 loops=1)
Buffers: shared hit=43
Planning time: 0.709 ms
Execution time: 15808.060 ms
uj5u.com熱心網友回復:
不知道啥為位點?d.event_time欄位上有索引嗎?沒有建一個看看,能否走到d表的索引,然后使用嵌套回圈完成整個SQL,不過無論怎么優化,分頁一般都是越往后翻越慢的
uj5u.com熱心網友回復:
要么自建位點,要么使用游標吧,其它應該也沒啥辦法優化了uj5u.com熱心網友回復:
很簡單的道理看似之需要20行,但是你總得過濾到前面的1295980 才能找到你要的20行。隨著offset越來越多當然會越來越慢。實際上這是設計思想的問題把翻頁作在資料庫端,除了每次開銷都很大,還有結果集不穩定的隱患。如果兩次查詢間,資料更新,其實沒有得到任何正確的結果集。
可以考慮一次性把資料輸出到最前端的sqlite。
如果一定要在資料庫端做,可以考慮建臨時表。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/16714.html
標籤:PostgreSQL
上一篇:達夢資料庫資料脫敏如何實作
