My sql 性能优化

My sql 性能优化

1.从0到1构建索引系统:
1.1 为什么要使用索引?:加快查找速度
1.2 如何设计索引?:DB数据-->磁盘,
1.查询关键值;2.定位文件名;3.偏移量
1.3 设计索引时用什么数据结构:
mysql:
B+树 数据结构,myssql.com: 搜索引擎:innodb,myisam
hash 数据结构:搜索引擎memory
B树:
二叉树:
AVL树:
红黑树:
聚集索引:索引文件和数据文件放置一起;
非聚集索引:索引文件和数据文件不放置一起;
processon.com:在线画图,思维题,UML,
cs.usfca.edu: 二叉树等各种数据结构
mysql.com -document--example databases zip

hash:
二叉树:都会因为树的深度过深而造成IO次数变多,影响读取效率
B树:
B+树:让磁盘的每个块上能尽可能多的存储数据;
单机:自增
分布式:雪花算法,直接算法

回表:select * from table wher name ="a",2次B+树,若一次次B+树需3次Io
索引覆盖:
select * from table where name="a" 2次B+树
select id from table where name="a" 1次B+树

最左匹配:会使用索引
select id from table where name="a" and age =10
select id from table where name="a"
select id from table where age=10
select id from table where age=10 and name="a"

索引下推:直接拉所有条件的数据(name,age),减少磁盘索引量,提示性能,在mysql5.6以后版本出现的新功能;
索引下推:唯一的缺点是需要在磁盘上多做数赛选,原来的赛选放在内存里,现在放在磁盘
select t1.name,t2.name from t1 join t2 on t1.id=t2.id
1.先做表连接,然后查询需要的字段;
2.先把需要的字段拿出来,然后再做关联,谓词下推(推荐这个,Io少)

FIC: fast index create
插入和删除数据:
1. 先创建临时表,将数据 记写入临时表;
2. 把原始表删除;
3.修改临时表的名字;

FIC:
给当前表添加一个share锁,不会有创建临时表的资源消耗,
还是在原表中修改,此时有人发起DML操作,会有数据不一致,所以加share锁,
读取时没有问题,写操作有问题;



索引匹配方式:
全值匹配: select id from table where name="a" and age =10
匹配最左前缀:select id from table where name="a"
匹配列前缀:select id from table where 'J%'
匹配范围值:select id from table where age>10
select id from table where name='aa' and age>10 and post=20; ==post =20 查询条件失效
select id from table where name='aa' and post=20 and age>10 ; ==post =20 查询条件有效效
精准匹配某一列并范围匹配另一列
索引覆盖:
select id,name from table where name="a" //效率高,不用回表就可以查询到结构
select id,age from table where name="a" //效率低,要回表

hash 索引:
组合索引:
select * from table where a=1 and b =2
select * from table where a=1 or b =2 ==or 不走索引
select * from table where a in (1,2) ==In 不走索引
select * from table where a=1 or a =2 ==or 不走索引
select id,name, from table where a=1 or b =2 ==or 普通列 走索引

建立索引时尽量减少索引字段的数据量

url :CRC32 循环数据校验规则


索引失效:查询条件中有表达式

尽量使用主键查询,不会产生回表
建立索引可以:部分值作为索引,

排序为什么比较费时间?
排序有:文件排序,组合索引排序
推荐使用索引来排序;

1.如果是单例索引,or 会使用索引
如何是组合索引,全部列都是组合索引,会使用全部列对应的索引;

2.如何部分列是组合索引,则不会走索引。

隐式类型转换会导致索引失效;
DV:typelog

当进行多表查询时,最后不要超过3个,需要join的字段,数据类型必须一致,隐式类型转换会导致索引失效

大数据量查询:用 unionall ,子查询
能使用limit时尽量使用limit
单表索引控制在5个以内;
单索引字段数不超过5个(组合索引)


1.4 my sql 是如何实现的

1.5.面试常问的问题:

多线程,高并发,(锁,AQS)
数据结构
设计模式6大原则
JVM
数据库
REDIS
消息中间件
zookeeper
linux内核

原文地址:https://www.cnblogs.com/csj007523/p/13587017.html