quinta-feira, 10 de março de 2016

MySQL - ìndices

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

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> EXPLAIN SELECT * from tabela_a WHERE cd_reg = 1;
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table    | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | tabela_a | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)

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

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

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

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

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

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

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

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

Linux - Permissão de Arquivos

chmod 400 file - Read by owner
chmod 040 file - Read by group
chmod 004 file - Read by world 

chmod 200 file - Write by owner
chmod 020 file - Write by group
chmod 002 file - Write by world

chmod 100 file - execute by owner
chmod 010 file - execute by group
chmod 001 file - execute by world

Comuns:
777 - Liberado para todos para LER, GRAVAR e EXECUTAR
555 - Liberado para todos para LER e EXECUTAR
666 - Liberado para todos para LER e GRAVAR
444 - Liberado para todos para LER
222 - Liberado para todos para GRAVAR
111 - Liberado para todos para EXECUTAR

755 - Acesso completo para o Proprietário (owner) e aos demais permitido ler e executar;
775 - Acesso completo para o Proprietário (owner) e para membros do grupo; aos demais ler e executar;
750 - Acesso completo para o Proprietário (owner); para membros do grupo é permitido ler e executar e proibido acesso aos demais usuários;