Skip to content

⚡ Bolt: Optimize sequential DB queries#121

Open
bobdivx wants to merge 1 commit into
devfrom
bolt-optimize-db-queries-4427519362897185717
Open

⚡ Bolt: Optimize sequential DB queries#121
bobdivx wants to merge 1 commit into
devfrom
bolt-optimize-db-queries-4427519362897185717

Conversation

@bobdivx

@bobdivx bobdivx commented Jun 15, 2026

Copy link
Copy Markdown
Owner

💡 What: Grouped independent database queries in src/pages/api/work-diagnostic.ts using Promise.all().

🎯 Why: Sequential queries that do not depend on each other add unnecessary latency. By executing them concurrently, we reduce the total DB wait time.

📊 Impact: Reduces the wait time for fetching Project, AgentInstruction, and AgentBudget from the sum of their individual execution times to just the duration of the longest query.

🔬 Measurement: Verify by executing the API endpoint /api/work-diagnostic and observing improved response times compared to baseline.


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

💡 What: Grouped independent database queries in src/pages/api/work-diagnostic.ts using Promise.all().
🎯 Why: Sequential queries that do not depend on each other add unnecessary latency. By executing them concurrently, we reduce the total DB wait time.
📊 Impact: Reduces the wait time for fetching Project, AgentInstruction, and AgentBudget from the sum of their individual execution times to just the duration of the longest query.
🔬 Measurement: Verify by executing the API endpoint /api/work-diagnostic and observing improved response times compared to baseline.

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

Copy link
Copy Markdown

👋 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.

@vercel

vercel Bot commented Jun 15, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
forge Ready Ready Preview, Comment Jun 15, 2026 5:40pm

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes the src/pages/api/work-diagnostic.ts endpoint by fetching projects, agents, and budgets concurrently using Promise.all, and documents this change in .jules/bolt.md. The reviewer suggests further optimizing this endpoint by grouping other independent asynchronous operations—such as the costs query, logs query, and work status fetch—into the same Promise.all call to minimize latency.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +12 to +16
const [projects, agents, budgets] = await Promise.all([
db.select().from(Project),
db.select().from(AgentInstruction),
db.select().from(AgentBudget),
]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

While grouping the Project, AgentInstruction, and AgentBudget queries into Promise.all is a great step, the API route still executes several independent asynchronous operations sequentially further down:

  1. The costs query (lines 20-25)
  2. The logs query (lines 30-40)
  3. The work status fetch (line 42)

Since none of these queries depend on the results of each other, they can all be executed concurrently. We can compute startOfMonth first, and then run all 6 queries/fetches in a single Promise.all to minimize the response latency of this endpoint.

Here is how the fully optimized code would look:

export const GET: APIRoute = async () => {
  try {
    const { db, Project, AgentInstruction, ActivityLog, AgentBudget, CostEvent, sql } = await loadAstroDb();

    const now = new Date();
    const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);

    // Fetch all independent data concurrently
    const [projects, agents, budgets, costsResult, logs, work] = await Promise.all([
      db.select().from(Project),
      db.select().from(AgentInstruction),
      db.select().from(AgentBudget),
      db.select({
        total: sql`sum(${CostEvent.costCents})`,
      })
      .from(CostEvent)
      .where(sql`${CostEvent.occurredAt} >= ${startOfMonth}`),
      db.select()
        .from(ActivityLog)
        .where(
          or(
            eq(ActivityLog.action, 'swarm.task.dispatch_failed'),
            eq(ActivityLog.action, 'swarm.issue.dispatch_failed'),
          ),
        )
        .orderBy(desc(ActivityLog.createdAt))
        .limit(20),
      getWorkSystemStatus(),
    ]);

    const activeBudgets = budgets.filter((b) => b.enabled === 1);
    const currentMonthlyCents = Number(costsResult[0]?.total || 0);
    const globalHardStopCents =
      activeBudgets.reduce((acc, b) => acc + Number(b.monthlyCents || 0), 0) || 5000;

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