tldr: use l2_keystones in JOIN to avoid page reads of pop_basis
updated query to use l2_keystones rather than pop_basis in JOIN to improve performance
Changes
Here are the before and after query plans; neither are great, but the after is much better.
note the differences between costs and "actual time"s.
when using the l2_keystones result instead of pop_basis, we can use an "Index only scan" for pop_basis. This means we don't have to pull any values from the pop_basis records after the join because the join itself uses the index.
Previously we had to use the result of the pop_basis JOIN to then read the table data then pull l2_keystone_abrev_hash from that. When we use l2_keystones instead, we never have to read the table data, we only need the index itself.
Our cost for this query for the exemplified l2 keystone '\x0013d9736f664d0780583182468bd6d74d4b38234106950d87b14271230ef47f goes from cost=520904.80..520911.52 to cost=34745.71..34752.43
and the timing goes from actual time=5980.838..5980.848 to actual time=151.756..151.762 ❗
Summary
tldr: use
l2_keystones
inJOIN
to avoid page reads ofpop_basis
updated query to use l2_keystones rather than pop_basis in JOIN to improve performance
Changes
Here are the before and after query plans; neither are great, but the after is much better.
note the differences between costs and "actual time"s.
when using the
l2_keystones
result instead ofpop_basis
, we can use an "Index only scan" forpop_basis
. This means we don't have to pull any values from thepop_basis
records after the join because the join itself uses the index.Previously we had to use the result of the
pop_basis
JOIN to then read the table data then pulll2_keystone_abrev_hash
from that. When we usel2_keystones
instead, we never have to read the table data, we only need the index itself.Our cost for this query for the exemplified l2 keystone
'\x0013d9736f664d0780583182468bd6d74d4b38234106950d87b14271230ef47f
goes fromcost=520904.80..520911.52
tocost=34745.71..34752.43
and the timing goes from
actual time=5980.838..5980.848
toactual time=151.756..151.762
❗before
after