Skip to content

Instantly share code, notes, and snippets.

@amalloy
Last active January 16, 2019 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/133029e0f8dda25eba23e3c55c2308a6 to your computer and use it in GitHub Desktop.
Save amalloy/133029e0f8dda25eba23e3c55c2308a6 to your computer and use it in GitHub Desktop.
`!lg * recent / won` produces 2 queries, and divides one by the other. Here's the numerator:
explain analyze SELECT COUNT(*) AS fieldcount FROM logrecord
INNER JOIN l_ktyp ON logrecord.ktyp_id = l_ktyp.id
INNER JOIN l_cversion ON logrecord.cv_id = l_cversion.id
WHERE ((l_ktyp.ktyp = 'winning') AND (l_cversion.cvnum >= '2200099000000'::bigint));
Aggregate (cost=446001.87..446001.88 rows=1 width=8) (actual time=9661.391..96
61.391 rows=1 loops=1)
-> Hash Join (cost=4909.80..445813.68 rows=75274 width=0) (actual time=1912
.521..9658.063 rows=8193 loops=1)
Hash Cond: (logrecord.cv_id = l_cversion.id)
-> Nested Loop (cost=4908.11..445173.50 rows=220446 width=4) (actual
time=26.322..9616.571 rows=81599 loops=1)
-> Seq Scan on l_ktyp (cost=0.00..1.55 rows=1 width=4) (actual time=1.043..1.086 rows=1 loops=1)
Filter: (ktyp = 'winning'::citext)
Rows Removed by Filter: 43
-> Bitmap Heap Scan on logrecord (cost=4908.11..442550.43 rows=262152 width=8) (actual time=25.267..9572.177 rows=81599 loops=1)
Recheck Cond: (ktyp_id = l_ktyp.id)
Rows Removed by Index Recheck: 618597
Heap Blocks: exact=37291 lossy=34619
-> Bitmap Index Scan on ind_logrecord_ktyp_id (cost=0.00..4842.57 rows=262152 width=0) (actual time=18.830..18.830 rows=81599 loops=1)
Index Cond: (ktyp_id = l_ktyp.id)
-> Hash (cost=1.51..1.51 rows=14 width=4) (actual time=0.369..0.369 rows=2 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 9kB
-> Seq Scan on l_cversion (cost=0.00..1.51 rows=14 width=4) (actual time=0.361..0.365 rows=2 loops=1)
Filter: (cvnum >= '2200099000000'::numeric)
Rows Removed by Filter: 39
Planning time: 0.895 ms
Execution time: 9661.498 ms
(20 rows)
`!lg * recent / won` produces 2 queries, and divides one by the other. Here's the denominator:
explain analyze SELECT COUNT(*) FROM logrecord
INNER JOIN l_cversion ON logrecord.cv_id = l_cversion.id
WHERE (l_cversion.cvnum >= '2200099000000'::bigint);
Finalize Aggregate (cost=567080.75..567080.76 rows=1 width=8) (actual time=112
6.771..1126.771 rows=1 loops=1)
-> Gather (cost=567080.54..567080.75 rows=2 width=8) (actual time=1126.701..1131.216 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial Aggregate (cost=566080.54..566080.55 rows=1 width=8) (actual time=1122.313..1122.313 rows=1 loops=3)
-> Hash Join (cost=1.69..562630.46 rows=1380030 width=0) (actual time=244.437..1109.496 rows=228415 loops=3)
Hash Cond: (logrecord.cv_id = l_cversion.id)
-> Parallel Seq Scan on logrecord (cost=0.00..550923.16 rows=4041516 width=4) (actual time=0.042..783.569 rows=3233213 loops=3)
-> Hash (cost=1.51..1.51 rows=14 width=4) (actual time=0.049..0.049 rows=2 loops=3)
Buckets: 1024 Batches: 1 Memory Usage: 9kB
-> Seq Scan on l_cversion (cost=0.00..1.51 rows=14 width=4) (actual time=0.035..0.040 rows=2 loops=3)
Filter: (cvnum >= '2200099000000'::numeric)
Rows Removed by Filter: 39
Planning time: 0.878 ms
Execution time: 1131.304 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment