Skip to content

Commit cc0781d

Browse files
authored
perf: add indexes for slow db queries (#698)
1 parent daced1e commit cc0781d

3 files changed

Lines changed: 63 additions & 6 deletions

File tree

app/Concerns/BelongsToNurseThroughBeneficiary.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,26 @@ public function scopeForUser(Builder $query, User $user): Builder
3636

3737
public function scopeForNurse(Builder $query, User $user): Builder
3838
{
39-
return $query->whereRelation('beneficiary', 'nurse_id', $user->id);
39+
return $query->whereHas(
40+
'beneficiary',
41+
fn (Builder $query): Builder => $query
42+
// The parent row is already nurse-scoped
43+
->withoutGlobalScope('forCurrentUser')
44+
->where('nurse_id', $user->id)
45+
);
4046
}
4147

4248
public function scopeForMediator(Builder $query, User $user): Builder
4349
{
4450
return $query->where(
4551
fn (Builder $query): Builder => $query
46-
->whereRelation('beneficiary', 'mediator_id', $user->id)
52+
->whereHas(
53+
'beneficiary',
54+
fn (Builder $query): Builder => $query
55+
// The parent row is already nurse-scoped
56+
->withoutGlobalScope('forCurrentUser')
57+
->where('mediator_id', $user->id)
58+
)
4759
->when(
4860
$this->isFillable('mediator_has_access'),
4961
fn (Builder $query): Builder => $query->where('mediator_has_access', true)
@@ -53,10 +65,15 @@ public function scopeForMediator(Builder $query, User $user): Builder
5365

5466
public function scopeForCoordinator(Builder $query, User $user): Builder
5567
{
56-
return $query->where(
68+
return $query->whereHas(
69+
'beneficiary',
5770
fn (Builder $query): Builder => $query
58-
->whereRelation('beneficiary.nurse', 'activity_county_id', $user->county_id)
59-
->orWhereRelation('beneficiary.mediator', 'activity_county_id', $user->county_id)
71+
->withoutGlobalScope('forCurrentUser')
72+
->where(
73+
fn (Builder $query): Builder => $query
74+
->whereRelation('nurse', 'activity_county_id', $user->county_id)
75+
->orWhereRelation('mediator', 'activity_county_id', $user->county_id)
76+
)
6077
);
6178
}
6279
}

app/Filament/Widgets/OpenCasesWidget.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public function table(Table $table): Table
3333
->onlyOpen()
3434
->withCount([
3535
'appointments' => fn (Builder $query) => $query->countUnique(),
36-
'interventions as realized_interventions_count' => fn (Builder $query) => $query->onlyRealized(),
36+
'interventions as realized_interventions_count' => fn (Builder $query): Builder => $query
37+
// The parent row is already nurse-scoped, so the child count
38+
// doesn't need to re-derive the beneficiary→nurse `exists`.
39+
->withoutGlobalScope('forCurrentUser')
40+
->onlyRealized(),
3741
]);
3842
})
3943
->heading(__('intervention.title.open_cases_widget'))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*/
14+
public function up(): void
15+
{
16+
// For the open-cases widget (interventionable_type + closed_at IS NULL)
17+
// and the stats trend queries (whereBetween('closed_at')).
18+
Schema::table('interventions', function (Blueprint $table) {
19+
$table->index(['interventionable_type', 'closed_at']);
20+
});
21+
22+
// For the onlyRealized()/onlyPlanned() correlated `exists (... where status = ?)`.
23+
Schema::table('interventionable_individual_services', function (Blueprint $table) {
24+
$table->index('status');
25+
});
26+
27+
// For the beneficiary-status report query: the log_name + event filter,
28+
// plus the LEAD() OVER (PARTITION BY subject_id ORDER BY created_at) window.
29+
Schema::table('activity_log', function (Blueprint $table) {
30+
$table->index(
31+
['subject_type', 'log_name', 'event', 'subject_id', 'created_at'],
32+
'activity_log_subject_event_created_at_index'
33+
);
34+
});
35+
}
36+
};

0 commit comments

Comments
 (0)