mysql explain 유형 연결 유형 예
MySQL 실행 계획을 얻으려면 explain 메소드를 통해 볼 수 있습니다. explain 메소드는 간단해 보이지만 실제로는 많은 내용이 포함되어 있습니다. 특히 출력 결과를 입력합니다. 이러한 다양한 유형을 이해하는 것은 SQL 최적화에 매우 중요합니다. 이 기사에서는 explian 출력 결과의 유형 열만 설명하고 이에 대한 데모를 제공합니다.
explian 출력에 대한 전체 설명은 MySQL EXPLAIN SQL 출력 정보 설명을 참조하세요.
1 EXPLAIN 문의 유형 열 값
type: 连接类型 system 表只有一行 const 表最多只有一行匹配,通用用于主键或者唯一索引比较时 eq_ref 每次与之前的表合并行都只在该表读取一行,这是除了system,const之外最好的一种, 特点是使用=,而且索引的所有部分都参与join且索引是主键或非空唯一键的索引 ref 如果每次只匹配少数行,那就是比较好的一种,使用=或<=>,可以是左覆盖索引或非主键或非唯一键 fulltext 全文搜索 ref_or_null 与ref类似,但包括NULL index_merge 表示出现了索引合并优化(包括交集,并集以及交集之间的并集),但不包括跨表和全文索引。 这个比较复杂,目前的理解是合并单表的范围索引扫描(如果成本估算比普通的range要更优的话) unique_subquery 在in子查询中,就是value in (select...)把形如“select unique_key_column”的子查询替换。 PS:所以不一定in子句中使用子查询就是低效的! index_subquery 同上,但把形如”select non_unique_key_column“的子查询替换 range 常数值的范围 index a.当查询是索引覆盖的,即所有数据均可从索引树获取的时候(Extra中有Using Index); b.以索引顺序从索引中查找数据行的全表扫描(无 Using Index); c.如果Extra中Using Index与Using Where同时出现的话,则是利用索引查找键值的意思; d.如单独出现,则是用读索引来代替读行,但不用于查找 all 全表扫描
2. 연결 유형 부분 예시
1、all-- 环境描述 (root@localhost) [sakila]> show variables like 'version'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | version | 5.6.26 | +---------------+--------+ MySQL采取全表遍历的方式来返回数据行,等同于Oracle的full table scan (root@localhost) [sakila]> explain select count(description) from film; +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | film | ALL | NULL | NULL | NULL | NULL | 1000 | NULL | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ 2、index MySQL采取索引全扫描的方式来返回数据行,等同于Oracle的full index scan (root@localhost) [sakila]> explain select title from film \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: film type: indexpossible_keys: NULL key: idx_title key_len: 767 ref: NULL rows: 1000 Extra: Using index1 row in set (0.00 sec) 3、 range 索引范围扫描,对索引的扫描开始于某一点,返回匹配值域的行,常见于between、<、>等的查询 等同于Oracle的index range scan (root@localhost) [sakila]> explain select * from payment where customer_id>300 and customer_id<400\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: payment type: rangepossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: NULL rows: 2637 Extra: Using where1 row in set (0.00 sec) (root@localhost) [sakila]> explain select * from payment where customer_id in (200,300,400)\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: payment type: rangepossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: NULL rows: 86 Extra: Using index condition1 row in set (0.00 sec) 4、ref 非唯一性索引扫描或者,返回匹配某个单独值的所有行。常见于使用非唯一索引即唯一索引的非唯一前缀进行的查找 (root@localhost) [sakila]> explain select * from payment where customer_id=305\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: payment type: refpossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: const rows: 25 Extra: 1 row in set (0.00 sec) idx_fk_customer_id为表payment上的外键索引,且存在多个不不唯一的值,如下查询 (root@localhost) [sakila]> select customer_id,count(*) from payment group by customer_id -> limit 2; +-------------+----------+ | customer_id | count(*) |+-------------+----------+ | 1 | 32 || 2 | 27 | +-------------+----------+-- 下面是非唯一前缀索引使用ref的示例 (root@localhost) [sakila]> create index idx_fisrt_last_name on customer(first_name,last_name); Query OK, 599 rows affected (0.09 sec) Records: 599 Duplicates: 0 Warnings: 0(root@localhost) [sakila]> select first_name,count(*) from customer group by first_name -> having count(*)>1 limit 2; +------------+----------+| first_name | count(*) | +------------+----------+| JAMIE | 2 || JESSIE | 2 | +------------+----------+2 rows in set (0.00 sec) (root@localhost) [sakila]> explain select first_name from customer where first_name='JESSIE'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: customer type: refpossible_keys: idx_fisrt_last_name key: idx_fisrt_last_name key_len: 137 ref: const rows: 2 Extra: Using where; Using index1 row in set (0.00 sec) (root@localhost) [sakila]> alter table customer drop index idx_fisrt_last_name; Query OK, 599 rows affected (0.03 sec) Records: 599 Duplicates: 0 Warnings: 0--下面演示出现在join是ref的示例 (root@localhost) [sakila]> explain select b.*,a.* from payment a inner join -> customer b on a.customer_id=b.customer_id\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: b type: ALLpossible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 599 Extra: NULL *************************** 2. row *************************** id: 1 select_type: SIMPLE table: a type: refpossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: sakila.b.customer_id rows: 13 Extra: NULL2 rows in set (0.01 sec) 5、eq_ref 类似于ref,其差别在于使用的索引为唯一索引,对于每个索引键值,表中只有一条记录与之匹配。 多见于主键扫描或者索引唯一扫描。 (root@localhost) [sakila]> explain select * from film a join film_text b -> on a.film_id=b.film_id; +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ | 1 | SIMPLE | b | ALL | PRIMARY | NULL | NULL | NULL | 1000 | NULL | | 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 2 | sakila.b.film_id | 1 | Using where | +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ (root@localhost) [sakila]> explain select title from film where film_id=5; +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+| 1 | SIMPLE | film | const | PRIMAR | PRIMARY | 2 | const | 1 | NULL | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+6、const、system: 当MySQL对查询某部分进行优化,这个匹配的行的其他列值可以转换为一个常量来处理。 如将主键或者唯一索引置于where列表中,MySQL就能将该查询转换为一个常量 (root@localhost) [sakila]> create table t1(id int,ename varchar(20) unique); Query OK, 0 rows affected (0.05 sec) (root@localhost) [sakila]> insert into t1 values(1,'robin'),(2,'jack'),(3,'henry'); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 (root@localhost) [sakila]> explain select * from (select * from t1 where ename='robin')x; +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ | 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL | | 2 | DERIVED | t1 | const | ename | ename | 23 | const | 1 | NULL | +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ 2 rows in set (0.00 sec) 7、type=NULL MySQL不用访问表或者索引就可以直接得到结果 (root@localhost) [sakila]> explain select sysdate();+----+-------------+-------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec)
MySQL 실행 계획을 얻으려면 explain 메소드를 통해 볼 수 있지만, 설명 방법은 간단합니다. 특히 출력 결과의 유형 열에는 실제로 많은 내용이 포함되어 있습니다. 이러한 다양한 유형을 이해하는 것은 SQL 최적화에 매우 중요합니다. 이 기사에서는 explian 출력 결과의 유형 열만 설명하고 이에 대한 데모를 제공합니다.
explian 출력에 대한 전체 설명은 MySQL EXPLAIN SQL 출력 정보 설명을 참조하세요.
1 EXPLAIN 문의 유형 열 값
type: 连接类型 system 表只有一行 const 表最多只有一行匹配,通用用于主键或者唯一索引比较时 eq_ref 每次与之前的表合并行都只在该表读取一行,这是除了system,const之外最好的一种, 特点是使用=,而且索引的所有部分都参与join且索引是主键或非空唯一键的索引 ref 如果每次只匹配少数行,那就是比较好的一种,使用=或<=>,可以是左覆盖索引或非主键或非唯一键 fulltext 全文搜索 ref_or_null 与ref类似,但包括NULL index_merge 表示出现了索引合并优化(包括交集,并集以及交集之间的并集),但不包括跨表和全文索引。 这个比较复杂,目前的理解是合并单表的范围索引扫描(如果成本估算比普通的range要更优的话) unique_subquery 在in子查询中,就是value in (select...)把形如“select unique_key_column”的子查询替换。 PS:所以不一定in子句中使用子查询就是低效的! index_subquery 同上,但把形如”select non_unique_key_column“的子查询替换 range 常数值的范围 index a.当查询是索引覆盖的,即所有数据均可从索引树获取的时候(Extra中有Using Index); b.以索引顺序从索引中查找数据行的全表扫描(无 Using Index); c.如果Extra中Using Index与Using Where同时出现的话,则是利用索引查找键值的意思; d.如单独出现,则是用读索引来代替读行,但不用于查找 all 全表扫描
2. 연결 유형 부분 예시
1、all-- 环境描述 (root@localhost) [sakila]> show variables like 'version'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | version | 5.6.26 | +---------------+--------+MySQL采取全表遍历的方式来返回数据行,等同于Oracle的full table scan (root@localhost) [sakila]> explain select count(description) from film; +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | film | ALL | NULL | NULL | NULL | NULL | 1000 | NULL | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ 2、index MySQL采取索引全扫描的方式来返回数据行,等同于Oracle的full index scan (root@localhost) [sakila]> explain select title from film \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: film type: indexpossible_keys: NULL key: idx_title key_len: 767 ref: NULL rows: 1000 Extra: Using index1 row in set (0.00 sec) 3、 range 索引范围扫描,对索引的扫描开始于某一点,返回匹配值域的行,常见于between、<、>等的查询 等同于Oracle的index range scan (root@localhost) [sakila]> explain select * from payment where customer_id>300 and customer_id<400\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: payment type: rangepossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: NULL rows: 2637 Extra: Using where1 row in set (0.00 sec) (root@localhost) [sakila]> explain select * from payment where customer_id in (200,300,400)\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: payment type: rangepossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: NULL rows: 86 Extra: Using index condition1 row in set (0.00 sec) 4、ref 非唯一性索引扫描或者,返回匹配某个单独值的所有行。常见于使用非唯一索引即唯一索引的非唯一前缀进行的查找 (root@localhost) [sakila]> explain select * from payment where customer_id=305\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: payment type: refpossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: const rows: 25 Extra: 1 row in set (0.00 sec) idx_fk_customer_id为表payment上的外键索引,且存在多个不不唯一的值,如下查询 (root@localhost) [sakila]> select customer_id,count(*) from payment group by customer_id -> limit 2; +-------------+----------+ | customer_id | count(*) |+-------------+----------+ | 1 | 32 || 2 | 27 | +-------------+----------+-- 下面是非唯一前缀索引使用ref的示例 (root@localhost) [sakila]> create index idx_fisrt_last_name on customer(first_name,last_name); Query OK, 599 rows affected (0.09 sec) Records: 599 Duplicates: 0 Warnings: 0(root@localhost) [sakila]> select first_name,count(*) from customer group by first_name -> having count(*)>1 limit 2; +------------+----------+| first_name | count(*) | +------------+----------+| JAMIE | 2 || JESSIE | 2 | +------------+----------+2 rows in set (0.00 sec) (root@localhost) [sakila]> explain select first_name from customer where first_name='JESSIE'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: customer type: refpossible_keys: idx_fisrt_last_name key: idx_fisrt_last_name key_len: 137 ref: const rows: 2 Extra: Using where; Using index1 row in set (0.00 sec) (root@localhost) [sakila]> alter table customer drop index idx_fisrt_last_name; Query OK, 599 rows affected (0.03 sec) Records: 599 Duplicates: 0 Warnings: 0--下面演示出现在join是ref的示例 (root@localhost) [sakila]> explain select b.*,a.* from payment a inner join -> customer b on a.customer_id=b.customer_id\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: b type: ALLpossible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 599 Extra: NULL *************************** 2. row *************************** id: 1 select_type: SIMPLE table: a type: refpossible_keys: idx_fk_customer_id key: idx_fk_customer_id key_len: 2 ref: sakila.b.customer_id rows: 13 Extra: NULL2 rows in set (0.01 sec) 5、eq_ref 类似于ref,其差别在于使用的索引为唯一索引,对于每个索引键值,表中只有一条记录与之匹配。 多见于主键扫描或者索引唯一扫描。 (root@localhost) [sakila]> explain select * from film a join film_text b -> on a.film_id=b.film_id; +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ | 1 | SIMPLE | b | ALL | PRIMARY | NULL | NULL | NULL | 1000 | NULL | | 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 2 | sakila.b.film_id | 1 | Using where | +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ (root@localhost) [sakila]> explain select title from film where film_id=5; +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | film | const | PRIMARY | PRIMARY | 2 | const | 1 | NULL | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ 6、const、system: 当MySQL对查询某部分进行优化,这个匹配的行的其他列值可以转换为一个常量来处理。 如将主键或者唯一索引置于where列表中,MySQL就能将该查询转换为一个常量 (root@localhost) [sakila]> create table t1(id int,ename varchar(20) unique); Query OK, 0 rows affected (0.05 sec) (root@localhost) [sakila]> insert into t1 values(1,'robin'),(2,'jack'),(3,'henry'); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 (root@localhost) [sakila]> explain select * from (select * from t1 where ename='robin')x; +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL || 2 | DERIVED | t1 | const | ename | ename | 2 3 | const | 1 | NULL | +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ 2 rows in set (0.00 sec) 7、type=NULL MySQL不用访问表或者索引就可以直接得到结果 (root@localhost) [sakila]> explain select sysdate(); +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec)
위 내용은 mysql 설명 유형 연결 유형 예시 내용이며, 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템입니다. 1) 데이터베이스 및 테이블 작성 : CreateAbase 및 CreateTable 명령을 사용하십시오. 2) 기본 작업 : 삽입, 업데이트, 삭제 및 선택. 3) 고급 운영 : 가입, 하위 쿼리 및 거래 처리. 4) 디버깅 기술 : 확인, 데이터 유형 및 권한을 확인하십시오. 5) 최적화 제안 : 인덱스 사용, 선택을 피하고 거래를 사용하십시오.

다음 단계를 통해 phpmyadmin을 열 수 있습니다. 1. 웹 사이트 제어판에 로그인; 2. phpmyadmin 아이콘을 찾고 클릭하십시오. 3. MySQL 자격 증명을 입력하십시오. 4. "로그인"을 클릭하십시오.

Navicat Premium을 사용하여 데이터베이스 생성 : 데이터베이스 서버에 연결하고 연결 매개 변수를 입력하십시오. 서버를 마우스 오른쪽 버튼으로 클릭하고 데이터베이스 생성을 선택하십시오. 새 데이터베이스의 이름과 지정된 문자 세트 및 Collation의 이름을 입력하십시오. 새 데이터베이스에 연결하고 객체 브라우저에서 테이블을 만듭니다. 테이블을 마우스 오른쪽 버튼으로 클릭하고 데이터 삽입을 선택하여 데이터를 삽입하십시오.

응용 프로그램을 열고 새로운 연결 (Ctrl n)을 선택하여 Navicat에서 새로운 MySQL 연결을 만들 수 있습니다. "MySQL"을 연결 유형으로 선택하십시오. 호스트 이름/IP 주소, 포트, 사용자 이름 및 비밀번호를 입력하십시오. (선택 사항) 고급 옵션을 구성합니다. 연결을 저장하고 연결 이름을 입력하십시오.

MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템으로, 주로 데이터를 신속하고 안정적으로 저장하고 검색하는 데 사용됩니다. 작업 원칙에는 클라이언트 요청, 쿼리 해상도, 쿼리 실행 및 반환 결과가 포함됩니다. 사용의 예로는 테이블 작성, 데이터 삽입 및 쿼리 및 조인 작업과 같은 고급 기능이 포함됩니다. 일반적인 오류에는 SQL 구문, 데이터 유형 및 권한이 포함되며 최적화 제안에는 인덱스 사용, 최적화 된 쿼리 및 테이블 분할이 포함됩니다.

MySQL 및 SQL은 개발자에게 필수적인 기술입니다. 1.MySQL은 오픈 소스 관계형 데이터베이스 관리 시스템이며 SQL은 데이터베이스를 관리하고 작동하는 데 사용되는 표준 언어입니다. 2.MYSQL은 효율적인 데이터 저장 및 검색 기능을 통해 여러 스토리지 엔진을 지원하며 SQL은 간단한 문을 통해 복잡한 데이터 작업을 완료합니다. 3. 사용의 예에는 기본 쿼리 및 조건 별 필터링 및 정렬과 같은 고급 쿼리가 포함됩니다. 4. 일반적인 오류에는 구문 오류 및 성능 문제가 포함되며 SQL 문을 확인하고 설명 명령을 사용하여 최적화 할 수 있습니다. 5. 성능 최적화 기술에는 인덱스 사용, 전체 테이블 스캔 피하기, 조인 작업 최적화 및 코드 가독성 향상이 포함됩니다.

Redis는 단일 스레드 아키텍처를 사용하여 고성능, 단순성 및 일관성을 제공합니다. 동시성을 향상시키기 위해 I/O 멀티플렉싱, 이벤트 루프, 비 블로킹 I/O 및 공유 메모리를 사용하지만 동시성 제한 제한, 단일 고장 지점 및 쓰기 집약적 인 워크로드에 부적합한 제한이 있습니다.

백업 또는 트랜잭션 롤백 메커니즘이없는 한 데이터베이스에서 직접 삭제 된 행 복구는 일반적으로 불가능합니다. 키 포인트 : 거래 롤백 : 트랜잭션이 데이터를 복구하기 전에 롤백을 실행합니다. 백업 : 데이터베이스의 일반 백업을 사용하여 데이터를 신속하게 복원 할 수 있습니다. 데이터베이스 스냅 샷 : 데이터베이스의 읽기 전용 사본을 작성하고 데이터를 실수로 삭제 한 후 데이터를 복원 할 수 있습니다. 주의해서 삭제 명령문을 사용하십시오. 실수로 데이터를 삭제하지 않도록 조건을주의 깊게 점검하십시오. WHERE 절을 사용하십시오 : 삭제할 데이터를 명시 적으로 지정하십시오. 테스트 환경 사용 : 삭제 작업을 수행하기 전에 테스트하십시오.
