October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Find and Fix Slow MySQL Queries in Laravel: A Practical Workflow

Trace database time in Laravel, inspect slow MySQL statements with EXPLAIN, and validate a targeted fix against representative data.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

MySQL 8.4: Optimizing SELECT Statements

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

  1. Record the baseline. Capture the representative statement, its bindings, the relevant execution plan, and the request behavior you are trying to improve.
  2. 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.
  3. 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.
  4. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.