我有这张桌子:
CREATE TABLE IF NOT EXISTS `usergroups` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`group_id` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`group_id`,`user_id`),
KEY `id` (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5496 ;
运行此查询时:
EXPLAIN SELECT `UserGroup`.`user_id` FROM `usergroups` AS `UserGroup` WHERE `UserGroup`.`group_id` = 1 limit 30
它的输出是:
1 SIMPLE UserGroup ref PRIMARY PRIMARY 2 const 543 Using index
我觉得不对劲!因为它影响了 543 行,但我认为它最多应该影响 30 行。是真的吗?
限制确实会影响选择/影响的行数。543 只是与连接和 where 子句匹配的行数。