dmbf29 / predictor-api

match prediction app / back-end API built on Rails
1 stars 4 forks source link

Refactor using Database Views #110

Closed trouni closed 1 year ago

trouni commented 1 year ago

The work in this PR is required before deploying trouni/predictor-vue#212

Creation of materialized database views

Since we have a lot of calculations needed that involve data between different tables, the different queries and calculations outside of ActiveRecord have been hitting our database pretty badly, and loading the matches or leaderboards often took a few seconds for a single API call.

This PR uses the scenic gem to create SQL views (i.e. "virtual" tables based on the results of a specific query) that we can access with the convenience of a Rails model. Normal views simply run the query every time, which would already be a significant performance improvement for us, but I have set the views as "materialized" meaning that the results of the query are stored in an actual database table to further speed up the query time. This however means that the views need to be manually refreshed when adding or updating relevant data. I have added callbacks in the relevant models to refresh the materialized views on create or update.

I have created three materialized views:

Other database migrations

I have made some migrations to remove some of the complexity in the code by adding some redundancies in the columns:

Removal of unnecessary code

I have removed methods that were not used, most notably in the Group and Team models since we are not showing the competition's score and don't have plans to do so at this stage.

Some methods in the User and Match model were unused or made obsolete by the database views, and have been removed.

Refactoring of views

The views have been cleaned up using partials.

Other changes

trouni commented 1 year ago

@dmbf29 I've added some explanation on the PR to help you review. Could you take a look and test today? Would be nice to merge tonight.