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

Improve database performance by removing duplicated queries and using eager loading #3357

Merged
merged 11 commits into from
Jan 7, 2019
Merged

Improve database performance by removing duplicated queries and using eager loading #3357

merged 11 commits into from
Jan 7, 2019

Conversation

AdrienPoupa
Copy link
Contributor

@AdrienPoupa AdrienPoupa commented Dec 7, 2018

This PR eliminates some unnecessary queries:

The DashboardComposer was executed as many times as there were dashboard views rendered, resulting in 5 additional queries for each execution when one was enough. To ensure that it was run only once, I used this method.

Also, for some reason, the retrieval of the setting enable_external_dependencies was done using setting and not Cache::, thus a query to the database was done whenever an avatar was displayed.

In IncidentController, eager loading the user is useful because the dashboard.incident.index view queries $incident->user twice.

In ComponentController, eager loading the group is useful because it is queried twice per component. On my test instance that has 12 components, the dashboard/components goes from 35 queries, 5.3Mb and 60ms to 10 queries, 4.8Mb and 46ms

Finally, on the homepage, by adding eager loadings to $allIncidents in the StatusPageController and creating attributes that act like a cache, on a large database, I have reduced the number of queries from 539 to 118, the memory consumption from 18Mb to 8Mb and the loading time from 530ms to 240ms.

@welcome
Copy link

welcome bot commented Dec 7, 2018

Congratulations on opening your first Pull Request, this is a momentous day for you and us! ✨
To help us out, please make sure that you've followed the below:

@AdrienPoupa AdrienPoupa changed the title Improve database performance by removing duplicated queries Improve database performance by removing duplicated queries and using eager loading Dec 7, 2018
Copy link
Member

@jbrooksuk jbrooksuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great change @AdrienPoupa, thank you very much!

Just a couple of changes, please.

app/Composers/DashboardComposer.php Outdated Show resolved Hide resolved
app/Composers/DashboardComposer.php Outdated Show resolved Hide resolved
app/Presenters/ComponentGroupPresenter.php Show resolved Hide resolved
*/
public function enabled_components_lowest()
{
if (is_bool($this->enabledComponentsLowest)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we checking is_bool if the variable is declared as false by default?

app/Presenters/IncidentPresenter.php Show resolved Hide resolved
@@ -248,9 +250,11 @@ public function latest_icon()
*/
public function latest()
{
if ($update = $this->wrappedObject->updates()->orderBy('created_at', 'desc')->first()) {
return $update;
if (is_bool($this->latest)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just say if ($this->latest).

@jbrooksuk
Copy link
Member

Ping @AdrienPoupa

@AdrienPoupa
Copy link
Contributor Author

Hi,

Thanks for the review. I chose to use a boolean because by default, if a var is not declared in PHP, it's a null. If there is no hit in the database, Eloquent returns a null as well. Thus, we need a way to know if we queried the database at least once; if we were checking a null and Eloquent returned a null we would be checking over and over again...

By using is_bool, we know for sure that for the current request, if the attribute is a boolean, the query was not executed yet. After the execution, it can be set either to its actual value, or null if nothing was found in the database.

@jbrooksuk jbrooksuk merged commit 73eea81 into cachethq:2.4 Jan 7, 2019
@welcome
Copy link

welcome bot commented Jan 7, 2019

Hooray! Your first Pull Request was merged, here's to many more 🚀

@jbrooksuk
Copy link
Member

Awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants