Queries
The Queries resource provides a centralized view of every SQL statement your application executes. Use this page to identify slow queries, N+1 patterns, and duplicates, and to receive AI-driven recommendations for optimization.
Navigation: Access Queries via the sidebar under the “Queries” section.
1. Queries Index Table
Section titled “1. Queries Index Table”The main content of the Queries page is a table listing individual query executions. By default, the table displays 25 queries, ordered by Executed At in descending order (most recent first). Click Load More at the bottom to fetch additional records.
1.1 Columns
Section titled “1.1 Columns”-
Executed At
- Timestamp when the SQL query was run (e.g.,
2025-05-30 14:23:07
).
- Timestamp when the SQL query was run (e.g.,
-
Query
- The full SQL statement, including any bindings or parameters (e.g.,
SELECT * FROM users WHERE status = 'active'
).
- The full SQL statement, including any bindings or parameters (e.g.,
-
Duration
- Total time taken by the database engine to execute the query, in milliseconds (e.g.,
45 ms
).
- Total time taken by the database engine to execute the query, in milliseconds (e.g.,
-
Occurrence
- The context in which this query ran, indicating the parent execution type and ID:
- Request (e.g.,
Request #1023
) - Command (e.g.,
Command cache:clear
) - Scheduled Task (e.g.,
Task backup:run
) - Queued Job (e.g.,
Job SendEmailJob
)
- Request (e.g.,
- Clicking the occurrence link navigates you to the corresponding details page (e.g., Request Details) so you can see the full event timeline around this query.
- The context in which this query ran, indicating the parent execution type and ID:
-
Optimize with AI
- A link or button labeled Optimize with AI. Clicking this opens a dialog that provides AI-driven recommendations for optimizing this specific query.
- The AI engine is fully aware of your application’s database schema, indexes, and table relationships, so suggestions are tailored to your exact environment (e.g., recommending composite indexes, rewrite to use joins instead of subqueries, or highlight missing
WHERE
clauses).
Tip: Use Optimize with AI for slow or frequently-run queries to receive actionable guidance on how to refactor or index them.
2. Filters
Section titled “2. Filters”Above the Queries table, a set of filter controls allows you to refine which query executions are displayed. Filters can be combined to narrow your search:
-
Search
- A free-text search field that matches against the Query text.
- Partial matches and substrings are supported (e.g., searching for “JOIN users” will match all queries containing that fragment).
-
Date & Time Range
- Select a Start Date & Time and End Date & Time to show only queries executed within that window.
-
Duration
- A numeric input (in milliseconds) to filter queries whose execution time is above or below a specified threshold (e.g., show queries longer than
100 ms
).
- A numeric input (in milliseconds) to filter queries whose execution time is above or below a specified threshold (e.g., show queries longer than
-
N+1 Badge
- A clickable badge labeled N+1. Clicking filters to queries identified as part of an N+1 pattern—e.g., repeated queries with the same structure but different bindings.
- Use this to locate inefficient loops or missing eager loads in your application code.
-
Duplicate Badge
- A clickable badge labeled Duplicate. Clicking filters to queries that were executed multiple times within the same occurrence (e.g., identical SQL statements fired repeatedly during a single request or job).
- Helps you identify opportunities to cache results or batch queries.
Tip: Combine N+1 and Duplicate filters with a narrow date range to pinpoint hot spots for optimization during recent deployments.
3. Load More
Section titled “3. Load More”- By default, the Queries table shows the 25 most recent entries.
- Click the Load More button at the bottom to retrieve the next batch of 25 queries, ordered by Executed At.
- Incremental loading ensures quick initial page loads while still providing access to your full query history.
4. Optimize with AI
Section titled “4. Optimize with AI”Each row’s Optimize with AI link launches a modal dialog that analyzes the specific query in the context of your database schema and historical performance metrics. The AI recommendations may include:
- Index Suggestions
- Propose single-column or composite indexes based on filtered columns and join predicates.
- Query Rewrites
- Recommend rewriting subqueries or correlated queries to use more efficient joins or apply
EXISTS
clauses.
- Recommend rewriting subqueries or correlated queries to use more efficient joins or apply
- Redundant Condition Removal
- Highlight unnecessary
ORDER BY
orGROUP BY
clauses that slow down execution without affecting results.
- Highlight unnecessary
- Batching Opportunities
- Identify small, repeatable queries that can be batched into a single query (e.g., converting multiple
SELECT
statements into a singleWHERE IN
).
- Identify small, repeatable queries that can be batched into a single query (e.g., converting multiple
- Schema Refactoring Advice
- Suggest denormalization or adding foreign key constraints if beneficial for query performance.
Tip: Preview AI recommendations in a staging environment before applying changes to production. Always test index additions and query rewrites to confirm performance gains.
5. Best Practices
Section titled “5. Best Practices”- Filter by Duration First
- Start with the Duration filter to surface the slowest queries; then click Optimize with AI to get targeted recommendations.
- Tackle N+1 Patterns
- Use the N+1 badge filter to find opportunities to add eager loading (e.g.,
with()
) or refactor loops that fire repetitive queries.
- Use the N+1 badge filter to find opportunities to add eager loading (e.g.,
- Eliminate Duplicate Queries
- Use the Duplicate badge filter to locate identical queries within the same occurrence; caching or consolidating these can reduce load.
- Correlate with Occurrences
- Click on the Occurrence link to see the full event timeline (request, command, task, or job) where the query ran. This helps you understand downstream or upstream factors affecting performance.
- Leverage Date Ranges
- Narrow in on a deployment window by specifying a Start Date & Time and End Date & Time—especially useful when investigating performance regressions after a release.
- Implement AI Suggestions Cautiously
- Use the Optimize with AI dialog to guide your refactoring, but always benchmark changes (e.g., via
EXPLAIN ANALYZE
or profiling tools) before committing to schema modifications.
- Use the Optimize with AI dialog to guide your refactoring, but always benchmark changes (e.g., via
By leveraging the Queries index, powerful filters, and AI-driven optimization guidance, Laritor helps you identify and improve slow or redundant SQL statements—leading to faster response times, reduced database load, and a more efficient application overall.