【问题标题】:Shouldn't two consts in mysql for ref be a type const?mysql for ref 中的两个 const 不应该是 const 类型吗?
【发布时间】:2012-08-22 12:30:13
【问题描述】:

我有以下 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 indexing constants ref explain


    【解决方案1】:

    高性能 MySql 将 ref 类型查找描述为“这是一个返回匹配单个值的行的索引访问”

    当添加单词const 时,它被描述为“优化掉部分查询并将其转换为常量”

    所以看来MySQL首先需要能够从索引中找到行

    ref 列中的 const 意味着 mysql 可以使用以前的值来查找索引中的内容。

    【讨论】:

    • 查询必须访问一些数据 - ref 表示数据与单个值匹配 - 它不是 const 的原因是 mysql 无法优化它 - 它必须访问一行 -进而将 type 设置为 ref 而不是 const
    • 我应该从EXPLAIN 中添加您的查询看起来非常有效。
    猜你喜欢
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2019-05-12
    • 2022-11-27
    相关资源
    最近更新 更多