I have a table with 25 million rows indexed accordingly.
But adding the AND status IS NULL clause turns a super fast query into a crazy slow query.
Please help me speed it up.
Query:
SELECT student_id, grade, status FROM grades WHERE class_id = 1 AND status IS NULL
Table:
CREATE TABLE IF NOT EXISTS `grades` ( `student_id` BIGINT(20) NOT NULL, `class_id` INT(11) NOT NULL, `grade` FLOAT(10,6) DEFAULT NULL, `status` INT(11) DEFAULT NULL, UNIQUE KEY `unique_key` (`student_id`,`class_id`), KEY `class_id` (`class_id`), KEY `status` (`status`), KEY `grade` (`grade`) ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Local development shows results instantly (<200 ms). The production server is a huge decline (40-70 seconds!).
Can you point me in the right direction for debugging?
I explain:
+----+-------------+--------+-------------+-----------------------+-----------------+---------+------+-------+--------------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------------+-----------------------+-----------------+---------+------+-------+--------------------------------------------------------+ | 1 | SIMPLE | grades | index_merge | class_id,status,grade | status,class_id | 5,4 | NULL | 26811 | Using intersect(status,class_id); Using where | +----+-------------+--------+-------------+-----------------------+-----------------+---------+------+-------+--------------------------------------------------------+
optimization null sql mysql query-optimization
Ryan
source share