perf: optimize tables query #542
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Tables info can be queried faster by calling pg_total_relation_size only once per row, pg_total_relation_size is a volatile function, meaning that it would be called twice per row as volatile functions are not guaranteed the same result per table lookup, but that is not meaningful in this context
Additionally the group by expression can be removed entirely since only one row per oid can be produced by the join conditions
the speed up can be noticed specially in databases with large number of tables, to test this out I will set up a test database and measure the improvement
Set up the environment
We will create a postgres 12 database and populate it with 50000 tables with primary key
Using the following init.sql script:
Let's start a postgres server that initializes with init.sql
Validation
Using the two versions of the query:
current_query
new_query
The first step is to test that both queries produce the same results
Performance analysis
Knowing both queries produce the same result lets check the performance
Prior to each hyperfine run we must restart the database to ensure queries run on equal context
Mean and max time differ on 700ms