美文网首页
MySql 索引理解及测试

MySql 索引理解及测试

作者: Messix_1102 | 来源:发表于2020-03-05 15:09 被阅读0次

    准备

    1.创建如下表结构

    表结构图

    2.添加百万条测试数据,注意数据的分散性

    测试数据截图

    测试

    1.创建 account 单列索引

    CREATE INDEX account_index ON user_info(account);
    

    1.1 首先测试哪些操作会用到索引

    • ORDER BY
    select * from user_info order by account limit 10;
    
    排序使用索引测试
    • WHERE
    select * from user_info where account = '10701445';
    
    查询使用索引测试
    • 结论
      ORDER BY和WHERE操作都会使用到索引,且数据越分散作用越大。

    1.2 如何使用索引

    • ORDER BY 左前规则
    select * from user_info order by account limit 10; (使用索引)
    select * from user_info order by age, account limit 10; (不使用索引)
    
    • WHERE 只要条件中有该索引字段则会引用
    select * from user_info where age > 25 and account = '15182615'; (使用索引)
    select * from user_info where account = '15182615'; (使用索引)
    

    2.创建 组合索引 account, email

    create index account_email_index on user_info(account, email);
    

    2.1 同单列索引一样,ORDER BY 和 SELECT 会使用到组合索引

    2.2 如何使用组合索引

    • ORDER BY 左前规则
    select * from user_info order by account, email limit 10; (使用索引)
    select * from user_info order by account limit 10; (使用索引)
    select * from user_info order by email limit 10; (不使用索引)
    
    • WHERE 左前规则
    select * from user_info where name = '是稍' and account = '15182615'; (使用索引)
    select * from user_info where name = '是稍' and email = '2x63dC7y@yahoo.com'; (不使用索引)
    

    相关文章

      网友评论

          本文标题:MySql 索引理解及测试

          本文链接:https://www.haomeiwen.com/subject/ypzxrhtx.html