一些数据库优化 2022-05-02

2022-05-05  本文已影响0人  9_SooHyun

字段设计

- 优先选择符合存储需求的最小数据类型

字段越大,建索引需要的空间就越大,一页存储下的索引结点就越少,这样一次索引查找的io次数就会上升,导致索引性能变差

- 尽可能把所有列定义为not null

索引设计

索引的目的是,在磁盘io去提取数据之前,尽可能多地过滤不必要的数据,缩小磁盘io的“范围”

- 联合索引。一般区分度高的、使用最频繁的字段,放到联合索引的最左侧

- 前缀索引。索引列的字符串过长时,全部拿来建索引那么索引文件就会偏大,磁盘io上升。这时可以选择使用前n位建索引

存储引擎

- innodb使用B+tree索引

查询优化

- 如可以,尽量使用left join or not exists替换not innot in通常不走索引

- 一条sql语句,where和order by都可以使用索引

- index_merge. where子句中可能有多个条件(或者join)涉及到一张表的多个字段,它们之间进行 AND 或者 OR,就有可能会使用到 index merge 。index merge 其实就是对一张表的多个索引分别进行条件扫描,然后将它们各自的结果进行合并(intersect/union)

The Index Merge access method retrieves rows with multiple range scans and merges their results into one. This access method merges index scans from a single table only, not scans across multiple tables. The merge can produce unions, intersections, or unions-of-intersections of its underlying scans

- 推荐使用union替代or

- IN clause.

expr IN (value,...)
optimization of IN expr:

上一篇下一篇

猜你喜欢

热点阅读