我有以下 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 的复合索引。
最佳答案
高性能 MySql 将 ref 类型查找描述为“这是一个返回与单个值匹配的行的索引访问”
当添加单词 const
时,它被描述为“优化部分查询并将其变成常量”
所以看来MySQL首先需要能够从索引中找到行
ref 列中的常量意味着 mysql 可以使用以前的值在索引中查找内容。
关于mysql - mysql 中 ref 的两个常量不应该是一个类型常量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12151805/