To find a slow MySQL query in Laravel, first measure database activity in the request with Laravel’s query hooks, then inspect a representative SQL statement with MySQL’s slow query log and EXPLAIN. Use the execution plan and your schema to form a targeted change, then test it against representative data. Laravel’s hooks connect database work to application requests; MySQL’s tools help diagnose what the server is doing with a statement.
Start by separating database time from total request time
A slow page or API response does not automatically mean MySQL is the cause. Laravel query observation can show the database work performed during a request, but it does not measure all application work outside SQL. Use it to determine whether database activity is a likely contributor before changing a query or adding an index.
As an Amazon Associate I earn from qualifying purchases.
Observe individual queries with DB::listen
Laravel’s DB::listen lets you observe SQL queries executed by the application, including individual query events. Use it in a controlled diagnostic or development context to identify unexpectedly expensive statements and repeated queries. Check the Laravel documentation for the version installed in your project before copying an API example: the cited documentation covers Laravel 13.x.
Laravel 13.x: Listening for Query Events
Catch requests with high cumulative query time
When the concern is database time accumulated across multiple statements, Laravel’s whenQueryingForLongerThan callback accepts a threshold in milliseconds and runs when query time in a single request exceeds it. This can flag a database-heavy request even when no one statement crosses the threshold. Choose a threshold that fits your application; the documentation does not prescribe a universal value.
#1 Best Overall
Laravel also documents db:monitor, which can dispatch an event when open database connections exceed a configured count. That indicates connection pressure, not that a particular SQL statement is slow.
Laravel 13.x: Monitoring Cumulative Query Time
Use MySQL’s slow query log for server-side evidence
Laravel’s query hooks provide application context. MySQL’s slow query log records statements that meet server-configured criteria, which can help identify slow statements beyond a single request-level investigation.
Rank #2
In the MySQL 8.0 Reference Manual, the slow query log is disabled by default. It logs queries that take longer than long_query_time and examine at least min_examined_row_limit rows. The documented MySQL 8.0 default for long_query_time is 10 seconds. That is a server default, not a recommended Laravel response-time budget or a universal definition of a slow query.
Recommended Free Tools
MySQL 8.0: The Slow Query Log · MySQL 8.0: Server System Variables
Scope configuration changes and manage log volume
MySQL can also log queries that do not use indexes through log_queries_not_using_indexes. The manual warns this can make the log grow quickly; throttling is available. Treat changes to logging settings as operational decisions: check the exact behavior supported by your MySQL release and hosting environment, monitor the resulting log volume, and revert settings that are no longer needed.
Inspect the statement with EXPLAIN
Take a representative SQL statement and its bindings from the Laravel query event or slow log, then examine its execution plan with EXPLAIN. The plan describes the operations MySQL selects to execute the statement, including index choices. Review it alongside the query’s filters, joins, sort order, and the distribution of data; a single plan field is not enough to prove what change will help.
MySQL 8.0: Understanding the Query Execution Plan
For a slow SELECT ... WHERE query, MySQL’s optimization guidance says to check whether an index can help, including on columns used in the WHERE clause. It also recommends avoiding full table scans on large tables where possible, examining expensive expressions, and keeping table statistics current with ANALYZE TABLE where appropriate.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
These are diagnostic directions, not a rule to add an index to every filtered column. Whether an index helps depends on the statement, schema, data, and workload.
Best Value
MySQL 8.4: Optimizing SELECT Statements
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Make one targeted change and validate the trade-offs
Use the execution plan and workload to state a specific hypothesis before changing code or schema. Possible areas to investigate include an absent or unsuitable index, retrieving more rows or columns than needed, an expensive expression, or an inefficient query shape. The right fix cannot be determined without the actual query, schema, plan, and data.
- Record the baseline. Capture the representative statement, its bindings, the relevant execution plan, and the request behavior you are trying to improve.
- Choose one change. For example, test a candidate index only if the plan and query pattern suggest it could help. Avoid changing several parts of the query and schema at once; doing so makes the result harder to interpret.
- Test with representative data. Compare the statement and request under conditions relevant to the real workload, rather than assuming that a change will help based on its appearance alone.
- Check costs as well as reads. Indexes use storage and add work to writes. Keep a proposed index only when its benefits make sense for the application’s actual read and write patterns.
Recheck the Laravel request after the change
Confirm that the request now issues the intended number of queries and that the changed statement’s plan and latency improve under relevant conditions. Use Laravel’s cumulative query-time callback to identify database-heavy requests, and the slow query log to find statements that meet its configured criteria. Neither mechanism supplies a universal threshold for your application.
For version-specific behavior, use documentation matching the Laravel and MySQL versions actually deployed. The cited Laravel pages are for 13.x, while the slow-log, execution-plan, and system-variable references are for MySQL 8.0; the SELECT optimization reference is for MySQL 8.4. Hosting environments may also restrict or alter server settings.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsQuick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




