Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor live "/:username", ProfileLive #1004

Open
7 of 8 tasks
wintermeyer opened this issue Sep 2, 2024 · 8 comments
Open
7 of 8 tasks

Refactor live "/:username", ProfileLive #1004

wintermeyer opened this issue Sep 2, 2024 · 8 comments
Assignees
Labels
in progress Somebody is working on this issue refactor Refactor code
Milestone

Comments

@wintermeyer
Copy link
Collaborator

wintermeyer commented Sep 2, 2024

Feed the database with the demo users and load a /:username page in your browser. Have a look at the millions of SQL queries which are triggered by that page.

I am sure that there is a massive potential to DRY that code and to improve it. No need to load the same data over and over again from the database.

@briankariuki Please do create sub issues when it makes sense. I prefer small and quick over big PRs since everybody else is working on the same code.

PS: Please add a before/after time when possible. I would like to get a feeling how much performance we gain with this issue.

Tasks

@wintermeyer wintermeyer added this to the MVP milestone Sep 2, 2024
@wintermeyer wintermeyer added the refactor Refactor code label Sep 2, 2024
@wintermeyer
Copy link
Collaborator Author

wintermeyer commented Sep 2, 2024

Code readability is a major point too. Since the application is online now more people look into the code. They should have a chance to understand it.

@briankariuki
Copy link
Collaborator

Let me investigate what's causing the issue. I'll add the findings here before attempting to fix it

@briankariuki
Copy link
Collaborator

The first issue I've noticed is the read :by_user_id action on the Story resource.
It loads the user, which is redundant as already the user_id is passed. The User resource loads a few things when its built which require reading from the database. Hence the many sql query reads for each story fetched with the user preloaded.

I can go ahead and refactor the Story resource not to load the user or do it on the profile_stories_live.ex where we fetch the stories and use the :read action and preload only what we need to display that page.

@wintermeyer what do you think?

@briankariuki
Copy link
Collaborator

The second issue I found is that the UserFlags resource also preloads the user.

@wintermeyer
Copy link
Collaborator Author

@wintermeyer what do you think?

I can't say much about it because I am not deep in the code. I just see all the SQL queries in the log and always think "this can not be the optimal way of doing it". Within the Phoenix world SQL queries are expensive (at least 9 out of 10 times). I am saying this because I used to program a lot with Ruby on Rails and there SQL was much cheaper than Ruby. But for us normal Elixir is cheaper than multiple SQL queries.

Bottom line: Do what makes most sense for you to get more speed and improve readability (hoping that both come together). In case you have to make a choice between speed and code readability I tend to go into the code readability direction.

@wintermeyer
Copy link
Collaborator Author

In my opinion the /:username ProfileLive should not need more than 2-4 SQL queries. In a perfect world it should only need 1 but let's be realistic.

@wintermeyer
Copy link
Collaborator Author

The second issue I found is that the UserFlags resource also preloads the user.

Please keep #1002 in mind while working with the flags.

@wintermeyer
Copy link
Collaborator Author

@briankariuki Please do me a favour and set the in progress flag to this issue while you are working on it. Thanks!

@briankariuki briankariuki added the in progress Somebody is working on this issue label Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in progress Somebody is working on this issue refactor Refactor code
Projects
None yet
Development

No branches or pull requests

2 participants