Skip to content

⚡ Bolt: Optimize Eloquent queries to fix memory and N+1 bottlenecks - #7

Closed
bobdivx wants to merge 1 commit into
mainfrom
bolt-performance-eloquent-optimizations-6669000249635787529
Closed

bobdivx wants to merge 1 commit into
mainfrom
bolt-performance-eloquent-optimizations-6669000249635787529

Conversation

@bobdivx

@bobdivx bobdivx commented Aug 24, 2026

Copy link
Copy Markdown
Owner

💡 What: Optimized several highly inefficient Eloquent queries in CleanupStuckedResources.php and Emails.php.
🎯 Why: The original code used Model::all() followed by collection methods like ->first() or ->filter(). This pulls every single row of a table into memory, leading to massive memory bloat and slow execution times for large datasets. In one case, it also triggered a severe N+1 query problem.
📊 Impact:

  • Eliminates an O(N) memory allocation problem in Emails.php (now O(1) with LIMIT 1).
  • Eliminates a 2N+1 query execution bottleneck when finding empty teams in CleanupStuckedResources.php (now a single query).
    🔬 Measurement: Can be verified by running query logs or measuring peak memory usage of the cleanup:stucked-resources and emails console commands.

PR created automatically by Jules for task 6669000249635787529 started by @bobdivx

- Refactored `Team::all()->filter(...)` in CleanupStuckedResources to `Team::doesntHave('members')->doesntHave('servers')->get()` to eliminate an N+1 query issue and memory bottleneck.
- Refactored `StandalonePostgresql::all()->where(...)` to `StandalonePostgresql::where(...)->get()` to filter at the database level.
- Replaced multiple instances of `Model::all()->first()` with `Model::first()` in Emails.php to prevent fetching entire tables into memory just to retrieve a single record.

Co-authored-by: bobdivx <6737167+bobdivx@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@bobdivx bobdivx closed this Sep 11, 2026
@bobdivx
bobdivx deleted the bolt-performance-eloquent-optimizations-6669000249635787529 branch October 22, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant