Databases 7 min read

Boost MySQL Performance: 8 Proven SQL Optimization Techniques

This article presents eight practical MySQL optimization strategies—including LIMIT redesign, implicit type conversion fixes, JOIN rewrites for updates/deletes, mixed sorting tricks, EXISTS elimination, condition push‑down, early data reduction, and intermediate result set push‑down—each illustrated with before‑and‑after queries, execution plans, and measurable speed improvements.

ITPUB
ITPUB
ITPUB
Boost MySQL Performance: 8 Proven SQL Optimization Techniques

Pagination is a common but error‑prone scenario; a simple query with LIMIT 1000000,10 can be slow because the database must scan from the start even with indexes. Rewriting the query to use the previous page's maximum value as a filter makes execution time essentially constant regardless of data size.

1. LIMIT Clause Redesign

Replace offset‑based LIMIT with a condition on the last retrieved key, eliminating full scans.

2. Implicit Type Conversion

When a column defined as VARCHAR(20) is compared to a numeric value, MySQL converts the string to a number, causing index loss. Ensure matching data types or cast explicitly to preserve index usage.

3. Update/Delete via JOIN

MySQL 5.6’s materialized subquery optimization applies only to SELECTs. For UPDATE/DELETE, rewrite statements as JOINs to avoid dependent subqueries, reducing execution from seconds to milliseconds.

4. Mixed Sorting

MySQL cannot use indexes for mixed orderings, but specific tricks (e.g., adding covering indexes or restructuring conditions) can dramatically cut execution time, as shown by before‑and‑after plans.

5. EXISTS Replacement

EXISTS often triggers nested subqueries; converting to JOIN eliminates the subquery and can drop execution time from ~2 s to ~1 ms.

6. Condition Push‑Down

External predicates cannot be pushed into complex views or subqueries such as aggregates, LIMIT‑containing subqueries, UNIONs, or scalar subqueries. When push‑down is possible, rewrite the query so the optimizer can apply filters early, improving performance.

7. Early Data Reduction

When the final WHERE clause and ORDER BY target the leftmost table, sort that table first and then perform LEFT JOINs. This reduces rows processed from hundreds of thousands to a few, cutting runtime from seconds to ~1 ms.

8. Intermediate Result Set Push‑Down

For queries with repeated subqueries, use a WITH clause (CTE) to materialize once and reference multiple times, simplifying the plan and reducing execution time from seconds to milliseconds.

Overall, understanding the database compiler’s limitations and applying these techniques enables developers to write high‑performance SQL across various database systems.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mysqlSQL OptimizationCTEjoin rewritecondition push-downLIMIT redesign
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.