Account
Categories

Optimizing Queries and Joins in MySQL: Best Practices with Examples


Definition:
Optimizing joins and queries is important for improving MySQL performance. Using joins correctly and applying filters smartly helps queries run faster and reduces the load on the system.

1. Optimizing Queries

1.1 Select Only Necessary Columns (Avoid SELECT)

If you want to search data in a specific column, you should use the SELECT query with only that column's name along with the table name. It searches only in the specific column of the table. You should avoid searching the entire table or selecting all columns unnecessarily, as it reduces the speed of the SELECT query.

1.2. Apply Filters Early (Use WHERE clause properly)

If you want fast query searches, then you must apply the filter using the WHERE clause with the SELECT command. It reduces the number of rows the query needs to process, making it run faster. You should not apply the filters too late, because if you do so, then the server first loads all the rows including the unwanted ones and then filters them. This wastes time. But if you apply the filter early using the WHERE clause, then only the needed rows are processed — and your query runs faster.

Example:

If you want to search for a user with a specific email, you should apply the filter early, so the query processes only the necessary rows.

❌ Wrong Query:

SELECT * FROM users WHERE email = 'example@example.com';

✅ Correct Query:

SELECT name, email FROM users WHERE email = 'example@example.com';

1.3. Use Proper Indexes in JOIN Columns

If you use indexing in JOIN columns, such as Column A and Column B, you should add indexes on the columns that are used in the JOIN condition. Then, the query will search data in the rows faster and run quickly.

Note:
Indexing makes the search query faster and improves the overall performance.

Example:

If you are joining two tables — the first table is: students and the second is: marks, and you JOIN the student_idcolumn that is common in both tables. and the student_id column in both tables has proper indexing, then the JOIN will search for matching rows faster.
But if there is no index, then the query will scan the entire table, which slows down the performance.

1.4. Avoid Nested Subqueries if Not Needed

If you use nested subqueries (a query inside another query) without actual need, then it increases the time to process the data. Because first the inner query runs, then the outer query depends on that result — which takes more time. But if the same result can be achieved with a single query or using JOIN, then avoid using nested subqueries. It will make the query faster and improve performance.

Example:


Instead of writing:
SELECT name FROM students
WHERE id IN (SELECT student_id FROM marks WHERE marks > 80);

Try using JOIN:
SELECT students.name FROM students
JOIN marks ON students.id = marks.student_id
WHERE marks.marks > 80;
    

1.5. Use LIMIT with Large Result Sets

If you have a lot of data and need to fetch specific rows, you must use the LIMIT in the SELECT query. It fetches only the rows you specify in LIMIT, so it does not load extra data. This makes the query faster, reduces server load, and saves time.

Example:

Suppose you have a big table named products, and you just want to see the first 10 products.

Query

SELECT * FROM products LIMIT 10;