SQL 성능 최적화의 목표는 최소한 범위 수준에 도달하는 것입니다. 요구 사항은 참조 수준(바람직하게는 consts 수준)입니다. (Alibaba 개발 매뉴얼에 필요함)
일련번호
목록
설명하다
1.1
체계
테이블에 레코드가 하나만 있고 해당 테이블에서 사용하는 MyISAM, Memory 등 스토리지 엔진의 통계가 정확한 경우
1.2
const
기본 키 또는 고유 인덱스 열과 상수를 기반으로 등가 매칭을 수행할 때 단일 테이블에 대한 액세스 방법은 다음과 같습니다.const
1.3
eq_ref
쿼리 연결 시, Primary Key 또는 Unique Secondary Index 컬럼의 동일값 매칭을 통해 Driven Table에 접근하는 경우(Primary Key 또는 Unique Secondary Index가 Joint Index인 경우 모든 인덱스 컬럼이 동일해야 함) 값 비교) , 구동 테이블에 대한 액세스 방법은 다음과 같습니다.eq ref
1.4
심판
일반 보조 인덱스 컬럼과 상수 간의 동등 매칭을 통해 테이블을 쿼리할 때 테이블에 대한 액세스 방법은 다음과 같습니다.ref
2.1
전문
약간
2.2
ref_or_null
공통 보조 인덱스에 대해 동등한 일치 쿼리를 수행할 때 인덱스 열의 값도 NULL값이 될 수 있으며, 테이블에 대한 액세스 방법은 다음과 같습니다.ref_or_null
2.3
index_merge
단일 테이블 액세스 방법을 사용할 때 일부 시나리오에서는 , 및 의 세 가지 인덱스 병합 방법을 사용하여 Intersection쿼리 Union를 sort-Union실행할 수 있습니다.
2.4
고유_하위 쿼리
IN쿼리 최적화 프로그램이 하위 쿼리를 하위 쿼리 IN로 변환하기 로 결정하고 하위 쿼리가 동등한 일치를 위해 고유 인덱스를 사용할 수 있는 경우 하위 쿼리가 포함된 일부 쿼리 문에 대한 것입니다 .EXISTS
2.5
index_subquery
IN쿼리 최적화 프로그램이 하위 쿼리를 하위 쿼리 IN로 변환하기 로 결정하고 하위 쿼리가 동등한 일치를 위해 일반 인덱스를 사용할 수 있는 경우 하위 쿼리가 포함된 일부 쿼리 문에 대한 것입니다 .EXISTS
2.6
범위
특정 레코드를 얻기 위해 색인을 사용하는 경우 范围区间'범위' 액세스 방법을 사용할 수 있습니다.
3.1
색인
인덱스 커버리지를 사용할 수 있지만 모든 인덱스 레코드를 스캔해야 하는 경우 테이블의 액세스 방법은 다음과 같습니다.index
3.2
모두
인덱스를 사용하지 않는 상황(다양한 인덱스 실패 상황)
1.1.1.SQL 요약
-- 结果值从最好到最坏依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL。SQL 性能优化的目标:至少要达到 range 级别,要求是 ref 级别,最好是 consts级别。(阿里巴巴开发手册要求)-- 1.等值匹配-- 1.1.`system`。当表中只有一条记录并且该表使用的在储引擎的统计数据是精确的,比如MyISAM、Memoryexplainselect*from demo where id ='1';-- 1.2.`const`。当我们根据主键或者唯一索引列与常数进行等值匹配时,对单表的访问方法就是`const`explainselect*from demo where id2 ='1';-- 1.3.`eq_ref`。在连接查询时,如果被驱动表是通过主键或者唯一二级索引列等值匹配的方式进行访问的(如果该主键或者唯一二级索引是联合索引的话,所有的索引列都必须进行等值比较),则对该被驱动表的访问方法就是`eq ref`explainselect*from demo a,demo b where a.id2 = b.id2;-- 1.4.`ref`。当通过普通的二级索引列与常量进行等值匹配时来查询某个表,那么对该表的访问方法就可能是`ref`explainselect*from demo where id3 ='1';explainselect*from demo where id2 isnull;-- 对比上面的1.2.explainselect*from demo a,demo b where a.id3 = b.id3;-- 对比上面的1.3.-- 2.模糊、范围查询等-- 2.1.`fulltext`略-- 2.2.`ref_or_null`。当对普通二级索引进行等值匹配查询,该索引列的值也可以是`NULL`值时,那么对该表的访问方法就可能是`ref_or_null`explainselect*from demo where id2 ='1'or id2 isnull;-- 如果字段有not null约束,则为const(唯一索引时)、ref(普通索引时)explainselect*from demo where id3 ='1'or id3 isnull;-- 如果字段有not null约束,则为const(唯一索引时)、ref(普通索引时)-- 2.3.`index_merge`。单表访问方法时在某些场景下可以使用`Intersection`、`Union`、`sort-Union`这三种索引合并的方式来执行查询explainselect*from demo where id2 ='1'or id3 ='2';explainselect*from demo where id2 ='1'or id3 in('2','3');-- 2.4.`unique_subquery`。是针对在一些包含`IN`子查询的查询语句中,如果查询优化器决定将`IN`子查询转换为`EXISTS`子查询,而且子查询可以使用到唯一索引进行等值匹配的话。explainselect*from demo a where id4 in(select id2 from demo where id4 = a.id4)or id4 ='1';-- 2.5.`index_subquery`。是针对在一些包含`IN`子查询的查询语句中,如果查询优化器决定将`IN`子查询转换为`EXISTS`子查询,而且子查询可以使用到普通索引进行等值匹配的话。explainselect*from demo a where id4 in(select id3 from demo where id4 = a.id4)or id4 ='1';-- 2.6.`range`。如果使用索引获取某些`范围区间`的记录,那么就可能使用到`range '访问方法explainselect*from demo where id in('1','2');explainselect*from demo where id >'1';explainselect*from demo where id between'1'and'2';explainselect*from demo where id like'1%';-- 注意:id为数字类型,索引失效-- 3.全表扫描-- 3.1.`index`。当我们可以使用索引覆盖,但需要扫描全部的索引记录时,该表的访问方法就是`index`explainselectdistinct id3 from demo;-- 如果id3有大量重复数据时,则为rangeexplainselect id from demo;-- 3.2.`ALL`各种未使用索引(索引失效情况)的情况explainselectdistinct id4 from demo;explainselect id4 from demo;explainselect*from demo where id isnotnull;