synapse-product/synapse
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
..
_scripts
api Implement room version 6 (MSC2240). (#7506) 2020-05-15 09:30:10 -04:00
app Ensure we persist and ack the same token 2020-05-27 19:45:42 +01:00
appservice Fix a couple of small typos 2020-05-15 18:54:32 +01:00
config Fix sample config docs error (#7581) 2020-05-27 13:52:18 +01:00
crypto Clean up some LoggingContext stuff (#7120) 2020-03-24 14:45:33 +00:00
events Strictly enforce canonicaljson requirements in a new room version (#7381) 2020-05-14 13:24:01 -04:00
federation add a comment 2020-05-21 13:25:41 +01:00
groups Convert groups local and server to async/await. (#7600) 2020-06-01 07:28:43 -04:00
handlers Convert groups local and server to async/await. (#7600) 2020-06-01 07:28:43 -04:00
http Fix missing CORS headers on OPTION responses (#7560) 2020-05-22 17:42:39 +01:00
logging remove miscellaneous PY2 code 2020-05-15 19:37:41 +01:00
metrics Make inflight background metrics more efficient. (#7597) 2020-05-29 13:25:32 +01:00
module_api Convert auth handler to async/await (#7261) 2020-04-15 12:40:18 -04:00
push Convert sending mail to async/await. (#7557) 2020-05-22 13:41:11 +01:00
replication Remove spurious change 2020-05-27 19:41:44 +01:00
res/templates Implement OpenID Connect-based login (#7256) 2020-05-08 08:30:40 -04:00
rest Add option to move event persistence off master (#7517) 2020-05-22 16:11:35 +01:00
server_notices Optimise some references to hs.config (#7546) 2020-05-22 21:47:07 +01:00
spam_checker_api
state Allow configuration of Synapse's cache without using synctl or environment variables (#6391) 2020-05-11 18:45:23 +01:00
static Make it clearer to the user that they haven't overridden onLogin/Register (#7237) 2020-04-09 12:38:38 +01:00
storage Improve performance of _get_state_groups_from_groups_txn (#7567) 2020-06-01 15:23:43 +01:00
streams
util Speed up processing of federation stream RDATA rows. 2020-05-27 19:34:07 +01:00
__init__.py 1.14.0 2020-05-28 12:36:00 +02:00
event_auth.py Add type hints to event_auth code. (#7505) 2020-05-15 11:19:43 -04:00
notifier.py Fix new flake8 errors (#7489) 2020-05-13 08:24:50 -04:00
python_dependencies.py Implement OpenID Connect-based login (#7256) 2020-05-08 08:30:40 -04:00
secrets.py
server.py Add ability to wait for replication streams (#7542) 2020-05-22 14:21:54 +01:00
server.pyi Add ability to wait for replication streams (#7542) 2020-05-22 14:21:54 +01:00
types.py
visibility.py Refactor a bit 2020-03-11 18:49:41 +00:00