zoukankan      html  css  js  c++  java
  • 索引的另一个好处及其运用实例介绍

    索引用来加速查询或者是件家喻户晓的事,但还有一优势是:降低排序成本
    下面用2个例子来分析


    运用实例

    例1:优化排序分页

    原语句:
    select * from t order by col limit 100000,10;
    改进后语句:
    select * from t where col >= (select col from t order by col limit 100000,1) limit 10;
    改进后的好处:
    1)无需排序(排序只在非聚簇索引col上进行)
    2)无需回表(利用覆盖索引即可回答查询)
     
    例2:消除filesort

    什么是filesort?说白了,就是排序没有走索引的都算filesort,无论是在内存或磁盘
    explain打印出执行计划的时候会看到using filesort 这种字眼
    原语句
    select * from t where col1> number order by col2 desc;
    复合索引:(col1,col2)
    改进后语句
    select * from t where col2 > min_value and col1 > number order by col2 desc;
    复合索引:(col2,col1)
    改进后的分析:
    col2>min_value是出于复合索引前缀性而不能少的无效条件

    这也给我们一个优化的思路,即:能否借助索引来抵消排序

     

    By 迦叶

    2013-12-12

    Good Luck

  • 相关阅读:
    python连接redis
    python3进行md5加密
    python操作mysql数据库
    python3操作excle
    memcache与redis的存储类型
    模块
    函数
    json与字典相互转换
    常用的Random函数
    字符串常用方法
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3471340.html
Copyright © 2011-2022 走看看