mysql 索引 碎片积累

show index form table_name:

Cardinality 在某列上不重复值的数量,对索引而言该值越大越好

seq_index 代表列在复合索引中的序号

analyze table user_profile; 触发采样

> 5.6 show create table table_name 不触发

创建索引:

  1. 高选择性的字段

  2. 不重复值较多的列

  3. 不带order by 的查询并不是按照主键排序的

  4. 复合索引(a, b) 会对a进行排序,但不会对b进行排序

    索引使用分析:

    select * from xxx where a = ? (yes)
    select * from xxx where a = ? and b = ? (yes)
    select * from xxx where a = ? or b = ? (no)
    分析: or拿到是a 和 b 查询结果的并集,即使a用到了索引,但b仍然要全表扫描,所以并不会先用索引再扫描全表

    select count(1) from buy_log where buy_date >= ‘2011-01-01’ and buy_date < ‘2011-02-01’ (yes)
    这个查询使用到复合索引(userid, buy_date):

    why here using the compound index?
    在索引覆盖的场景下有时候可以用到复合索引,这要求根据b列查找数据能得到想要的数据,例如上例中我们根据时间来统计数量,通过扫描索引就可以得到结果不必走全表扫描了。

explain:

  1. 显示sql语句的执行计划
  2. 5.6版本支持DML语句
  3. 5.6开始支持JSON格式输出

show variables like ‘%not%using%’ 查看是否 开启没有使用索引的sql记录到慢查询日志

desc select statement == explain select statement
format=json 参数会显示cost info 等信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值