我有以下MySQL查询
explain select item_id from items use index(user_item_id) where user_id=9 and item_id=10000
以下是返回的
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE items ref user_item_id user_item_id 8 const,const 1 Using index
为什么类型是ref而不是const?
user_item_id是user_id和item_id的组合索引。
High Performance MySql将ref类型查找描述为"这是一个返回与单个值匹配的行的索引访问"
当添加单词const
时,它被描述为"优化掉查询的部分并将其转化为常量"
因此,MySQL似乎首先需要能够从索引中找到行
ref列中的常量意味着mysql可以使用以前的值来查找索引中的内容。