-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve startup speeds by using temp tables
When the proxy is run with large DBs (10m+ events), the startup queries are very slow (around 30min to load the initial snapshot. After much EXPLAIN ANALYZEing, the cause is due to Postgres' query planner not making good decisions when the the tables are that large. Specifically, the startup queries need to pull all joined members in all rooms, which ends up being nearly 50% of the entire events table of 10m rows. When this query is embedded in a subselect, the query planner assumes that the subselect will return only a few rows, and decides to pull those rows via an index. In this particular case, indexes are the wrong choice, as there are SO MANY rows a Seq Scan is often more appropriate. By using an index (which is a btree), this ends up doing log(n) operations _per row_ or `O(0.5 * n * log(n))` assuming we pull 50% of the table of n rows. As n increases, this is increasingly the wrong call over a basic O(n) seq scan. When n=10m, a seq scan has a cost of 10m, but using indexes has a cost of 16.6m. By dumping the result of the subselect to a temporary table, this allows the query planner to notice that using an index is the wrong thing to do, resulting in better performance. On large DBs, this decreases the startup time from 30m to ~5m.
- Loading branch information
Showing
2 changed files
with
37 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters