Databases 9 min read

How Indexes Can Slash MySQL Query Time from Hours to Milliseconds

This article walks through a MySQL 5.6 scenario where a nested subquery to find students scoring 100 in Chinese runs for over 30,000 seconds, then demonstrates how adding single‑column and multi‑column indexes, analyzing execution plans, and applying join optimizations can reduce the runtime to milliseconds, while also covering best practices for index design and query tuning.

Java Backend Technology
Java Backend Technology
Java Backend Technology
How Indexes Can Slash MySQL Query Time from Hours to Milliseconds

Scenario

The database uses MySQL 5.6 with three tables: a student table (100 rows), a score table SC (70,000 rows), and a student‑score table (700,000 rows). The goal is to find students who scored 100 in Chinese.

Initial query

select s.* from Student s where s.s_id in (select s_id from SC sc where sc.c_id = 0 and sc.score = 100)

Execution time: 30248.271 s . The execution plan shows type=ALL (full table scan) and no index usage.

First optimization – add indexes on the WHERE columns

CREATE index sc_c_id_index on SC(c_id);
CREATE index sc_score_index on SC(score);

New execution time: 1.054 s , a >30,000× speedup.

Further analysis

Even 1 s is long for such a query. The execution plan reveals that MySQL first performs a join then filters, causing many row combinations. By rewriting the query to let MySQL filter before joining, performance improves.

Join vs subquery

MySQL can transform the subquery into an EXISTS clause, but still may execute the outer query repeatedly (≈70 000 × 8 loops). Switching to an explicit join and ensuring proper indexes reduces the time to 0.057 s .

Index strategy

Single‑column indexes improve filtering on individual columns.

When data volume grows (e.g., SC table to 3 million rows) and column selectivity is low, single indexes become less effective.

Creating a composite (multi‑column) index on (c_id, score) dramatically reduces execution time to 0.007 s .

MySQL can merge multiple single‑column indexes (INTERSECT) but a composite index is usually faster.

Composite index benefits

Leverages the left‑most prefix rule: the first indexed column must appear in the WHERE clause.

Enables covering indexes when all selected columns are indexed, allowing MySQL to return results directly from the index without accessing the table data.

Additional optimizations

Define numeric columns with the smallest appropriate type.

Build indexes on columns used in JOIN, WHERE, ORDER BY, and GROUP BY clauses.

Avoid functions on indexed columns in WHERE conditions to prevent index loss.

Summary

Nested subqueries in MySQL can be very slow.

Transform them into JOINs when possible.

Filter rows before joining to reduce intermediate result size.

Create appropriate single‑column and multi‑column indexes; composite indexes often give the best performance.

Analyze execution plans regularly; MySQL may rewrite queries, so understanding the plan is crucial.

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 performancedatabase indexingQuery Plan
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.