Skip to content

Add (gpg_key_id, id) index to gpg_signatures

Igor Drozdov requested to merge id-add-index-to-gpg-signatures into master

What does this MR do and why?

Currently, the query to fetch gpg signatures is timed out. Let's add an index to improve query + order time according to: !130350 (comment 1596320550)

Query:

SELECT "gpg_signatures"."id" FROM "gpg_signatures" WHERE "gpg_signatures"."gpg_key_id" = 133066 ORDER BY "gpg_signatures"."id" ASC LIMIT 1000

Before:

Limit  (cost=1000.59..41570.03 rows=1000 width=4)
  ->  Gather Merge  (cost=1000.59..3890595.43 rows=95875 width=4)
        Workers Planned: 2
        ->  Parallel Index Scan using gpg_signatures_pkey on gpg_signatures  (cost=0.56..3878529.05 rows=39948 width=4)
              Filter: (gpg_key_id = 133066)
Time: 10.730 min  
  - planning: 3.002 ms  
  - execution: 10.729 min  
    - I/O read: 29.581 min  
    - I/O write: 105.307 ms  
  
Shared buffers:  
  - hits: 31719940 (~242.00 GiB) from the buffer pool  
  - reads: 5139916 (~39.20 GiB) from the OS file cache, including disk I/O  
  - dirtied: 29293 (~228.90 MiB)  
  - writes: 660 (~5.20 MiB)  

After:

 Limit  (cost=0.56..89.37 rows=1000 width=4) (actual time=0.726..8.473 rows=1000 loops=1)
   Buffers: shared hit=41 read=73 dirtied=29
   I/O Timings: read=7.644 write=0.000
   ->  Index Only Scan using gpg_signatures_new_idx on public.gpg_signatures  (cost=0.56..8578.20 rows=96587 width=4) (actual time=0.724..8.385 rows=1000 loops=1)
         Index Cond: (gpg_signatures.gpg_key_id = 133066)
         Heap Fetches: 70
         Buffers: shared hit=41 read=73 dirtied=29
         I/O Timings: read=7.644 write=0.000
  
Time: 9.790 ms  
  - planning: 1.206 ms  
  - execution: 8.584 ms  
    - I/O read: 7.644 ms  
    - I/O write: 0.000 ms  
  
Shared buffers:  
  - hits: 41 (~328.00 KiB) from the buffer pool  
  - reads: 73 (~584.00 KiB) from the OS file cache, including disk I/O  
  - dirtied: 29 (~232.00 KiB)  
  - writes: 0  

After dropping redundant index:

 Limit  (cost=0.56..64.37 rows=1000 width=4) (actual time=0.576..5.323 rows=1000 loops=1)
   Buffers: shared hit=38 read=53 dirtied=24
   I/O Timings: read=4.110 write=0.000
   ->  Index Only Scan using index_gpg_signatures_on_gpg_key_id_and_id on public.gpg_signatures  (cost=0.56..6061.23 rows=94984 width=4) (actual time=0.574..5.158 rows=1000 loops=1)
         Index Cond: (gpg_signatures.gpg_key_id = 133066)
         Heap Fetches: 48
         Buffers: shared hit=38 read=53 dirtied=24
         I/O Timings: read=4.110 write=0.000
Time: 7.619 ms  
  - planning: 2.116 ms  
  - execution: 5.503 ms  
    - I/O read: 4.110 ms  
    - I/O write: 0.000 ms  
  
Shared buffers:  
  - hits: 38 (~304.00 KiB) from the buffer pool  
  - reads: 53 (~424.00 KiB) from the OS file cache, including disk I/O  
  - dirtied: 24 (~192.00 KiB)  
  - writes: 0  
Edited by Igor Drozdov

Merge request reports