在我们的MySQL 5.5数据库中,我们有以下InnoDB表,其中包含30M +行:
+----------------+-------------+-----------------+--------+
| participant_id | question_id | given_answer_id | status |
+----------------+-------------+-----------------+--------+
| 500 | 12 | 25 | 0 |
+----------------+-------------+-----------------+--------+
参与者participant_id + question_id + given_answer_id是唯一的。
目前我们有关键字:
PRIMARY KEY(按此顺序)
INDEX KEY
对于此表,我们的应用程序中有两种选择查询:
[...] WHERE participant_id = x AND question_id = y AND given_answer_id = z;
和
[...] WHERE question_id = x;
通常,每个participant_id都在0到<100行之间,具有不同的question_id。反过来说,每个question_id可以有不同的(通常不超过10万)行与不同的participant_id。第一个查询的执行方式比第二个查询更频繁。
当我们执行以下查询时,会向我们显示 32096行所在的位置:
EXPLAIN SELECT * FROM example WHERE question_id = 500;
+----+-------------+-----------+------+---------------+-------------+---------+-------+-------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+-------------+---------+-------+-------+-------+
| 1 | SIMPLE | example | ref | question_id | question_id | 8 | const | 32096 | |
+----+-------------+-----------+------+---------------+-------------+---------+-------+-------+-------+
然而,当我们执行相同的查询没有EXPLAIN 时,只返回 18732行。
我们需要在此表上使用哪些索引来防止这种开销但仍然可以执行这两种查询?
以下是创建此表的代码:
CREATE TABLE `example` ( `participant_id` BIGINT(20) UNSIGNED NOT NULL, `question_id` BIGINT(20) UNSIGNED NOT NULL, `given_answer_id` BIGINT(20) UNSIGNED NOT NULL, `status` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0', PRIMARY KEY (`participant_id`, `question_id`, `given_answer_id`), INDEX `question_id` (`question_id`) ) ENGINE=InnoDB;
答案 0 :(得分:0)
我已按照建议将此问题发布到dba.stackexchange.com,并收到了全面的答案:https://dba.stackexchange.com/questions/49275/mysql-looking-up-more-rows-than-needed-indexing-issue