Mysql

限制是否影響受影響的行?

  • September 2, 2013

我有這張桌子:

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 子句的行數。

引用自:https://dba.stackexchange.com/questions/49079