I am running two queries with same query but different parameter and one of them is extemely slow while the other is fast. Both the results are alomost the same. (500 and 575) Any idea why it skips "attended" index in the second query ? If I FORCE INDEX, it works very fast !
MYSQL 5.0.77-log Both InnoDB tables
GOOD
SELECT count(*) as total_students FROM students s,subjects b WHERE s.subjectid = b.subjectid AND s.attended>'2012-05-22 00:00:00' AND su.classid=47;
+----+-------------+-------+--------+------------------------+---------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+------------------------+---------+---------+---------------------+------+-------------+
| 1 | SIMPLE | s | range | subjectstatus,attended | attended | 8 | NULL | 5000 | Using where |
| 1 | SIMPLE | b | eq_ref | PRIMARY,classid | PRIMARY | 4 | s.subjectid | 1 | Using where |
+----+-------------+-------+--------+------------------------+---------+---------+---------------------+------+-------------+
BAD
SELECT count(*) as total_students FROM students s,subjects b WHERE s.subjectid = b.subjectid AND s.attended>'2012-05-22 00:00:00' AND su.classid=43;
+----+-------------+-------+------+------------------------+----------------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+------------------------+----------------+---------+---------------------+------+-------------+
| 1 | SIMPLE | b | ref | PRIMARY,classid | classid | 4 | const | 86 | Using index |
| 1 | SIMPLE | s | ref | subjectstatus,attended | subjectstatus | 4 | b.subjectid | 198 | Using where |
+----+-------------+-------+------+------------------------+----------------+---------+---------------------+------+-------------+
su
? I don't see it referenced in thefrom
but that alias is in thewhere
. How many rows whereclassid = 43
vs.47
? Perhaps it's a cardinality issue? What happens when you use properINNER JOIN
syntax instead of old-styleFROM a,b WHERE
?