Beruflich Dokumente
Kultur Dokumente
67
5
4
26
6
18
47
34
38
57
72
68
74
Tarefa
Tempo (s)
0,25
0,02
Engine
(Inno,My,Mem)
My
InnoDB
use employees;
Observaes
Sem ndice
ndice Hash
2 ENGINE: [InnoDB]
2.1 Sem ndice
2.1.1 SELECT SQL_NO_CACHE * from employees.employees;
2.1.2 SELECT SQL_NO_CACHE `emp_no` , `birth_date`, `first_name` ,
`last_name` , `gender` , `hire_date` from `employees`.`employees`
order by first_name asc;
2.1.3 SELECT SQL_NO_CACHE `emp_no` , `birth_date`, `first_name` ,
`last_name` , `gender` , `hire_date` from `employees`.`employees`
order by first_name desc;
2.1.4 SELECT SQL_NO_CACHE * from employees where last_name like
'%T%' and first_name like 'Ar%';
2.1.5 SELECT SQL_NO_CACHE * from employees where first_name
='DeForest';
2.1.6 SELECT * from employees where first_name like 'Defo%';
2.1.7 SELECT * from employees where first_name like 'De%';
2.1.8 SELECT * from employees where first_name like '%Defo%';
Tarefas:
Tarefas:
Tarefas:
Tarefas:
Tarefas:
InnoDB
MyISAM
MEMORY
Tarefas:
Tarefas:
3 Tipos de ndices
3.1 B-Tree
Tarefas:
Executar
! show index from employees_mem; (Observar o index_type).
Executar as consultas e verificar quais usam ou no usam ndices:
! EXPLAIN SELECT * from employees where first_name like
'Valery%';
! EXPLAIN SELECT * from employees where first_name like
'%alery%';
! EXPLAIN SELECT * from employees where first_name like
last_name;
! SELECT * from employees where emp_no = 10001 * 2 or 1=1;
3.2 HASH
ENGINES: Memory
Indicado para comparaes que usam =, >=, <=
Mais rpido que B-tree
Tarefas:
Executar:
! CREATE INDEX idx_mesh ON employees_mem (mes) USING
hash;
! CREATE INDEX idx_mesb ON employees_mem (mes) USING
btree
! show index from employees_mem; (Observar o index_type).
! SELECT SQL_NO_CACHE * from `employees_mem` force
index (IDX_mesb) where mes=5;
! SELECT SQL_NO_CACHE * from `employees_mem` force
index (IDX_mesh) where mes=5;
! Usar explain nos selects acima; (Observar key)
Comparar as velocidades dos selects abaixo
! SELECT SQL_NO_CACHE * from `employees_mem` force
index (IDX_mesa) order by mes;
! SELECT SQL_NO_CACHE * from `employees_mem` force
index (IDX_mesh) order by mes;
Tarefas:
Executar:
! ALTER TABLE employees add column name_hash char(32);
! UPDATE employees set
name_hash=md5(concat(first_name,last_name))
! CREATE INDEX IDX_name_hash ON `employees` (name_hash
asc);
! SELECT * from employees where first_name='filipe' and
last_name='baba';
! SELECT * from employees where
name_hash=md5(concat('Filipe', 'Baba'));
Testar:
! SELECT * from employees where
name_hash=md5(concat('filipe', 'baba'));
! Usar explain nos selects acima; (Observar key)
Tarefas:
Executar:
! ALTER TABLE `employees` ADD FULLTEXT INDEX
`IDX_FT_name` (`first_name` ASC, `last_name` ASC) ;
! SELECT SQL_NO_CACHE * FROM `employees` WHERE
MATCH (first_name, last_name) AGAINST ('wi*' in boolean
mode);
! SELECT SQL_NO_CACHE * FROM `employees` WHERE
MATCH (first_name, last_name) AGAINST ('wi* -s' in boolean
mode);
! SELECT SQL_NO_CACHE * FROM `employees` WHERE
MATCH (first_name, last_name) AGAINST ('wi* -wiegley' in
boolean mode);
V3.mai.16
Atividades de Laboratrio
1) Em qual situao o MyISAM mais performtico? Justifique.
2) Em qual situao o InnoBD mais performtico? Justifique.
3) Em qual situao o Memory mais performtico? Justifique.
4) Qual a diferena em % de tempo de ganho com o ndice entre o
MyISAM e o InnoDB? Era esperado este ganho? Justifique
5) Em qual situao o ndice foi mais performtico (ASC ou DESC)
quando forado o ndice com correspondente ASC ou DESC?
Justifique.
6) possvel usar o Hash em todas as tabelas de uma Database? Qual
sua relao com o tamanho total da tabela?
7) Crie trs consultas usando = para first_name. Mostre e explique por
meio do tempo de consulta e explain qual o mais rpido: hash, b-tree
ou Full-Text.
8) Qual a resposta para situao de negcio colocado no exerccio de
fixao
Unique x Primary Key
CREATE TABLE Clientes (
ClienteID INT NOT NULL, ClienteNome VARCHAR(50) NOT
NULL, ClienteRG VARCHAR(15) NOT NULL, ClienteCPF
CHAR(11) NOT NULL, primary key (ClienteID));
CREATE UNIQUE INDEX un_cpf ON Clientes (ClienteCPF)
USING BTREE;
--Testes
INSERT INTO Clientes VALUES
(1,"Natanael","14538543","75712230466");
INSERT INTO Clientes VALUES
(2,"Marcelo","9845423","82013423920");
INSERT INTO Clientes VALUES (3,"Jonas","557023"
,"12345678900");
INSERT INTO Clientes VALUES (3,"Joao","557023"
,"99999999988");
INSERT INTO Clientes VALUES (4,"Joao","557023"
,"82013423920");
SELECT SQL_NO_CACHE * FROM CLIENTES;