1. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 1/68
AFUP AIX-MARSEILLEAFUP AIX-MARSEILLE
MEETUP 28 Mai 2019
1 . 1
2. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 2/68
Maxime LONGUET
50% - Administrateur système et services
Linux/Internet
50% - Développeur Php
Depuis 15 ans... :(
1 . 2
3. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 3/68
Cool maintenant je suis DevOps
Et un Dev Ops Libre !
1 . 3
5. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 5/68
Coordinateur de l'antenne AFUP Aix/Marseille
1 . 5
6. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 6/68
OPTIMIZE PRIMEOPTIMIZE PRIME
2 . 1
7. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 7/68
AU MENUAU MENU
Le choix du moteur de stockage
Les paramètres de configuration
Les systèmes de cache
Les optimisations système
vi /etc/mysql/my.cnf
2 . 2
8. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 8/68
CE QU'ON NE VERRA PASCE QU'ON NE VERRA PAS
Le bon MCD et le mauvais MCD
Le bon choix des types de données
La récriture de requête SQL
La bonne utilisation de doctrine
2 . 3
9. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 9/68
LES MOTEURSLES MOTEURS
3 . 1
10. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 10/68
CHOIX DES MOTEURSCHOIX DES MOTEURS
Innodb - Si beaucoup de lock de table c'est mieux.
Mais ACID = perte de perf
Myisam - Meilleur en simple lecture, mais moteur
non ACID et limité
3 . 2
12. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 12/68
INNODB_BUFFER_POOL_SIZEINNODB_BUFFER_POOL_SIZE
Le Cache principal (données et index).
2/3 DE LA RAM OU MOINS2/3 DE LA RAM OU MOINS
innodb_buffer_pool_size = 3G
3 . 4
13. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 13/68
CONSERVER VOTRE DUMP INNODBCONSERVER VOTRE DUMP INNODB
Permet de recharger les pages du buffer innodb
[mysqld]
innodb_buffer_pool_dump_at_shutdow = 1
innodb_buffer_pool_load_at_startup = 1
3 . 5
14. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 14/68
3 AUTRES VARIABLES3 AUTRES VARIABLES
innodb_log_file_size = 256M
le journal transactionnel => Une heure de
stockage
innodb_file_per_table
Fichier ibdata par table => 1
Innodb_io_capacity /
Innodb_io_capacity_max
SATA = 200 à 400 io/s
SSD = 2000 io/s 3 . 6
16. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 16/68
KEY_BUFFER_SIZEKEY_BUFFER_SIZE
Le seul, l'unique !
Comment on calcule son bon bon key_buffer_size
Key_read
nombre de lectures d'index non statisfaites
par le cache
Key_read_request
le nombre de lectures dans l'index
key_buffer_size = 128M
( 1 - Key_reads/Key_read_requests) *100 = 99%
3 . 8
17. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 17/68
LES TABLES TMPLES TABLES TMP
Mysql créer des tables temporaires pour travailler.
Qui peuvent finir sur le disque.
show global status LIKE 'Created_tmp%tables'
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Created_tmp_disk_tables | 49094 |
| Created_tmp_tables | 37842181 |
+-------------------------+----------+
4 . 1
18. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 18/68
RECOMMANDATIONSRECOMMANDATIONS
Ratio table sur disque : doit rester inférieur à 20 %
created_tmp_tables / Uptime -- En dessous de
20/s
created_tmp_disk_tables / Uptime -- En dessous
de 1/seconde
Tmp_disk_tables=((created_tmp_disk_tables*100/
(created_tmp_tables+created_tmp_disk_tables))
4 . 2
19. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 19/68
ON PEUT JOUER SUR LES VARIABLESON PEUT JOUER SUR LES VARIABLES
Variable_name Value
max_heap_table_size
taille maximale des tables
Memory
tmp_table_size
taille maximale de la
mémoire utilisée par les
tables temporaires
4 . 3
20. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 20/68
QUERY CACHEQUERY CACHE
Le cache c'est le bien ! ou pas ?!
5 . 1
21. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 21/68
LE CACHE DES REQUÊTES ?LE CACHE DES REQUÊTES ?
le résultat des SELECT en mémoire
Un renvoi immédiat du résultat au 2éme même
select
Génial! là ça va bien marcher !
5 . 2
22. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 22/68
QUI NE PASSE PAS EN CACHE ?QUI NE PASSE PAS EN CACHE ?
les requêtes non deterministes,
NOW(), RAND(), CURRENT_DATE, UUID() ...
SELECT sur table temporaire ne passe pas en
cache
SELECT dans les procédures stockées
Les syntaxes différentes
SELECT id, nom FROM t;
SELECT id,nom FROM t;
5 . 3
23. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 23/68
LES PARAMÈTRES DELES PARAMÈTRES DE
CONFIGURATION DU CACHECONFIGURATION DU CACHE
SHOW GLOBAL VARIABLES LIKE 'query_cache%'
Variable_name Value
query_cache_limit 512000
query_cache_min_res_unit 4096
query_cache_size 47185920
query_cache_type ON
query_cache_wlock_invalidate OFF
6 . 1
24. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 24/68
Paramètre Définition
query_cache_size Taille du cache global
query_cache_limit
Taille maximum du
résultat d'une requête
query_cache_min_res_unit
taille minimum du
block de stockage.
query_cache_type ON / DEMAND / OFF
6 . 2
25. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 25/68
TROUVER LES BONNES VALEURSTROUVER LES BONNES VALEURS
show global status like '%qcache%'
Variable_name Value
Qcache_free_blocks 1509
Qcache_free_memory 45212856
Qcache_hits 20775204
Qcache_inserts 4072610
Qcache_lowmem_prunes 303605
Qcache_not_cached 223013
Qcache_queries_in_cache 940
Qcache_total_blocks 3499
6 . 3
26. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 26/68
Paramètre Définition
Qcache_free_memory espace libre du cache
Qcache_lowmem_prunes
Nombre de requêtes
supprimées du cache
Qcache_hits Nombre de hit
6 . 4
27. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 27/68
On se sert ensuite de 'Com_select' et 'Uptime'
Paramètre Définition
Com_select Nombre total de select executé
Uptime Services démarré depuis ...
show global status like 'Com_select'
Com_select 4518550
show global status like 'Uptime'
Uptime 16054064
6 . 5
28. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 28/68
CALCUL DE TAUXCALCUL DE TAUX
TAUX DE HIT
TAUX d'insertions
Ou encore d'autre taux ...
100*Qcache_hits/(Qcache_hits+Com_select)
100*Qcache_inserts/Com_select
6 . 6
29. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 29/68
https://stackoverflow.com/questions/45259529/query-cache-size
* Pct Query Cache free -- Qcache_free_memory / query_cache_siz
* Running out of room in QC -- Qcache_lowmem_prunes / Uptime
* SQL_CACHE attempted, but ignored -- Qcache_not_cached / Upti
* Percent of SELECTs that were not cached in the QC. -- Qcache
* Fragmentation in Query Cache -- Qcache_free_blocks * 4096 /
* Hit to insert ratio -- high is good -- Qcache_hits / Qcache_
* Hit ratio -- SELECTs that used QC -- Qcache_hits / (Qcache_h
* Query cache hit rate -- Qcache_hits / (Qcache_hits + Qcache_
* Fragmentation in Query Cache. -- Qcache_free_blocks / Qcache
* Invalidations/sec. -- Qcache_inserts - Qcache_queries_in_cac
6 . 7
30. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 30/68
Exemple à ne pas faire :
!! Il ne faut pas un query_cache_limit trop grand !!
query_cache_size pas forcément plus que 50M
Current query_cache_size = 512 M
Current query_cache_limit = 128 M
Current query_cache_min_res_unit = 4 K
6 . 8
31. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 31/68
LE CACHE C'EST PAS BIEN SILE CACHE C'EST PAS BIEN SI
Trop de requêtes en écriture
Beaucoup de requêtes différentes
Un cache trop petit
Beaucoup d'accès concurrents
6 . 9
32. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 32/68
DONC EN FAIT PAS DE CACHEDONC EN FAIT PAS DE CACHE
Mysql 5.7 le cache est déprécié, Mysql 8 le cache
n'existe plus !
Mariadb donne sur sa doc en réponse courte :
query_cache_type = OFF
query_cache_size = 0
A la rigueur on pourrait garder le cache si
Qcache_hits / Qcache_inserts > 5
6 . 10
33. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 33/68
ET DANS TOUS LES CAS PAS ENET DANS TOUS LES CAS PAS EN
DEV !DEV !
Pour tester vos requêtes : SELECT SQL_NO_CACHE
Désactivé tout simplement le cache mysql en dev
6 . 11
34. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 34/68
D'AUTRES CACHES UTILESD'AUTRES CACHES UTILES
6 . 12
35. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 35/68
TABLE_OPEN_CACHETABLE_OPEN_CACHE
Nombre de table ouverte pour chaque thread.
table_open_cache de 4000 n'est pas choquant
table_definition_cache un peu plus que le nombre de
tables total
SELECT @@table_open_cache;
SELECT @@table_definition_cache;
show global status like 'opened_tables';
6 . 13
36. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 36/68
LES THREADSLES THREADS
thread_cache_size et Threads_created
Threads_created : Doit rester en dessous de 5 par
secondes
6 . 14
37. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 37/68
LES SLOW QUERIESLES SLOW QUERIES
7 . 1
38. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 38/68
L'ACTIVATION DES SLOWS QUERIESL'ACTIVATION DES SLOWS QUERIES
De façon temporaire
Global
set global slow_query_log = 1;
set global slow_query_log_file = <some file na
set global long_query_time = 3;
slow_query_log = 1;
long_query_time = 3
slow_query_log_file = '/___/___/_____/___.log
7 . 2
39. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 39/68
Mais comment on étudie des slow querie
Premier choix on regarde juste le fichier....
Diminuer par palier 10s -> 7s -> 3s -> 1s
7 . 3
40. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 40/68
Mais est-ce qu'une requête de 7secondes qui
arrive une fois par semaine est plus importante
qu'une requête de 1,4 seconde qui se produit 700
fois.
7 . 4
41. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 41/68
LES OUTILS DE SLOW QUERIESLES OUTILS DE SLOW QUERIES
7 . 5
42. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 42/68
MYSQLDUMPSLOWMYSQLDUMPSLOW
Un binaire inclus avec mysql.
Note : Si vous avez accès en lecture au slow queries
alors vous pouvez lancer ce binaire avec un simple
utilisateur.
7 . 6
43. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 43/68
PT_QUERY_DIGESTPT_QUERY_DIGEST
En provenance de PerconaToolkit
pt-query-digest /var/log/mysql/mysql-slow.log
Sur serveur de dev se faire une petite session avec
long_query_time = 0
# Profile
# Rank Query ID Response time Calls R/Call V/M Item
# ==== ============= ============== ===== ======= ===== ======
7 . 7
44. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 44/68
ANEMOMETERANEMOMETER
Interface Web des résultats compilés de pt-query-
digest
https://github.com/box/Anemometer
7 . 8
45. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 45/68
LES INDEXLES INDEX
On cherche les requêtes n'utilisant pas d'index, le plus
souvent de façon temporaire
SET GLOBAL
log_queries_not_using_indexes=1;
7 . 9
46. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 46/68
LA GESTION DES INDEXLA GESTION DES INDEX
b.a.-ba. d'une base de données.
Toujours avoir une clef primaire
Index sur les Foreign-Keys
Index sur les Conditions et les ORDER BY selectif.
Index sur plusieur colonnes correspondant !
8 . 1
47. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 47/68
Juger de la pertinence d'un index selon sa
sélectivité. On met pas un index sur Homme /
Femme ça sert à rien.
Sur une table commune de 100 lignes il n'y a pas
d'intêret d'avoir un index...
Un index composite (A,B) peut utiliser A seul mais
pas B seul
8 . 2
48. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 48/68
LA MAINTENANCELA MAINTENANCE
TABLE FRAGMENTÉETABLE FRAGMENTÉE
A surveiller via une simple requête :
Et il suffit alors de lancer
SELECT CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS Name,Data_free
FROM information_schema.TABLES
WHERE TABLE_SCHEMA NOT IN ('information_schema','mysql')
AND Data_free > 0;
OPTIMIZE TABLE TABLE1,TABLE2,TABLE3
9 . 1
49. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 49/68
Ou de façon périodique
mysqloptimize -o -A -u root -p
9 . 2
50. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 50/68
OPTIMIZEOPTIMIZE
Pour les tables MyISAM, celles-ci seront
défragmentées et l’espace disque vide non utilisé
sera récupéré.
Pour les tables InnoDB, l’optimisation consiste en
un alter table pour récupérer l’espace libre.
Si vous avez des index, ces derniers seront
réindexés et les stastistiques mises à jour.
9 . 3
51. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 51/68
LES OPTIMISATIONSLES OPTIMISATIONS
SYSTÈMESYSTÈME
10 . 1
52. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 52/68
LE PROCESSEURLE PROCESSEUR
Beaucoup de Core pour les accès concurrents
Des Ghz pour une requête
Des GHz pour la replication (cpufreq-info Vérifier
que vous n'avez pas de powersave en governor du
cpu)
10 . 2
53. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 53/68
LE DISQUELE DISQUE
Séparation des flux (journal, binlog,
partitionnement) sur disque distinct
Attention pas de RAID5 et de séparation des flux
sur des simples partitions distinctes...
Privilégier : SSD -> NVMe - Gaint significatif pour
les bases non optimisées
Les options noatime,nodirtime
10 . 3
54. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 54/68
LA RAMLA RAM
Un rôle crucial
Assez pour mettre toute la base en cache
(Key_buffer_size et innodb_buffer_pool_size)
Ou au moins suffisament pour les données
chaudes
Le bon vieux swapiness (60 -> 10)
10 . 4
55. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 55/68
LES OUTIL D'AUDITLES OUTIL D'AUDIT
11 . 1
56. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 56/68
MYSQLTUNNERMYSQLTUNNER
TUNING-PRIMER.SHTUNING-PRIMER.SH
git clone https://github.com/major/MySQLTuner-perl.git
cd MySQLTuner-perl/
perl mysqltuner.pl
$ wget http://www.day32.com/MySQL/tuning-primer.sh
$ sh tuning-primer.sh
11 . 2
57. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 57/68
PHPMYADMINPHPMYADMIN
Phpmyadmin fournit également des conseils
quand on va dans les variables d'état
11 . 3
58. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 58/68
LE MEILLEUR TRUCLE MEILLEUR TRUC
11 . 4
59. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 59/68
QUI DECHIRE TOUTQUI DECHIRE TOUT
11 . 5
60. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 60/68
MIGRER SOUSMIGRER SOUS
POSTGRESQL11POSTGRESQL11
:):)
11 . 6
61. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 61/68
RENDEZ-VOUSRENDEZ-VOUS
19 JUIN 2019 - PGDAY FRANCE à Lyon
www.pgday.fr
11 . 7
62. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 62/68
MERCIMERCI
Merci pour l'orga et à Oxatis
12
64. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 64/68
L'OPTIMISEUR DE REQUÊTEL'OPTIMISEUR DE REQUÊTE
EXPLAIN .....
optimizer_search_depth = 7
13 . 2
65. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 65/68
MYSQLTMP EN TMPFSMYSQLTMP EN TMPFS
Création du ramdisk et point de montage
Modification de /etc/fstab
Modification de my.cnf
tmpdir = "/mnt/ramdisk"
# mkdir -p /mnt/ramdisk
# mount -t tmpfs -o size=512M tmpfs /mnt/ramdisk
# chown mysql:mysql /mnt/ramdisk
tmpfs /mnt/ramdisk tmpfs rw,mode=1777,size=512
13 . 3
66. 29/05/2019 MYSQL / MARIADB
localhost:8383/conference/mysql/optimyzeprime.html??print-pdf#/ 66/68
14