三.索引优化

一.如何选择合适的列建立索引

1.在where从句,group by从句,order by从句,on从句中出现的列.

2.在索引字段越小越好.

3.离散度大的列放到联合索引的前面

判断离散度
查看列唯一值的数量,数量越大,离散度越大.

select count(distinct customer_id),count(distinct staff_id) from payment;

二.索引优化SQL的方法

1.重复索引

是指相同的列以相同的顺序建立的同类型索引,如下表中的primary key和ID列上的索引就是重复索引.

create table test(
id int not null primary key,
name varchar(10) not null,
title varchar(50) not null,
unique(id)
)engine=innodb;

2.查找重复索引

可以使用工具pt-duplicate-key-checker

use information_schema
select a.table_schema as '数据表'
    ,a.table_name as '表名'
    ,a.index_name as '索引1'
    ,b.index_name as '索引2'
    ,a.column_name as '重复列名'
from statistics a join statistics b on
a.table_schema=b.table_schema 
and a.table_name=b.table_name 
and a.seq_in_index=b.seq_in_index
and a.column_name=b.column_name
where a.seq_in_index=1 and a.index_name<>b.index_name;

这里写图片描述

三.删除不使用的索引

perconMySQLMariaDB中可以通过index_staatistics表来查看哪些索引未使用
MySQL中目前只能通过慢查询日志配合pt-index-usage工具来进行索引使用情况的分析.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值