Version sans cache


Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

Rechercher dans le manuel MySQL

8.2.1.14 IS NULL Optimization

MySQL can perform the same optimization on col_name IS NULL that it can use for col_name = constant_value. For example, MySQL can use indexes and ranges to search for NULL with IS NULL.

Examples:

  1. SELECT * FROM tbl_name WHERE key_col IS NULL;
  2.  
  3. SELECT * FROM tbl_name WHERE key_col <=> NULL;
  4.  
  5. SELECT * FROM tbl_name
  6.   WHERE key_col=const1 OR key_col=const2 OR key_col IS NULL;

If a WHERE clause includes a col_name IS NULL condition for a column that is declared as NOT NULL, that expression is optimized away. This optimization does not occur in cases when the column might produce NULL anyway (for example, if it comes from a table on the right side of a LEFT JOIN).

MySQL can also optimize the combination col_name = expr OR col_name IS NULL, a form that is common in resolved subqueries. EXPLAIN shows ref_or_null when this optimization is used.

This optimization can handle one IS NULL for any key part.

Some examples of queries that are optimized, assuming that there is an index on columns a and b of table t2:

  1. SELECT * FROM t1 WHERE t1.a=expr OR t1.a IS NULL;
  2.  
  3. SELECT * FROM t1, t2 WHERE t1.a=t2.a OR t2.a IS NULL;
  4.  
  5. SELECT * FROM t1, t2
  6.   WHERE (t1.a=t2.a OR t2.a IS NULL) AND t2.b=t1.b;
  7.  
  8. SELECT * FROM t1, t2
  9.   WHERE t1.a=t2.a AND (t2.b=t1.b OR t2.b IS NULL);
  10.  
  11. SELECT * FROM t1, t2
  12.   WHERE (t1.a=t2.a AND t2.a IS NULL AND ...)
  13.   OR (t1.a=t2.a AND t2.a IS NULL AND ...);

ref_or_null works by first doing a read on the reference key, and then a separate search for rows with a NULL key value.

The optimization can handle only one IS NULL level. In the following query, MySQL uses key lookups only on the expression (t1.a=t2.a AND t2.a IS NULL) and is not able to use the key part on b:

  1. SELECT * FROM t1, t2
  2.   WHERE (t1.a=t2.a AND t2.a IS NULL)
  3.   OR (t1.b=t2.b AND t2.b IS NULL);

Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-is-null-optimization.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut