MatrixSynapse/synapse/storage
Dagfinn Ilmari Mannsåker df8a3cef6b
Improve performance of _get_state_groups_from_groups_txn (#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>
2020-06-01 15:23:43 +01:00
..
data_stores Improve performance of _get_state_groups_from_groups_txn (#7567) 2020-06-01 15:23:43 +01:00
engines Persist user interactive authentication sessions (#7302) 2020-04-30 13:47:49 -04:00
schema Only run one background update at a time 2020-03-31 17:43:58 +01:00
util Move event stream handling out of slave store. (#7491) 2020-05-15 16:43:59 +01:00
__init__.py
_base.py remove `builtins.buffer` code from storage code 2020-05-15 19:37:41 +01:00
background_updates.py Update docstring per review comments 2020-04-03 10:51:32 +01:00
database.py Replace device_27_unique_idx bg update with a fg one (#7562) 2020-05-26 11:43:17 +01:00
keys.py
persist_events.py Add option to move event persistence off master (#7517) 2020-05-22 16:11:35 +01:00
prepare_database.py Fix incorrect placeholder syntax in database prepartion code (#7575) 2020-05-27 16:26:59 +01:00
presence.py
purge_events.py
push_rule.py
relations.py
roommember.py
state.py
types.py