If I have a query like this
select * from student
inner join courses on courses.id = student.course_id
where student.gpa >= 3.0
order by student.gpa
limit 50;
How would Mysql execute this query to optimize the cost?
If I have a query like this
select * from student
inner join courses on courses.id = student.course_id
where student.gpa >= 3.0
order by student.gpa
limit 50;
How would Mysql execute this query to optimize the cost?
The following index might help:
CREATE INDEX idx ON student (gpa, course_id);
This index would cover the WHERE clause, allowing MySQL to discard any records with a GPA less than 3.0. In addition, it covers the join.
ORDER BY, thereby letting it get to the LIMIT -- hence it will touch only 50 rows (and not do a sort). Table size and FK are not relevant here. More discussion: mysql.rjweb.org/doc.php/index_cookbook_mysql