July 25, 2026 8 min read

Optimizing MySQL Queries for High-Traffic Laravel Apps

Written by Asim Ali
Optimizing MySQL Queries for High-Traffic Laravel Apps Illustration

As Laravel applications scale, database queries are almost always the first major bottleneck. Eloquent's clean syntax makes prototyping fast, but it can easily hide complex joins, missing indexes, and heavy geospatial calculations. In this article, we'll dive into practical strategies to profile queries, configure composite indexes, and optimize location-based searches utilizing MySQL GIS functions.

1. Profile Queries using the EXPLAIN Statement

Before optimizing, you must analyze how MySQL executes your query. By prepending EXPLAIN to your raw SQL, MySQL returns its execution plan, highlighting which indexes are evaluated and how many rows are scanned.

EXPLAIN SELECT * FROM businesses 
WHERE category_id = 5 
AND is_active = 1 
ORDER BY rating_average DESC;

Look at the type and rows columns in the explain results. If the type is ALL, MySQL is performing a full table scan. If it is ref or range, it is utilizing an index, which is highly desired.

2. Designing High-Performance Composite Indexes

When searching by multiple fields, creating single-column indexes on each field is not sufficient. MySQL can usually only use one index per query. Instead, you should create a composite index covering all search criteria.

The order of columns in a composite index is critical. Follow the equality-range-sort rule: put columns compared with equality first (=), columns compared with ranges second (>, <, LIKE), and sorting columns (ORDER BY) last.

In Laravel, you can define composite indexes easily within your database migrations:

Schema::table('businesses', function (Blueprint $table) {
    // Composite index for active businesses in a category sorted by rating
    $table->index(['category_id', 'is_active', 'rating_average']);
});

3. Geospatial Map Searches and ST_Distance_Sphere

A classic performance bottleneck in local directory portals is running geospatial coordinate math. A common mistake is using traditional mathematical formulas (like the Haversine equation) directly inside your SQL query. This prevents MySQL from using index optimization and forces a full table scan.

Instead, store coordinates using the MySQL native POINT spatial data type and query distances using ST_Distance_Sphere:

$latitude = 30.6777; // Sahiwal coordinates
$longitude = 73.1026;

$nearbyBusinesses = Business::query()
    ->selectRaw("id, name, ST_Distance_Sphere(location, POINT(?, ?)) as distance", [$longitude, $latitude])
    ->whereRaw("ST_Distance_Sphere(location, POINT(?, ?)) <= ?", [$longitude, $latitude, 5000]) // 5km radius
    ->orderBy('distance')
    ->get();

By mapping this point field to a Spatial Index in MySQL, queries on thousands of geographic coordinates return in under 100 milliseconds!

"Database optimization is not about making SQL queries run faster on a strong machine; it is about writing structured indexes that prevent the database engine from reading redundant pages in memory."