MySQL 인덱스 실효 몇 가지 상황 실전
| idnameageindex | CREATE TABLE `idnameageindex` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`),
KEY `idx_age` (`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------------+--------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------+
1 row in set (0.00 sec)
mysql> select * from idnameageindex where name='cc';
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | cc | 3 |
+----+------+------+
1 row in set (0.00 sec)
mysql> explain select * from idnameageindex where name='cc';
+----+-------------+----------------+------+---------------+----------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+----------+---------+-------+------+-----------------------+
| 1 | SIMPLE | idnameageindex | ref | idx_name | idx_name | 768 | const | 1 | Using index condition |
+----+-------------+----------------+------+---------------+----------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
mysql> explain select * from idnameageindex where name='cc' or age=2;
+----+-------------+----------------+------+------------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+------------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idnameageindex | ALL | idx_name,idx_age | NULL | NULL | NULL | 3 | Using where |
+----+-------------+----------------+------+------------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from idnameageindex where name='cc' or name='aa';
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idnameageindex | ALL | idx_name | NULL | NULL | NULL | 3 | Using where |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
2 like 조 회 는%로 시작 합 니 다.
mysql> explain select * from idnameageindex where name like '%c';
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idnameageindex | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from idnameageindex where name like 'c%';
+----+-------------+----------------+-------+---------------+----------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+---------------+----------+---------+------+------+-----------------------+
| 1 | SIMPLE | idnameageindex | range | idx_name | idx_name | 768 | NULL | 1 | Using index condition |
+----+-------------+----------------+-------+---------------+----------+---------+------+------+-----------------------+
우리 의 like 조건 이%로 끝 날 때 이 열의 색인 이 발 휘 된 것 을 볼 수 있다.
3.다 열 색인 에 대해 서 는 첫 번 째 부분 이 아 닌 색인 을 사용 하지 않 습 니 다.
mysql> explain select * from idnameageindex where province='hn';
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idnameageindex | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from idnameageindex where address='pd';
+----+-------------+----------------+------+---------------+--------------+---------+-------+------+--------------------
---+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
|
+----+-------------+----------------+------+---------------+--------------+---------+-------+------+--------------------
---+
| 1 | SIMPLE | idnameageindex | ref | idx_location | idx_location | 768 | const | 1 | Using index conditi
on |
+----+-------------+----------------+------+---------------+--------------+---------+-------+------+--------------------
---+
1 row in set (0.00 sec)
연합 색인 이 아 닌 첫 번 째 열 을 검색 조건 으로 사용 할 때 만 색인 을 사용 하지 않 은 것 을 볼 수 있다.4 열 형식 이 문자열 이 라면 데 이 터 를 따옴표 로 참조 해 야 합 니 다.그렇지 않 으 면 색인 을 사용 하지 않 습 니 다.
mysql> explain select * from idnameageindex where name=12;
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idnameageindex | ALL | idx_name | NULL | NULL | NULL | 4 | Using where |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from idnameageindex where name='12';
+----+-------------+----------------+------+---------------+----------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+----------+---------+-------+------+-----------------------+
| 1 | SIMPLE | idnameageindex | ref | idx_name | idx_name | 768 | const | 1 | Using index condition |
+----+-------------+----------------+------+---------------+----------+---------+-------+------+-----------------------+
=뒤에 문자열 이 있 으 면 따옴표 가 붙 지 않 으 면 sql 이 잘못 보고 되 지만 숫자 에는 문제 가 없습니다.5 함수 의 사용 으로 색인 이 실 효 됩 니 다.
mysql> explain select * from idnameageindex where round(id)=1;
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | idnameageindex | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
+----+-------------+----------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select * from idnameageindex where id=1;
+----+-------------+----------------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | idnameageindex | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
+----+-------------+----------------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
6.조회 표 의 효율 은 응용 색인 조회 보다 빠르다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 프로그래밍 시리즈 시작....오늘은 자바 시리즈를 시작하려고 합니다. 이 시리즈에서는 Java 프로그래밍 언어의 기본을 기술하려고 합니다. 이 시리즈는 초보자를 위한 것입니다. Java는 JVM에서 실행되는 고급 프로그래밍 언어입니다. Java...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.