MySQL Forums
Forum List  »  General

How to Optimize a Query with Multiple Joins and Conditions?
Posted by: Alex Myrepal
Date: January 05, 2025 06:43PM

Hello everyone,

I’m working on a MySQL database with a query that involves several JOIN operations across large tables, and it’s significantly impacting performance. Here’s a simplified version of my query:

sql
Копировать код
SELECT
t1.id, t1.name, t2.status, t3.created_at
FROM
table1 t1
JOIN
table2 t2 ON t1.id = t2.t1_id
LEFT JOIN
table3 t3 ON t1.id = t3.t1_id
WHERE
t2.status = 'active'
AND t3.created_at > '2024-01-01'
ORDER BY
t3.created_at DESC;
Problem:
The query is taking a long time to execute, especially as the tables grow larger (e.g., table1 has ~1M rows, table2 has ~500k rows, and table3 has ~2M rows).
I’ve indexed the id columns and the status column in table2, but the performance improvement is minimal.
My Questions:
What’s the best way to optimize this query to handle large datasets more efficiently?
Are there specific indexing strategies I should use for this type of query (e.g., compound indexes)?
Would restructuring the query or using temporary tables improve performance?
Is there any MySQL feature, like query caching or partitions, that can help in this scenario?
I’ve already run EXPLAIN on the query, but I’d like guidance on interpreting the output and applying meaningful optimizations.


Any suggestions or examples would be greatly appreciated!

Thank you in advance for your help.

Options: ReplyQuote


Subject
Written By
Posted
How to Optimize a Query with Multiple Joins and Conditions?
January 05, 2025 06:43PM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.