log from mysql-slow-log:
Query_time: 11.632750 Lock_time: 1.317329 Rows_sent: 851 Rows_examined: 1649439
select record.name, dept.name, school.name FROM record
inner join record as r2 ON
record.number=r2.number AND record.year=r2.year
AND r2.dept_id=12345 AND r2.type=1 AND r2.year=2015
LEFT JOIN school ON record.school_id=school.id
LEFT JOIN dept ON record.dept_id=dept.id
There are about 0.8 million rows in table record
but how come the query examine more than 1 million rows?
I've set the index for table dept
and table school
, but I'm not sure about how to create index for the inner join part. I read a previous question(Rows_sent: 12 Rows_examined: 549024 - how to optimize mySQL query?) but still can't get the solution to my extremely slow query.
Edited (why inner join):
I would like to select all rows from same persons having dept_id=12345. The result would be the yellow and orange rows as in the example picture. That's why I use inner join.