3. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas ?
Conclusion
4. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas ?
Conclusion
5. Pourquoi optimiser les index
Les problèmes de
bases de données
sont régulièrement
dans le top 5 des
problèmes de
performance d'une
application Java
6. Pourquoi optimiser les index
JPA/Hibernate, Spring JDBC et JDBC ont pour
point commun de générer des requêtes SQL
8. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas ?
Conclusion
11. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas ?
Conclusion
29. Théorie : Info sur les index d'une table
select index_name, index_type, uniqueness
from user_indexes
where table_name = 'CLIENTS';
30. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas ?
Conclusion
34. Quiz 1 : Explication
Plus d'index != meilleur perf
Il suffit d'un seul
35. Quiz 1: Nombre d'index
Plus d'index != plus de performance
Et donc il faut trouver le bon index
Making of
http://blog.aliecom.com/pourquoi-il-faut-faire-attention-au-nombre-dindex-sql/
36. Quiz 2 : ?
L'index est il utilisé ?
create table CLIENTS ....
create index pk_clients on clients(id_client)
select id_client from clients where id_client = '5'
38. Quiz 2 : Explication
Index sur clé primaire crée
automatiquement dans Oracle.
SQL> create index pk_clients on
clients(id_client);
ERROR at line 1:
ORA-01408: such column list already indexed
39. Quiz 2 : Index par défaut
Be aware sur la politique de création
d'index par défaut
40. Quiz 3 : ?
Pourquoi l'index n'est pas utilisé ?
create index ix_clientx_pays on clients(pays)
select nom,prenom from clients where upper(pays) =
'UNITED STATES'
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 99K| 9 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| CLIENTS | 1000 | 99K| 9 (0)| 00:00:01 |
-----------------------------------------------------------------------------
51. Quiz 4 : Solution
CREATE INDEX index_null ON
clients (sex, '1')
52. Quiz 4 : Solution
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 22000 | 32 (0)| 00:00:01
| 1 | TABLE ACCESS BY INDEX ROWID| CLIENTS | 1000 | 22000 | 32 (0)| 00:00:01
|* 2 | INDEX RANGE SCAN | INDEX_NULL | 1000 | | 4 (0)| 00:00:01
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("SEX" IS NULL)
53. Quiz 4 : Indexer les null
Be aware à ne pas oublier les
contraintes dans le schéma de bdd
Be aware aux valeurs null
Bien connaitre sin serveur de bdd
54. Quiz 5 : ?
Existe t il un meilleur index ?
create index clients_identifiant_unique_ix on
clients(identifiant_unique);
select id_client from clients where identifiant_unique
like '109876545210387';
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 |
| 1 | TABLE ACCESS BY INDEX ROWID| CLIENTS | 1 |
|* 2 | INDEX RANGE SCAN | CLIENTS_IDENTIFIANT_UNIQUE_IX | 4 |
--------------------------------------------------------------------------------
55. Quiz 5 : Indice
create index clients_identifiant_unique_ix on
clients(identifiant_unique)
select id_client from clients where identifiant_unique
like '109876545210387'
68. Quiz 7 : Moteur de base de données
Be aware sur les possibilitées du
serveur de bases de données utilisés
Be aware sur le contexte des astuces
trouvées sur Internet
69. Quiz 8 : ?
Pourquoi l'index n'est pas utilisé ?
create index ix_clientx_sex on clients(sex)
# Ajout de données dans la table clients
select nom,prenom from clients where sex = 'MALE'
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 510 | 11220 | 9 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| CLIENTS | 510 | 11220 | 9 (0)| 00:00:01 |
-----------------------------------------------------------------------------
73. Quiz 8 : Statistiques
Ne pas oublier de faire régulièrement
des update stat
Ne pas oublier de faire des update
stats après un gros changement de
données (update, insert, delete)
74. Quiz 9 : ?
Existe t il un meilleur index ?
create index clients_nom_ix on clients(nom)
select nom, prenom from clients where nom like 'A%'
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 37 | 2516 | 8
| 1 | TABLE ACCESS BY INDEX ROWID| CLIENTS | 37 | 2516 | 8
|* 2 | INDEX RANGE SCAN | CLIENTS_NOM_IX | 9 | | 2
--------------------------------------------------------------------------------
81. Quiz 10 : Solution
create index rev_clients_id_unique_nom_ix on
clients(reverse(identifiant_unique),nom)
select nom from clients where
reverse(identifiant_unique) like '1%'
82. Quiz 10 : Accès aux données
Be aware lors de l'écriture des
requêtes
83. Quiz 11 : ?
Pourquoi l'index n'est pas utilisé ?
create index ix_clients_num_rue on
clients(numero_de_rue)
select nom, prenom from clients where numero_de_rue = 1
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 20 | 360 | 9 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| CLIENTS | 20 | 360 | 9 (0)| 00:00:01 |
-----------------------------------------------------------------------------
92. Quiz 12 : Accès aux données
Be aware lors de l'écriture des
requêtes
93. Quiz 13 : ?
Quel est la requête la plus rapide ?
A : select count(*) from achats
B : select count(1) from achats
94. Quiz 13 : Solution
select count(*) from achats
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 516 (1)| 00:00:07 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| SYS_C007034 | 985K| 516 (1)| 00:00:07 |
-----------------------------------------------------------------------------
select count(1) from achats
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 516 (1)| 00:00:07 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| SYS_C007034 | 985K| 516 (1)| 00:00:07 |
-----------------------------------------------------------------------------
95. Quiz 13 : Astuces sur Internet
Méfiez vous des "astuces" trouvées
sur Internet
Mesurez, ne devinez pas
96. Quiz 14 : ?
Existe t il un meilleur index ?
create index achats_p_id on achats(prix,id_achat)
select prix,id_achat from achats where prix > 30 order
by prix asc,id_achat desc
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 963K| 9411K| | 4629 (2)|
| 1 | SORT ORDER BY | | 963K| 9411K| 18M| 4629 (2)|
|* 2 | INDEX FAST FULL SCAN| ACHATS_P_ID | 963K| 9411K| | 764 (2)|
-------------------------------------------------------------------------------------
97. Quiz 14 : Indice
Index = Représentation triée des éléments
indexés à l'aide d'un arbre équilibré
98. Quiz 14 : Indice
create index achats_p_id on achats(prix,id_achat)
select prix,id_achat from achats where prix > 30 order
by prix asc,id_achat desc
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 963K| 9411K| | 4629 (2)|
| 1 | SORT ORDER BY | | 963K| 9411K| 18M| 4629 (2)|
|* 2 | INDEX FAST FULL SCAN| ACHATS_P_ID | 963K| 9411K| | 764 (2)|
-------------------------------------------------------------------------------------
99. Quiz 14 : Solution
create index achats_p_id2 on achats(prix asc,id_achat
desc)
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 963K| 9411K| 2827 (1)| 00:00:34 |
|* 1 | INDEX RANGE SCAN| ACHATS_P_ID2 | 963K| 9411K| 2827 (1)| 00:00:34 |
---------------------------------------------------------------------------------
100. Quiz 14 : Ordre des index
Be aware sur l'ordre des index et
l'ordre d'accès aux données
101. Quiz 15 : ?
Pourquoi l'index n'est pas utilisé ?
create index clients_n_ix on clients(nom)
104. Quiz 15 : Explication
Hibernate:
/* criteria query */ select
this_.ID_CLIENT as ID1_0_0_,
this_.NOM as NOM0_0_,
...
this_.IDENTIFIANT_UNIQUE as IDENTIF17_0_0_
from
SYSTEM.CLIENTS this_
where
lower(this_.NOM)=?
105. Quiz 15 : Solution
Index de fonction
Ne pas utiliser ilike/ignoreCase()
Contrainte sur les valeurs de la table
106. Quiz 15 : ORM/Framework
Be aware sur le comportement des
librairies utilisées
107. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas ?
Conclusion
126. Eliminer l'inutile
Attention aux n+1 select avec les ORM
http://arodrigues.developpez.com/tutoriels/java/performance/hibernate-
performance-part1-strategies-chargement/
128. Eliminer l'inutile
Ne récupérer que le nécessaire
select * from clients
Statistics
----------------------------------------------------------
19 329 961 bytes sent via SQL*Net to client
100000 rows processed
select nom,prenom from clients
Statistics
----------------------------------------------------------
2 351 810 bytes sent via SQL*Net to client
100000 rows processed
133. Ajout d'index
Sur quoi mettre un index ?
clé étrangère
clause where
index couvrant
order
group by
union
distinct
134. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas ?
Conclusion
135. Et si cela ne suffit pas ?
Table space
index bitmap
schéma bdd
IOT (Index Organised Table)
Tuning du serveur de base de données
...
Appeler un DBA
136. Gestion des index SQL : Soyez AWARE !
Pourquoi optimiser les index
Attention
Un peu de théorie
Quiz
Méthodologie
Et si cela ne suffit pas
Conclusion