MySQL - ìndices
Autor: Renato Kolbe | Publicado: 2016-03-10T11:58:00-03:00

O uso de índices no MySQL é fantástico, mas vale aqui algumas observações.&nbsp;

Tab_1
cpo_a varchar(20) default null,
cpo_b int default 0,
cpo_c datetime default null,
cpo_d char(5) default null

Index indx01 (cpo_a, cpo_b, cpo_c);

Para esta tabela, usando o índice são válidas as seguintes pesquisas, para utilização do índice:


cpo_a, cpo_b, cpo_c
cpo_a, cpo_b
cpo_a



Caso o cpo_a NÃO seja utilizado em uma pesquisa, o índice NÃO será usado, como nos exemplos abaixo:


cpo_b, cpo_c
cpo_b
cpo_c



Outros exemplos de pesquisa, aonde o índice não será usado:


cpo_a, cpo_c (falta o cpo_b)
cpo_c, cpo_a (falta o cpo_b)



mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cd_reg = 1;
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table &nbsp; &nbsp;| type &nbsp;| possible_keys | key &nbsp; &nbsp; | key_len | ref &nbsp; | rows | Extra |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | const | PRIMARY &nbsp; &nbsp; &nbsp; | PRIMARY | 4 &nbsp; &nbsp; &nbsp; | const | &nbsp; &nbsp;1 | &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_a = 'a';
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp; &nbsp;| key_len | ref &nbsp; | rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ref &nbsp;| indx01 &nbsp; &nbsp; &nbsp; &nbsp;| indx01 | 10 &nbsp; &nbsp; &nbsp;| const | &nbsp; &nbsp;1 | Using where |
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_a = 'd';
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp; &nbsp;| key_len | ref &nbsp; | rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ref &nbsp;| indx01 &nbsp; &nbsp; &nbsp; &nbsp;| indx01 | 10 &nbsp; &nbsp; &nbsp;| const | &nbsp; &nbsp;1 | Using where |
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_a = 'd' and cpo_b = '2016-03-10' and cpo_c = 3;
+----+-------------+----------+------+---------------+--------+---------+-------------------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp; &nbsp;| key_len | ref &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+--------+---------+-------------------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ref &nbsp;| indx01 &nbsp; &nbsp; &nbsp; &nbsp;| indx01 | 24 &nbsp; &nbsp; &nbsp;| const,const,const | &nbsp; &nbsp;1 | Using where |
+----+-------------+----------+------+---------------+--------+---------+-------------------+------+-------------+
1 row in set, 2 warnings (0.01 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_a = 'd' and cpo_b = '2016-03-10';
+----+-------------+----------+------+---------------+--------+---------+-------------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp; &nbsp;| key_len | ref &nbsp; &nbsp; &nbsp; &nbsp; | rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+--------+---------+-------------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ref &nbsp;| indx01 &nbsp; &nbsp; &nbsp; &nbsp;| indx01 | 15 &nbsp; &nbsp; &nbsp;| const,const | &nbsp; &nbsp;1 | Using where |
+----+-------------+----------+------+---------------+--------+---------+-------------+------+-------------+
1 row in set (0.00 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_a = 'd';
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp; &nbsp;| key_len | ref &nbsp; | rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ref &nbsp;| indx01 &nbsp; &nbsp; &nbsp; &nbsp;| indx01 | 10 &nbsp; &nbsp; &nbsp;| const | &nbsp; &nbsp;1 | Using where |
+----+-------------+----------+------+---------------+--------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_a = 'd' and cpo_c = '2016-03-10' and cpo_b = 3;
+----+-------------+----------+------+---------------+--------+---------+-------------------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp; &nbsp;| key_len | ref &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+--------+---------+-------------------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ref &nbsp;| indx01 &nbsp; &nbsp; &nbsp; &nbsp;| indx01 | 24 &nbsp; &nbsp; &nbsp;| const,const,const | &nbsp; &nbsp;1 | Using where |
+----+-------------+----------+------+---------------+--------+---------+-------------------+------+-------------+
1 row in set (0.00 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_c = '2016-03-10' and cpo_b = 3;
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp;| key_len | ref &nbsp;| rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ALL &nbsp;| NULL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| NULL | NULL &nbsp; &nbsp;| NULL | &nbsp; &nbsp;2 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql&gt; EXPLAIN SELECT * from tabela_a WHERE cpo_b = 3;
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table &nbsp; &nbsp;| type | possible_keys | key &nbsp;| key_len | ref &nbsp;| rows | Extra &nbsp; &nbsp; &nbsp; |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| &nbsp;1 | SIMPLE &nbsp; &nbsp; &nbsp;| tabela_a | ALL &nbsp;| NULL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| NULL | NULL &nbsp; &nbsp;| NULL | &nbsp; &nbsp;2 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)