假設我有一個
mytable (id, ..., insertiondate)
index lastupdate_idx(lastupdate desc)
我查詢 select * from mytable order by lastupdate desc limit 10 offset 100
執行運行時是否會跳過索引offset中的記錄數量,只從堆中獲取行數?或者它是否從堆中獲取行并且只將行回傳給查詢。limitoffset limitlimit
這是計劃:
Limit
-> Index Scan using lastupdate_idx on mytable)
我無法從中看出(我應該能夠嗎?),我無法找到檔案中提到的位置。
uj5u.com熱心網友回復:
它從堆中獲取它們。在將 abalance 設定為隨機值并對其進行索引后,我重新啟動 PostgreSQL 以清除 shared_buffers,并執行此操作(使用 2,000,000 個元組):
explain (analyze , buffers ) select * from pgbench_accounts order by abalance desc limit 10 offset 100;
得到:
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=9.90..10.85 rows=10 width=97) (actual time=15.247..15.384 rows=10 loops=1)
Buffers: shared hit=1 read=114 dirtied=1
I/O Timings: read=15.088
-> Index Scan Backward using pgbench_accounts_abalance_idx on pgbench_accounts (cost=0.43..189448.93 rows=2000000 width=97) (actual time=0.113..15.363 rows=110 loops=1)
Buffers: shared hit=1 read=114 dirtied=1
I/O Timings: read=15.088
Planning:
Buffers: shared hit=44 read=4 dirtied=1
I/O Timings: read=0.173
Planning Time: 4.414 ms
Execution Time: 15.493 ms
它必須讀取的 114 個緩沖區只能解釋為 110 個堆元組,加上幾頁用于索引。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/415494.html
標籤:
下一篇:優化資料庫查詢
