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) Count and Sum Queries #1376

Merged
merged 4 commits into from
Jun 4, 2020
Merged

(Refactor) Count and Sum Queries #1376

merged 4 commits into from
Jun 4, 2020

Conversation

HDVinnie
Copy link
Collaborator

@HDVinnie HDVinnie commented Jun 3, 2020

Ideally we'd like to calculate these values using a single database query. What we were doing was not the case. The trick is to put conditions within aggregate functions. Here's an example in Query Builder that we are performing in this PR to combine multiple count or sum queries into one for better performance!

Calculating Totals Using Conditional Aggregates

Combine multiple count queries into one

- $total = Subscriber::count();
- $confirmed = Subscriber::where('status', 'confirmed')->count();
- $unconfirmed = Subscriber::where('status', 'unconfirmed')->count();
- $cancelled = Subscriber::where('status', 'cancelled')->count();
- $bounced = Subscriber::where('status', 'bounced')->count();

+ $totals = DB::table('subscribers')
+   ->selectRaw('count(*) as total')
+    ->selectRaw("count(case when status = 'confirmed' then 1 end) as confirmed")
+   ->selectRaw("count(case when status = 'unconfirmed' then 1 end) as unconfirmed")
+    ->selectRaw("count(case when status = 'cancelled' then 1 end) as cancelled")
+    ->selectRaw("count(case when status = 'bounced' then 1 end) as bounced")
+    ->first();

Which is then called in the view like so:

<div>Total: {{ $totals->total }}</div>
<div>Confirmed: {{ $totals->confirmed }}</div>
<div>Unconfirmed: {{ $totals->unconfirmed }}</div>
<div>Cancelled: {{ $totals->cancelled }}</div>
<div>Bounced: {{ $totals->bounced }}</div>

@codecov
Copy link

codecov bot commented Jun 3, 2020

Codecov Report

Merging #1376 into master will decrease coverage by 1.45%.
The diff coverage is 0.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1376      +/-   ##
============================================
- Coverage     28.08%   26.62%   -1.46%     
  Complexity     4245     4245              
============================================
  Files           304      304              
  Lines         13346    13351       +5     
============================================
- Hits           3748     3555     -193     
- Misses         9598     9796     +198     
Impacted Files Coverage Δ Complexity Δ
app/Http/Controllers/RequestController.php 0.00% <0.00%> (ø) 90.00 <0.00> (ø)
app/Http/Controllers/Staff/HomeController.php 0.00% <0.00%> (ø) 3.00 <0.00> (ø)
app/Services/Data/Person.php 0.00% <0.00%> (-87.50%) 7.00% <0.00%> (ø%)
app/Services/Clients/TmdbClient.php 6.91% <0.00%> (-68.67%) 90.00% <0.00%> (ø%)
app/Services/Data/Genre.php 0.00% <0.00%> (-60.00%) 7.00% <0.00%> (ø%)
app/Helpers/TorrentHelper.php 30.00% <0.00%> (-30.00%) 10.00% <0.00%> (ø%)
app/Services/Clients/Client.php 62.06% <0.00%> (-24.14%) 15.00% <0.00%> (ø%)
app/Services/Data/Movie.php 17.46% <0.00%> (-15.88%) 40.00% <0.00%> (ø%)
app/Models/User.php 40.87% <0.00%> (-2.78%) 122.00% <0.00%> (ø%)
app/Http/Controllers/API/TorrentController.php 70.21% <0.00%> (-2.13%) 108.00% <0.00%> (ø%)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c5de298...d3bc402. Read the comment docs.

@HDVinnie HDVinnie changed the title (Refactor) Count Queries (Refactor) Count and Sum Queries Jun 3, 2020
@HDVinnie HDVinnie merged commit c22701b into master Jun 4, 2020
@HDVinnie HDVinnie removed the WIP label Jun 4, 2020
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.

1 participant