MySQL 인덱스 실효 몇 가지 상황 실전

11016 단어 learnonwork
1 조건 중 사용 or
| 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.조회 표 의 효율 은 응용 색인 조회 보다 빠르다.

좋은 웹페이지 즐겨찾기