Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Improve performance of _get_state_groups_from_groups_txn #7567

Commits on May 30, 2020

  1. Improve performance of _get_state_groups_from_groups_txn (matrix-org#…

    …7567)
    
    The query keeps showing up in my slow query log.
    
    This changes the plan under the top-level Sort node from
    
        WindowAgg  (cost=280335.88..292963.15 rows=561212 width=80) (actual time=138.651..160.562 rows=27112 loops=1)
          ->  Sort  (cost=280335.88..281738.91 rows=561212 width=84) (actual time=138.597..140.622 rows=27112 loops=1)
                Sort Key: state_groups_state.type, state_groups_state.state_key, state_groups_state.state_group
                Sort Method: quicksort  Memory: 4581kB
                ->  Nested Loop  (cost=2.83..226745.22 rows=561212 width=84) (actual time=21.548..47.657 rows=27112 loops=1)
                      ->  HashAggregate  (cost=2.27..3.28 rows=101 width=8) (actual time=21.526..21.535 rows=20 loops=1)
                            Group Key: state.state_group
                            ->  CTE Scan on state  (cost=0.00..2.02 rows=101 width=8) (actual time=21.280..21.493 rows=20 loops=1)
                      ->  Index Scan using state_groups_state_type_idx on state_groups_state  (cost=0.56..2189.40 rows=5557 width=84) (actual time=0.005..0.991 rows=1356 loops=20)
                            Index Cond: (state_group = state.state_group)
    
    to
    
        Nested Loop  (cost=2.83..226745.22 rows=561212 width=84) (actual time=24.194..52.834 rows=27112 loops=1)
          ->  HashAggregate  (cost=2.27..3.28 rows=101 width=8) (actual time=24.130..24.138 rows=20 loops=1)
                Group Key: state.state_group
                ->  CTE Scan on state  (cost=0.00..2.02 rows=101 width=8) (actual time=23.887..24.113 rows=20 loops=1)
          ->  Index Scan using state_groups_state_type_idx on state_groups_state  (cost=0.56..2189.40 rows=5557 width=84) (actual time=0.016..1.159 rows=1356 loops=20)
                Index Cond: (state_group = state.state_group)
    
    This cuts the execution time from ~190ms to ~130ms, i.e. a reduction
    of ~30%.
    
    The full plans are visualised at https://explain.depesz.com/s/WpbT and
    https://explain.depesz.com/s/KlEk
    
    Signed-off-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
    ilmari committed May 30, 2020
    Configuration menu
    Copy the full SHA
    f16584c View commit details
    Browse the repository at this point in the history

Commits on Jun 1, 2020

  1. Configuration menu
    Copy the full SHA
    3399b9b View commit details
    Browse the repository at this point in the history